-
Notifications
You must be signed in to change notification settings - Fork 208
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #958 from shuvojitss/pr-2
Added Strassen's Matrix Multiplication in divide and conquer
- Loading branch information
Showing
2 changed files
with
52 additions
and
0 deletions.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
.../Design_and_Analysis_of_Algorithms/Divide_and_Conquer/Strassen's_Matrix_Multiplication.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# Python code to perform 2x2 matrix multiplication using Strassen's method | ||
import numpy as np | ||
|
||
def main(): | ||
x = np.zeros((2, 2), dtype=int) | ||
y = np.zeros((2, 2), dtype=int) | ||
z = np.zeros((2, 2), dtype=int) | ||
|
||
print("Enter the elements of the first matrix (2x2):") | ||
for i in range(2): | ||
for j in range(2): | ||
x[i][j] = int(input()) | ||
|
||
print("Enter the elements of the second matrix (2x2):") | ||
for i in range(2): | ||
for j in range(2): | ||
y[i][j] = int(input()) | ||
|
||
print("\nThe first matrix is:") | ||
for i in range(2): | ||
for j in range(2): | ||
print(f"{x[i][j]}\t", end="") | ||
print() | ||
|
||
print("\nThe second matrix is:") | ||
for i in range(2): | ||
for j in range(2): | ||
print(f"{y[i][j]}\t", end="") | ||
print() | ||
|
||
m1 = (x[0][0] + x[1][1]) * (y[0][0] + y[1][1]) | ||
m2 = (x[1][0] + x[1][1]) * y[0][0] | ||
m3 = x[0][0] * (y[0][1] - y[1][1]) | ||
m4 = x[1][1] * (y[1][0] - y[0][0]) | ||
m5 = (x[0][0] + x[0][1]) * y[1][1] | ||
m6 = (x[1][0] - x[0][0]) * (y[0][0] + y[0][1]) | ||
m7 = (x[0][1] - x[1][1]) * (y[1][0] + y[1][1]) | ||
|
||
z[0][0] = m1 + m4 - m5 + m7 | ||
z[0][1] = m3 + m5 | ||
z[1][0] = m2 + m4 | ||
z[1][1] = m1 - m2 + m3 + m6 | ||
|
||
print("\nResultant matrix:") | ||
for i in range(2): | ||
for j in range(2): | ||
print(f"{z[i][j]}\t", end="") | ||
print() | ||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters