-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay_16.py
More file actions
63 lines (53 loc) · 1.74 KB
/
Day_16.py
File metadata and controls
63 lines (53 loc) · 1.74 KB
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
54
55
56
57
58
59
60
61
62
63
"""
DAY 16 : Different Operations on Matrices.
https://www.geeksforgeeks.org/different-operation-matrices/
QUESTION : Perform Addititon, Subtraction and Multiplication on given Matrices.
"""
def add(matrix1, matrix2, n1, m1, n2, m2):
# n = no of rows
# m = no of columns
add = []
if n1 == n2 and m1 == m2:
for i in range(n1):
inner = []
for j in range(m1):
sum_ = matrix1[i][j] + matrix2[i][j]
inner.append(sum_)
add.append(inner)
print(f'Addition is : {add}')
else:
print(f'Addition not possible')
def sub(matrix1, matrix2, n1, m1, n2, m2):
# n = no of rows
# m = no of columns
sub = []
if n1 == n2 and m1 == m2:
for i in range(n1):
inner = []
for j in range(m1):
sum_ = matrix1[i][j] - matrix2[i][j]
inner.append(sum_)
sub.append(inner)
print(f'Subtraction is : {sub}')
else:
print(f'Subtraction not possible')
def multiply(matrix1, matrix2, n1, m1, n2, m2):
# n = no of rows
# m = no of columns
multiply = []
if m1 == n2:
for i in range(len(matrix1)):
inner = []
sum = 0
for j in range(len(matrix2[0])):
sum = 0
for k in range(len(matrix2)):
sum += matrix1[i][k] * matrix2[k][j]
inner.append(sum)
multiply.append(inner)
print(f'Multiplication is : {multiply}')
else:
print(f'Multiplication not possible')
matrix1 = [[1,2], [3,4], [5,6]]
matrix2 = [[5,6,7], [7,8,9]]
add(matrix1, matrix2, 2,3,3,2)