You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Strassen Matrix Multiplication/README.markdown
+75-67Lines changed: 75 additions & 67 deletions
Original file line number
Diff line number
Diff line change
@@ -11,17 +11,16 @@ Before we begin, you may ask what is matrix multiplication? Great question! It i
11
11
12
12
### Example: Matrix Multiplication
13
13
14
-
matrix A = |1 2|
15
-
|3 4|
16
-
17
-
matrix B = |5 6|
18
-
|7 8|
19
-
20
-
A * B = C
14
+
```
15
+
matrix A = |1 2|, matrix B = |5 6|
16
+
|3 4| |7 8|
21
17
22
-
|1 2| * |5 6| = |1*5+2*7 1*6+2*8| = |19 22|
23
-
|3 4| |7 8| |3*5+4*7 3*6+4*8| |43 48|
18
+
A * B = C
24
19
20
+
|1 2| * |5 6| = |1*5+2*7 1*6+2*8| = |19 22|
21
+
|3 4| |7 8| |3*5+4*7 3*6+4*8| |43 48|
22
+
```
23
+
25
24
What's going on here? To start, we're multiplying matricies A & B. Our new matrix, C's, elements `[i, j]` are determined by the dot product of the first matrix's ith row and the second matrix's jth column. See [here](https://www.khanacademy.org/math/linear-algebra/vectors-and-spaces/dot-cross-products/v/vector-dot-product-and-vector-length) for a refresher on the dot product.
26
25
27
26
So the upper left element `[i=1, j=1]` of our new matrix is a combination of A's 1st row and B's 1st column.
@@ -66,14 +65,14 @@ Next, we loop over A's columns and B's rows. Because we know both A's columns &
66
65
67
66
```swift
68
67
for i in0..<n {
69
-
for j in0..<n {
68
+
for j in0..<n {
70
69
```
71
70
72
71
Then, foreach row in A and column in B, we take the dot product of the ith row in A with the jth column in B and set that result equal to the `[i, j]` element in C. Or `C[i, j]`.
73
72
74
73
```swift
75
74
for k in0..<n {
76
-
C[i, j] = A[i, k] * B[k, j]
75
+
C[i, j] = A[i, k] * B[k, j]
77
76
}
78
77
```
79
78
@@ -83,20 +82,20 @@ Here's the full implementation:
assert(A.columns== B.rows, "Two matricies can only be matrix mulitiplied if one has dimensions mxn & the other has dimensions nxp where m, n, p are in R")
88
-
let n = A.columns
89
-
var C = Matrix<T>(rows: A.rows, columns: B.columns)
85
+
let A =self
86
+
assert(A.columns== B.rows, "Two matricies can only be matrix mulitiplied if one has dimensions mxn & the other has dimensions nxp where m, n, p are in R")
87
+
let n = A.columns
88
+
var C = Matrix<T>(rows: A.rows, columns: B.columns)
90
89
91
-
for i in0..<n {
92
-
for j in0..<n {
93
-
for k in0..<n {
94
-
C[i, j] = A[i, k] * B[k, j]
95
-
}
96
-
}
97
-
}
90
+
for i in0..<n {
91
+
for j in0..<n {
92
+
for k in0..<n {
93
+
C[i, j] = A[i, k] * B[k, j]
94
+
}
95
+
}
96
+
}
98
97
99
-
return C
98
+
return C
100
99
}
101
100
```
102
101
@@ -110,10 +109,10 @@ Volker Strassen first published his algorithm in 1969. It was the first algorith
110
109
111
110
The basic idea behind Strassen's algorithm is to split A & B into 8 submatricies and then recursively compute the submatricies of C. This strategy is called *Divide and Conquer*.
112
111
113
-
matrix A =|a b|
114
-
|c d|
115
-
matrix B =|e f|
116
-
|g h|
112
+
```
113
+
matrix A =|a b|, matrix B =|e f|
114
+
|c d||g h|
115
+
```
117
116
118
117
*There will be 8 recursive calls:*
119
118
@@ -149,41 +148,50 @@ Strassen's **7** calls are as follows:
149
148
150
149
Now we can compute our new matrix C's new quardents!
151
150
152
-
matrix C =|p5+p4-p2+p6 p1+p2 |
153
-
| p3+p4 p1+p5-p3-p7|
154
-
151
+
```
152
+
matrix C =|p5+p4-p2+p6 p1+p2 |
153
+
| p3+p4 p1+p5-p3-p7|
154
+
```
155
155
156
156
A great reaction right now would be !!??!?!?!!?! How does this even work??
0 commit comments