Numeric Matrix Processor

Ce script fait partie des projets Python de HyperSkill.

j'ai fait toute la formation python de Hyperskill pendant le Confinement 2020.

is_float = False
def isfloat(value):
try:
val = float(value)
return not val.is_integer()
except ValueError:
return False
def input_matrix():
global is_float
size = input("Enter size of matrix : ").split(" ")
mat = []
print("Enter matrix: ")
for i in range(int(size[0])):
user_input = input().split(" ")
for e in user_input:
if isfloat(e):
is_float = True
if is_float:
user_input_fix = [float(e) for e in user_input]
else:
user_input_fix = [int(float(e)) for e in user_input]
mat.append(user_input_fix)
return mat, size
def add(mat1, mat2):
if mat1[1] != mat2[1]:
return "ERROR"
mat_x = int(mat1[1][0])
mat_y = int(mat1[1][1])
return_matrix = {i: [] for i in range(mat_x)}
for x in range(mat_x):
for y in range(mat_y):
if len(mat1[0][x]) != mat_y or len(mat2[0][x]) != mat_y:
return "The operation cannot be performed."
return_matrix[x].append(mat1[0][x][y] + mat2[0][x][y])
return return_matrix
def mul_c(matrix, multiplier):
mat_x = int(matrix[1][0])
mat_y = int(matrix[1][1])
return_matrix = {i: [] for i in range(mat_x)}
print(matrix)
for x in range(mat_x):
for y in range(mat_y):
return_matrix[x].append(matrix[0][x][y] * multiplier)
return return_matrix
def mul(mat1, mat2):
mat_y = int(mat1[1][1])
mat2_x = int(mat2[1][0])
if mat_y != mat2_x:
return "The operation cannot be performed."
a = mat1[0]
b = mat2[0]
matrix = [[sum(a[i][k] * b[k][j] for k in range(len(b))) for j in range(len(b[0]))] for i in range(len(a))]
return {k: [round(i, 3) for i in v] for k, v in zip(range(len(matrix)), matrix)}
def format_matrix(matrix):
if type(matrix) == str:
print(matrix)
else:
for i in matrix:
print(" ".join([str(e) for e in matrix[i]]))
def insert(ua):
if isfloat(ua):
return float(ua)
return int(ua)
def minor(m, i, j):
return [row[:j] + row[j+1:] for row in (m[:i] + m[i+1:])]
def determinant(m):
if len(m) == 1:
return m[0][0]
if len(m) == 2:
return m[0][0] * m[1][1] - m[0][1] * m[1][0]
det = 0
for c in range(len(m)):
det += ((-1) ** c) * m[0][c] * determinant(minor(m, 0, c))
return det
def transpose_matrix_ask():
print("1. Main diagonal\n"
"2. Side diagonal\n"
"3. Vertical line\n"
"4. Horizontal line")
choice = int(input())
if choice == 1:
raw_matrix = input_matrix()[0]
matrix = list(zip(*raw_matrix))
format_matrix({k: v for k, v in zip(range(len(matrix)), matrix)})
elif choice == 2:
raw_matrix = input_matrix()[0]
matrix = list(zip(*reversed(raw_matrix)))[::-1]
format_matrix({k: v for k, v in zip(range(len(matrix)), matrix)})
elif choice == 3:
matrix = input_matrix()[0]
for i in range(len(matrix)):
matrix[i] = matrix[i][::-1]
format_matrix({k: v for k, v in zip(range(len(matrix)), matrix)})
elif choice == 4:
raw_matrix = input_matrix()[0]
matrix = raw_matrix[::-1]
format_matrix({k: v for k, v in zip(range(len(matrix)), matrix)})
def process(_type):
if _type == "add":
matrix_1 = input_matrix()
matrix_2 = input_matrix()
matrix = add(matrix_1, matrix_2)
format_matrix(matrix)
elif _type == "mul_c":
raw_matrix = input_matrix()
multiplier = insert(input("Enter constant : "))
matrix = mul_c(raw_matrix, multiplier)
format_matrix(matrix)
elif _type == "mul":
matrix_1 = input_matrix()
matrix_2 = input_matrix()
matrix = mul(matrix_1, matrix_2)
format_matrix(matrix)
elif _type == "transpose":
transpose_matrix_ask()
elif _type == "determinant":
raw_matrix = input_matrix()[0]
print("The result is:", determinant(raw_matrix), sep="\n")
def ask():
global is_float
print("1. Add matrices\n"
"2. Multiply matrix by a constant\n"
"3. Multiply matrices\n"
"4. Transpose matrix\n"
"5. Calculate a determinant\n"
"0. Exit")
try:
choice = int(input("Your choice: "))
if choice == 1:
process("add")
if choice == 2:
process("mul_c")
if choice == 3:
process("mul")
if choice == 4:
process("transpose")
if choice == 5:
process("determinant")
if choice == 0:
exit(0)
is_float = False
except ValueError:
print("please enter a number")
while True:
ask()
view raw matrix.py hosted with ❤ by GitHub

cyvax - 2024