-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrix_multiplication.py
More file actions
36 lines (28 loc) · 881 Bytes
/
Copy pathmatrix_multiplication.py
File metadata and controls
36 lines (28 loc) · 881 Bytes
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
import numpy as np
def classic_matrix_multiplication(A, B):
"""
Normal matrix multiplication using triple loop
A: m x n matrix
B: n x p matrix
Returns: m x p result matrix
"""
m, n = A.shape
nB, p = B.shape
if n != nB:
raise ValueError("Number of columns of A must match number of rows of B")
# Create a result matrix filled with zeros
C = np.zeros((m, p), dtype=int)
# Triple nested loop for multiplication
for i in range(m):
for j in range(p):
for k in range(n):
C[i][j] += A[i][k] * B[k][j]
return C
# Example usage
if __name__ == "__main__":
A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])
print("Matrix A:\n", A)
print("Matrix B:\n", B)
C = classic_matrix_multiplication(A, B)
print("Result of Normal Matrix Multiplication:\n", C)