# Matrix Calculator for Addition, Scalar Multiplication, Matrix Multiplication
# Matrix Transpose, Matrix Inverse and Determinant calculation.
# Click on the next cell to start:
# 1. Add matrices
# 2. Multiply matrix by a constant
# 3. Multiply matrices
# 4. Transpose matrix
# 5. Calculate a determinant
# 6. Inverse matrix
# 0. Exit
# Click on a number to carry out operation
# Enter size of matrix and press Enter. For example:
# Enter size of first matrix: 3 3
# Enter matrix by typing all rows at once separated by space. Then Press Enter.
# col1 col2 col3
# 1 2 3 - Row 1
# 3 6 9 - Row 2
# 5 9 11 - Row 3
# Thank you for viewing!
20
import copy
class NumericMatrix:
def __init__(self):
self.mat_list = []
self.res = []
self.nrow = None
self.ncol = None
self.A = None
def make_matrix(self, mat_no=None):
if mat_no:
aaa = input(f'Enter size of {mat_no} matrix: ').split()
print(f'Enter {mat_no} matrix:')
else:
aaa = input(f'Enter size of matrix: ').split()
print('Enter matrix:')
aaa = [eval(a) for a in aaa]
if len(aaa) == 2:
self.nrow, self.ncol = aaa
for _ in range(self.nrow):
ans = input().split()
ans = [eval(a) for a in ans]
self.mat_list.append(ans)
else:
print('The operation cannot be performed.')
def __add__(self, other):
if self.nrow == other.nrow and self.ncol == other.ncol:
print('The result is:')
c = [[] for _ in range(self.nrow)]
for i in range(self.nrow):
for j in range(self.ncol):
c[i].append((self.mat_list[i][j]) + (other.mat_list[i][j]))
for cc in c:
print(*cc)
else:
print('The operation cannot be performed.')
def k_mult(self):
inp = input('Enter constant: ')
multiply_by = eval(inp)
k_product = [[(multiply_by * (j)) for j in a_list] for a_list in self.mat_list]
print('The result is:')
if isinstance(inp, int) and isinstance(self.mat_list[0], int):
for one_row in k_product:
print(*[int(kk) for kk in one_row])
else:
for one_row in k_product:
print(*one_row)
def clear(self):
self.mat_list = []
def __mult__(self, other):
count = []
if self.ncol == other.nrow:
for a in self.mat_list:
for b in zip(*other.mat_list):
count.append([a, b])
for v1, v2 in zip(a,b):
ress = (v1) * (v2)
self.res.append(ress)
result = [[] for _ in range(self.nrow)]
z = int(other.nrow)
c = int(z)
i = 0
for j in range(self.nrow):
for _ in range(other.ncol):
sum_res = sum(self.res[i:c])
result[j].append(sum_res)
i = c
c += z
print('The result is:')
for one_result in result:
print(*one_result)
else:
print('The operation cannot be performed.')
def transpose(self, inp):
B = [[] for _ in range(self.ncol)]
for i in range(self.ncol):
for j in range(self.nrow):
B[i].append(0)
for i in range(self.ncol):
for j in range(self.nrow):
B[i][j] = self.mat_list[j][i]
print('The result is:')
if inp == '1':
for one_result in B:
print(*one_result)
elif inp == '2':
B = B[::-1]
B = [one_item[::-1] for one_item in B]
for one_result in B:
print(*one_result)
elif inp == '3':
B = [one_list[::-1] for one_list in self.mat_list]
for one_result in B:
print(*one_result)
elif inp == '4':
B = self.mat_list[::-1]
for one_result in B:
print(*one_result)
def getMatrixMinor(self, m,i,j):
return [row[:j] + row[j+1:] for row in (m[:i]+m[i+1:])]
def getMatrixDeterminant(self, 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]
determinant = 0
for c in range(len(m)):
determinant += ((-1)**c)*m[0][c]*self.getMatrixDeterminant(self.getMatrixMinor(m,0,c))
return determinant
def getinverse(self, A):
B = copy.deepcopy(A)
j = 0
i = 0
c = 0
for j in range(len(A[0])):
for i in range(len(A)):
if (i+j) % 2 == 0:
B[i][j] = self.getMatrixDeterminant(self.getMatrixMinor(A, i, j))
else:
B[i][j] = (-1) * self.getMatrixDeterminant(self.getMatrixMinor(A, i, j))
determinant = self.getMatrixDeterminant(A)
if determinant != 0:
cofactor_transpose = self.transpose_cofactor(B)
inverse_matrix = [[(1/determinant * item) for item in a_list] for a_list in cofactor_transpose]
print('The result is:')
for one_result in inverse_matrix:
print(*one_result)
else:
print('This matrix doesn\'t have an inverse.')
def transpose_cofactor(self, A):
nrow = len(A)
ncol = len(A[0])
B = [[] for _ in range(ncol)]
for i in range(ncol):
for j in range(nrow):
B[i].append(0)
for i in range(ncol):
for j in range(nrow):
B[i][j] = A[j][i]
return B
def menu():
print('1. Add matrices\n2. Multiply matrix by a constant\n3. Multiply matrices\n4. Transpose matrix\n5. Calculate a determinant\n6. Inverse matrix\n0. Exit')
while True:
inp = input('Your choice: ')
if inp == '0':
exit()
elif inp == '1':
A = NumericMatrix()
A.make_matrix(mat_no='first')
B = NumericMatrix()
B.make_matrix(mat_no='second')
A + B
A.clear()
B.clear()
menu()
elif inp == '2':
A = NumericMatrix()
A.make_matrix()
A.k_mult()
A.clear()
menu()
elif inp == '3':
A = NumericMatrix()
A.make_matrix(mat_no='first')
B = NumericMatrix()
B.make_matrix(mat_no='second')
A.__mult__(B)
A.clear()
B.clear()
menu()
elif inp == '4':
print('1. Main diagonal\n2. Side diagonal\n3. Vertical line\n4. Horizontal line')
t_inp = input('Your choice: ')
A = NumericMatrix()
A.make_matrix()
A.transpose(t_inp)
A.clear()
menu()
elif inp == '5':
C = NumericMatrix()
C.make_matrix()
print('The answer is:')
print(C.getMatrixDeterminant(C.mat_list))
C.clear()
menu()
elif inp == '6':
D = NumericMatrix()
D.make_matrix()
D.getinverse(D.mat_list)
D.clear()
menu()
menu()
1. Add matrices 2. Multiply matrix by a constant 3. Multiply matrices 4. Transpose matrix 5. Calculate a determinant 6. Inverse matrix 0. Exit Your choice: 3 Enter size of first matrix: 3 3 Enter first matrix: 1 2 3 3 6 9 5 9 11 Enter size of second matrix: 3 1 Enter second matrix: 1 2 3 The result is: 14 42 56 1. Add matrices 2. Multiply matrix by a constant 3. Multiply matrices 4. Transpose matrix 5. Calculate a determinant 6. Inverse matrix 0. Exit Your choice: 0 Your choice: 0 Your choice: 0