-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultiplication.py
150 lines (110 loc) · 3.69 KB
/
multiplication.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import numpy as np
from utils import (
split_matrix_into_quadrants,
add_matrices,
substract_matrices,
timeit,
check_matrices_preconditions,
log2
)
import config
def traditional_algorithm(A: np.ndarray, B: np.ndarray) -> np.ndarray:
final_matrix = []
for row_idx_A in range(A.shape[0]):
final_row = []
for col_idx_B in range(B.shape[1]):
row_col_product = 0
col_elem_B = 0
for row_elem_A in range(A.shape[0]):
row_col_product += \
A[row_idx_A][row_elem_A]*B[col_elem_B][col_idx_B]
config.FLOATING_POINT_OPERATIONS += 1
col_elem_B = col_elem_B + 1
config.FLOATING_POINT_OPERATIONS += 1
final_row.append(row_col_product)
final_matrix.append(final_row)
return np.array(final_matrix)
def strassen_2D(A: np.ndarray, B: np.ndarray) -> np.ndarray:
P1 = (A[0][0] + A[1][1]) * (B[0][0] + B[1][1])
P2 = (A[1][0] + A[1][1]) * B[0][0]
P3 = A[0][0] * (B[0][1] - B[1][1])
P4 = A[1][1] * (B[1][0] - B[0][0])
P5 = (A[0][0] + A[0][1]) * B[1][1]
P6 = (A[1][0] - A[0][0]) * (B[0][0] + B[0][1])
P7 = (A[0][1] - A[1][1]) * (B[1][0] + B[1][1])
return np.array([
[P1+P4-P5+P7, P3+P5], [P2+P4, P1-P2+P3+P6]
])
def strassen_recursive_algorithm(A: np.ndarray, B: np.ndarray) -> np.ndarray:
config.FLOATING_POINT_OPERATIONS += 25
# Base Case
if A.shape == (2, 2):
return strassen_2D(A, B)
A_11, A_12, A_21, A_22 = split_matrix_into_quadrants(A)
B_11, B_12, B_21, B_22 = split_matrix_into_quadrants(B)
P1 = strassen_recursive_algorithm(
add_matrices(A_11, A_22),
add_matrices(B_11, B_22)
)
P2 = strassen_recursive_algorithm(
add_matrices(A_21, A_22), B_11
)
P3 = strassen_recursive_algorithm(
A_11, substract_matrices(B_12, B_22)
)
P4 = strassen_recursive_algorithm(
A_22, substract_matrices(B_21, B_11)
)
P5 = strassen_recursive_algorithm(
add_matrices(A_11, A_12), B_22
)
P6 = strassen_recursive_algorithm(
substract_matrices(A_21, A_11),
add_matrices(B_11, B_12)
)
P7 = strassen_recursive_algorithm(
substract_matrices(A_12, A_22),
add_matrices(B_21, B_22)
)
# Top Left Quadrant
C_11 = add_matrices(
P1, add_matrices(substract_matrices(P4, P5), P7))
# Top Right Quadrant
C_12 = add_matrices(P3, P5)
# Bottom Left Quadrant
C_21 = add_matrices(P2, P4)
# Bottom Right Quadrant
C_22 = add_matrices(
substract_matrices(P1, P2),
add_matrices(P3, P6)
)
final_matrix = []
for idx in range(len(C_12)):
# Construct the top of the final matrix
# Top = Top Left Quadrant + Top Right Quadrant
final_matrix.append(list(C_11[idx]) + list(C_12[idx]))
for idx in range(len(C_22)):
# Construct the bottom of the final matrix
# Bottom = Bottom Left Quadrant + Bottom Right Quadrant
final_matrix.append(list(C_21[idx]) + list(C_22[idx]))
return np.array(final_matrix)
def multiply_matrices(
A: np.ndarray,
B: np.ndarray,
l: int # noqa: E741
) -> np.array:
if len(A) == 1 and len(B) == 1:
return np.array([list(A)[0] * list(B)[0]])
check_matrices_preconditions(A, B)
matrix_order = A.shape[0]
k = log2(matrix_order)
if k <= l:
return traditional_algorithm(A, B)
elif k > l:
return strassen_recursive_algorithm(A, B)
@timeit
def call_matrix_mutliplication_interface(
A: np.ndarray, B: np.ndarray,
l: int # noqa: E741
) -> np.array:
return multiply_matrices(A, B, l)