|
Size: 3406
Comment:
|
Size: 3078
Comment: Made notation consistent
|
| Deletions are marked like this. | Additions are marked like this. |
| Line 3: | Line 3: |
| == Introduction == Matrices are multiplied non-commutatively. The ''m'' rows of matrix A are multiplied by the ''p'' rows of matrix B. Therefore, note that A must be as tall as B is wide. {{{ ┌ ┐┌ ┐ ┌ ┐ │ 0 0││ 0 0 0│ │ 0 0 0│ │ 0 0││ 0 0 0│ = │ 0 0 0│ │ 0 0│└ ┘ │ 0 0 0│ └ ┘ └ ┘ A x B = C mxn x nxp = mxp }}} A cell in a matrix is expressed as C,,ij,, where `i` is a row index and `j` is a column index. |
<<TableOfContents>> |
| Line 27: | Line 9: |
| == Multiplication == | == Introduction == |
| Line 29: | Line 11: |
| === Cell-wise === In a multiplication of matrices A and B, cell C,,ij,, is solved as (row `i` of A)(column `j` of B). Consider the following: |
Matrices are multiplied '''non-commutatively'''. The ''m'' rows of '''''A''''' are multiplied by the ''p'' rows of '''''B'''''. '''''A''''' must be as ''tall'' as '''''B''''' is ''wide''. |
| Line 36: | Line 14: |
| ┌ ┐┌ ┐ ┌ ┐ │ 1 2││ 1 0│ │ 1 2│ │ 3 4││ 0 1│ = │ 3 4│ └ ┘└ ┘ └ ┘ |
julia> A = [1 2 0 0 0 0] 3×2 Matrix{Int64}: 1 2 0 0 0 0 |
| Line 41: | Line 22: |
| cell (1,1) = (row 1 of A)(column 1 of B) = [1 2][1 0] = (1 * 1) + (2 * 0) = 1 |
julia> B = [1 0 0 2 0 0] 2×3 Matrix{Int64}: 1 0 0 2 0 0 |
| Line 46: | Line 28: |
| cell (1,2) = (row 1 of A)(column 2 of B) = [1 2][0 1] = (1 * 0) + (2 * 1) = 2 |
julia> A * B 3×3 Matrix{Int64}: 5 0 0 0 0 0 0 0 0 }}} |
| Line 51: | Line 35: |
| cell (2,1) = [3 4][1 0] = 3 cell (2,2) = [3 4][0 1] = 4 }}} |
A cell in a matrix is expressed as ''c,,ij,,'' where ''i'' is a row index and ''j'' is a column index. |
| Line 62: | Line 41: |
| == Computation == === Cell-wise === In a multiplication of matrices '''''A''''' and '''''B''''', cell ''c,,ij,,'' of the new matrix '''''C''''' is the [[LinearAlgebra/VectorMultiplication#Dot_Product|dot product]] of row '''''A,,i,,''''' and column '''''B,,j,,'''''. {{{ julia> [1 2; 3 4] * [1 0; 0 1] 2×2 Matrix{Int64}: 1 2 3 4 }}} |
|
| Line 64: | Line 60: |
| In a multiplication of matrices A and B, row `i` of C is a linear combination of the columns of B. | Row ''i'' of '''''C''''' is a linear combination of the columns of '''''B'''''. |
| Line 66: | Line 62: |
| Consider the following: | Solving for each row as: |
| Line 69: | Line 65: |
| ┌ ┐┌ ┐ ┌ ┐ │ 1 2││ 1 0│ │ 1 2│ │ 3 4││ 0 1│ = │ 3 4│ └ ┘└ ┘ └ ┘ |
|
| Line 85: | Line 76: |
| ---- |
|
| Line 91: | Line 80: |
| Column ''j'' of '''''C''''' is a linear combination of the rows of '''''A'''''. | |
| Line 92: | Line 82: |
| In a multiplication of matrices A and B, column `j` of C is a linear combination of the rows of A. Consider the following: |
Solving for each column as: |
| Line 97: | Line 85: |
| ┌ ┐┌ ┐ ┌ ┐ │ 1 2││ 1 0│ │ 1 2│ │ 3 4││ 0 1│ = │ 3 4│ └ ┘└ ┘ └ ┘ |
|
| Line 111: | Line 94: |
| ---- |
|
| Line 117: | Line 98: |
| In a multiplication of matrices A and B, C can be evaluated as a summation of the columns of A by the rows of B. | '''''C''''' can be evaluated as a summation of the columns of '''''A''''' by the rows of '''''B'''''. |
| Line 120: | Line 101: |
| ┌ ┐┌ ┐ ┌ ┐ │ 1 2││ 1 0│ │ 1 2│ │ 3 4││ 0 1│ = │ 3 4│ └ ┘└ ┘ └ ┘ |
julia> [1; 2] * [1 0] 2×2 Matrix{Int64}: 1 0 2 0 |
| Line 125: | Line 106: |
| ┌ ┐┌ ┐ ┌ ┐┌ ┐ ┌ ┐ │ 1││ 1 0│ │ 2│| 0 1| │ 1 2│ │ 3│└ ┘ + │ 4│└ ┘ = │ 3 4│ └ ┘ └ ┘ └ ┘ |
julia> [3; 4] * [0 1] 2×2 Matrix{Int64}: 0 3 0 4 |
| Line 130: | Line 111: |
| ┌ ┐ ┌ ┐ ┌ ┐ | 1 0| | 0 2| │ 1 2│ | 3 0| + | 0 4| = │ 3 4│ └ ┘ └ ┘ └ ┘ |
julia> [1; 2] * [1 0] + [3; 4] * [0 1] 2×2 Matrix{Int64}: 1 3 2 4 |
| Line 135: | Line 116: |
---- |
|
| Line 142: | Line 121: |
| In a multiplication of matrices A and B, C can be evaluated block-wise. Suppose A and B are 20x20 matrices; they can be divided each into 10x10 quadrants. | '''''C''''' can be evaluated block-wise. Suppose '''''A''''' and '''''B''''' are 20x20 matrices; they can be divided each into 10x10 quadrants. Using these matrices '''''A''''' and '''''B''''' as the building blocks: |
| Line 145: | Line 126: |
| ┌ ┐┌ ┐ ┌ ┐ │ A1 A2││ B1 B2│ │ C1 C2│ │ A3 A4││ B3 B4│ = │ C3 C4│ └ ┘└ ┘ └ ┘ |
julia> A = [1 2; 3 4] 2×2 Matrix{Int64}: 1 2 3 4 julia> B = [1 0; 0 1] 2×2 Matrix{Int64}: 1 0 0 1 |
| Line 151: | Line 137: |
| C^1^ = A^1^B^1^ + A^2^B^3^ | Much larger matrices may be composed of these building blocks. {{{ julia> A_ = [A1 A2; A3 A4] 4×4 Matrix{Int64}: 1 2 1 2 3 4 3 4 1 2 1 2 3 4 3 4 julia> B_ = [B1 B2; B3 B4] 4×4 Matrix{Int64}: 1 0 1 0 0 1 0 1 1 0 1 0 0 1 0 1 }}} The entire product could be computed, as in: {{{ julia> A_ * B_ 4×4 Matrix{Int64}: 2 4 2 4 6 8 6 8 2 4 2 4 6 8 6 8 }}} But if a specific block of the product is of interest, it can be solved as '''''C^1^''''' = '''''A^1^B^1^''''' + '''''A^2^B^3^'''''. {{{ julia> A1 * B1 + A2 * B3 2×2 Matrix{Int64}: 2 4 6 8 }}} |
Matrix Multiplication
Contents
Introduction
Matrices are multiplied non-commutatively. The m rows of A are multiplied by the p rows of B. A must be as tall as B is wide.
julia> A = [1 2
0 0
0 0]
3×2 Matrix{Int64}:
1 2
0 0
0 0
julia> B = [1 0 0
2 0 0]
2×3 Matrix{Int64}:
1 0 0
2 0 0
julia> A * B
3×3 Matrix{Int64}:
5 0 0
0 0 0
0 0 0A cell in a matrix is expressed as cij where i is a row index and j is a column index.
Computation
Cell-wise
In a multiplication of matrices A and B, cell cij of the new matrix C is the dot product of row Ai and column Bj.
julia> [1 2; 3 4] * [1 0; 0 1]
2×2 Matrix{Int64}:
1 2
3 4
Row-wise
Row i of C is a linear combination of the columns of B.
Solving for each row as:
row 1 = 1(column 1 of B) + 2(column 2 of B)
= 1[1 0] + 2[0 1]
= [1 0] + [0 2]
= [1 2]
row 2 = 3(column 1 of B) 4(column 2 of B)
= 3[1 0] + 4[0 1]
= [3 0] + [0 4]
= [3 4]
Column-wise
Column j of C is a linear combination of the rows of A.
Solving for each column as:
column 1 = 1(row 1 of A) + 0(row 2 of A)
= 1[1 2] + 0
= [1 2]
column 2 = 0(row 1 of A) + 1(row 2 of A)
= 0 + 1[3 4]
= [3 4]
Summation
C can be evaluated as a summation of the columns of A by the rows of B.
julia> [1; 2] * [1 0]
2×2 Matrix{Int64}:
1 0
2 0
julia> [3; 4] * [0 1]
2×2 Matrix{Int64}:
0 3
0 4
julia> [1; 2] * [1 0] + [3; 4] * [0 1]
2×2 Matrix{Int64}:
1 3
2 4
Block-wise
C can be evaluated block-wise. Suppose A and B are 20x20 matrices; they can be divided each into 10x10 quadrants.
Using these matrices A and B as the building blocks:
julia> A = [1 2; 3 4]
2×2 Matrix{Int64}:
1 2
3 4
julia> B = [1 0; 0 1]
2×2 Matrix{Int64}:
1 0
0 1Much larger matrices may be composed of these building blocks.
julia> A_ = [A1 A2; A3 A4]
4×4 Matrix{Int64}:
1 2 1 2
3 4 3 4
1 2 1 2
3 4 3 4
julia> B_ = [B1 B2; B3 B4]
4×4 Matrix{Int64}:
1 0 1 0
0 1 0 1
1 0 1 0
0 1 0 1The entire product could be computed, as in:
julia> A_ * B_
4×4 Matrix{Int64}:
2 4 2 4
6 8 6 8
2 4 2 4
6 8 6 8But if a specific block of the product is of interest, it can be solved as C1 = A1B1 + A2B3.
julia> A1 * B1 + A2 * B3
2×2 Matrix{Int64}:
2 4
6 8