-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlaying_with_Matrices.py
59 lines (50 loc) · 2.12 KB
/
Playing_with_Matrices.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
54
55
56
57
58
59
import numpy as np
# import time
"""
This program works (with both the functions being active) very well for square matrices.
For non-square matrices, be careful to check the shape compatibility of the matricess involved.
NOTE: This is a brute-force implementation. There are more efficient algorithms for operating on matrices.
"""
def MatrixAddition(matrix_a, matrix_b):
m,n = matrix_a.shape
matrix_c = [[0 for i in range(m)] for j in range(n)]
for i in range(m):
for j in range(n):
matrix_c[i][j] += matrix_a[i][j] + matrix_b[i][j]
return np.array(matrix_c)
def MatrixMultiplication(matrix_a, matrix_b):
m,n = matrix_a.shape
n,p= matrix_b.shape
matrix_c = [[0 for i in range(m)] for j in range(n)]
for i in range(m):
for j in range(p):
for k in range(n):
matrix_c[i][j] += matrix_a[i][k]*matrix_b[k][j]
return np.array(matrix_c)
a1 = int(input("Enter how many rows in the first matrix: "))
b1 = int(input("Enter how many columns in the first matrix: "))
a2 = int(input("Enter how many rows in the second matrix: "))
b2 = int(input("Enter how many columns in the second matrix: "))
print("\n")
first_matrix = [[0 for i in range(a1)] for j in range(b1)]
for i in range(a1):
for j in range(b1):
first_matrix[i][j] += int(input("first_matrix["+str(i)+"]["+str(j)+"]: "))
first_matrix = np.array(first_matrix)
print("\n")
second_matrix = [[0 for i in range(a2)] for j in range(b2)]
for i in range(a2):
for j in range(b2):
second_matrix[i][j] += int(input("second_matrix["+str(i)+"]["+str(j)+"]: "))
second_matrix = np.array(second_matrix)
print("\n")
print("The first matrix given is: \n", first_matrix)
print("The second matrix given is: \n", second_matrix)
# resultant_sum_matrix = MatrixAddition(first_matrix, second_matrix)
# print("The sum of the given matrices is:")
# time.sleep(0.5)
# print(resultant_sum_matrix)
resultant_product_matrix= MatrixMultiplication(first_matrix, second_matrix)
print("The product of the given matrices is: ")
# time.sleep(0.5)
print(resultant_product_matrix)