Skip to content

Commit

Permalink
Merge pull request #91 from mosesrukshan/main
Browse files Browse the repository at this point in the history
Added matrix calculator
  • Loading branch information
avastino7 authored Nov 1, 2022
2 parents 35261eb + 525457c commit 38d829b
Showing 1 changed file with 72 additions and 0 deletions.
72 changes: 72 additions & 0 deletions Matrix_Calculator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
operation = input("Operation(use these notations; +,-,*) :")
C_A = int(input("No of columns of matrix A:"))
R_A = int(input("No of raws of matrix A:"))
print("Enter Matrix A:")
A = [[int(s) for s in input().split()] for i in range(C_A)] #Input matrix A

C_B = int(input("No of columns of matrix B:"))
R_B = int(input("No of raws of matrix B:"))
print("Enter Matrix B:")
B = [[int(s) for s in input().split()] for j in range(C_B)] #Input matrix B

print("Resultant Matrix:")
def add(a, b, c, d):
if a == c and b == d: #condition
X = [] #resultant matrix
for i in range(b):
r = []
x = 0
for j in range(a):
x = A[i][j] + B[i][j]
r.append(x)
X.append(r)
for n in X:
print(*n)
else: print("Invalid")


def sub(a, b, c, d):
if a == c and b == d: #condition
X = [] #resultant matrix
for i in range(b):
r = []
x = 0
for j in range(a):
x = A[i][j] - B[i][j]
r.append(x)
X.append(r)
for n in X:
print(*n)
else: print("Invalid")


def mult(a, b, c, d):
X = [] #resultant matrix
if a == d: #condition to multiply two matrices
for k in range(b):
r = []
for l in range(c):
x = 0
for m in range(d):
x += A[k][m]*B[m][l]
r.append(x)
X.append(r)
for n in X:
print(*n)
else:print("Invalid")

if operation == "+":
add(C_A, R_A, C_B, R_B)

elif operation == "-":
sub(C_A, R_A, C_B, R_B)

elif operation == "*":
mult(C_A, R_A, C_B, R_B)







0 comments on commit 38d829b

Please sign in to comment.