-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrassen.py
More file actions
57 lines (46 loc) · 1.5 KB
/
Copy pathstrassen.py
File metadata and controls
57 lines (46 loc) · 1.5 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
import numpy as np
def strassen_matrix_multiplication(A, B):
"""
Strassen's Matrix Multiplication Algorithm
A and B must be square matrices of size 2^n x 2^n
"""
n = len(A)
# Base case
if n == 1:
return A * B
# Split matrices into quadrants
mid = n // 2
A11 = A[:mid, :mid]
A12 = A[:mid, mid:]
A21 = A[mid:, :mid]
A22 = A[mid:, mid:]
B11 = B[:mid, :mid]
B12 = B[:mid, mid:]
B21 = B[mid:, :mid]
B22 = B[mid:, mid:]
# Calculate the 7 products (Strassen’s formulas)
M1 = strassen_matrix_multiplication(A11 + A22, B11 + B22)
M2 = strassen_matrix_multiplication(A21 + A22, B11)
M3 = strassen_matrix_multiplication(A11, B12 - B22)
M4 = strassen_matrix_multiplication(A22, B21 - B11)
M5 = strassen_matrix_multiplication(A11 + A12, B22)
M6 = strassen_matrix_multiplication(A21 - A11, B11 + B12)
M7 = strassen_matrix_multiplication(A12 - A22, B21 + B22)
# Combine results into final matrix
C11 = M1 + M4 - M5 + M7
C12 = M3 + M5
C21 = M2 + M4
C22 = M1 - M2 + M3 + M6
# Join the quadrants into a single matrix
top = np.hstack((C11, C12))
bottom = np.hstack((C21, C22))
C = np.vstack((top, bottom))
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 = strassen_matrix_multiplication(A, B)
print("Result of Strassen Matrix Multiplication:\n", C)