-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsum&product.py
53 lines (42 loc) · 1.69 KB
/
sum&product.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# Python function to calculate the sum and product of two compatible matrices
def get_matrix(rows, cols):
mat = []
for i in range(rows):
row = input(f"Enter the elements of row {i+1} separated by space: ")
row = row.split(" ")
if len(row) != cols:
print("Error: Invalid number of elements in row.")
return None
row = [int(x) for x in row]
mat.append(row)
return mat
def matSumProt():
rows1 = int(input("Enter the number of rows in the first matrix: "))
cols1 = int(input("Enter the number of columns in the first matrix: "))
rows2 = int(input("Enter the number of rows in the second matrix: "))
cols2 = int(input("Enter the number of columns in the second matrix: "))
if cols1 != rows2:
print("Error: The matrices are not compatible for addition and multiplication.")
return None
print("Enter the elements of the first matrix:")
mat1 = get_matrix(rows1, cols1)
print("Enter the elements of the second matrix:")
mat2 = get_matrix(rows2, cols2)
if mat1 is None or mat2 is None:
return None
sum_mat = [[0 for j in range(cols1)] for i in range(rows1)]
prod_mat = [[0 for j in range(cols2)] for i in range(rows1)]
for i in range(rows1):
for j in range(cols1):
sum_mat[i][j] = mat1[i][j] + mat2[i][j]
for k in range(cols2):
prod_mat[i][k] += mat1[i][j] * mat2[j][k]
return (sum_mat, prod_mat)
result = matSumProt()
if result is not None:
print("The sum of the matrices is:")
for row in result[0]:
print(row)
print("The product of the matrices is:")
for row in result[1]:
print(row)