Differences between revisions 6 and 16 (spanning 10 versions)
Revision 6 as of 2023-10-30 18:01:59
Size: 3078
Comment: Made notation consistent
Revision 16 as of 2026-01-21 16:22:47
Size: 4728
Comment: Link
Deletions are marked like this. Additions are marked like this.
Line 2: Line 2:

'''Matrix multiplication''' is a fundamental operation corresponding to [[LinearAlgebra/LinearMapping|homomorphisms]].

See also [[Calculus/VectorOperations|vector operations]].
Line 9: Line 13:
== Introduction == == Dimensions ==
Line 11: Line 15:
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''. To multiply two matrices (i.e., '''''AB''' = '''C'''''), they must have a common dimension. '''''A''''' must be as wide as '''''B''''' is tall, and the product will be as wide as '''''B''''' and as tall as '''''A'''''. Alternatively: '''''A''''' has shape ''m x n'' (rows by columns), and '''''B''''' has shape ''n x p'', so the product '''''C''''' will have shape ''m x p''.

This can be visualized as:
Line 14: Line 20:
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 0
                              ┌ ┐
                              │ 1 . │
                              │ 2 . │
                              │ 3 . │
                              │ 4 . │
       ┌ ┐ <=> └ ┘
       │ B │ ┌ ┐ ┌ ┐
       └ ┘ │ 1 2 3 4 │ │ 30 . │
┌ ┐ ┌ ┐ │ . . . . │ │ . . │
│ A │ │ C │ │ . . . . │ │ . . │
└ ┘ └ ┘ └ ┘ └ ┘
Line 35: Line 33:
A cell in a matrix is expressed as ''c,,ij,,'' where ''i'' is a row index and ''j'' is a column index. Vectors are columns by convention, so a matrix can only be multiplied by a vector on the right (i.e., '''''A'''x = y''). The only workaround is to denote the [[LinearAlgebra/Transposition|transposed]] vector (i.e., ''x^T^'''A''''').

Violations of these dimensional requirements mean the product DNE.
Line 41: Line 41:
== Computation == == Description ==

The product of two matrices (i.e., '''''AB''' = '''C''''') is a linear combination of the rows of '''''A''''' according to the columns of '''''B''''', or a linear combination of the columns of '''''B''''' according to the rows of '''''A'''''.
Line 45: Line 47:
=== Cell-wise === === Properties ===
Line 47: Line 49:
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,,'''''. Matrix multiplication is ''not'' commutative.
Line 49: Line 51:
{{{
julia> [1 2; 3 4] * [1 0; 0 1]
2×2 Matrix{Int64}:
 1 2
 3 4
}}}
Matrix multiplication is associative: ''('''AB''')'''X''' = '''A'''('''BX''')''. Furthermore, scalar multiplication is associative within matrix multiplication: ''r('''AB''') = (r'''A''')'''B''' = '''A'''(r'''B''')''.

Matrix multiplication has distinct left and right distributive properties: '''''A'''('''B'''+'''C''') = '''AB''' + '''AC''''' and ''('''B'''+'''C''')'''A''' = '''BA''' + '''CA''''' respectively.

----
Line 58: Line 59:
=== Row-wise === == Cell-wise Computation ==
Line 60: Line 61:
Row ''i'' of '''''C''''' is a linear combination of the columns of '''''B'''''. A cell in a matrix is expressed as '''''A''',,ij,,'' where ''i'' is a row index and ''j'' is a column index. Indexing starts at 1.
Line 62: Line 63:
Solving for each row as: For '''''C''' = '''AB''''': '''''C''',,ij,,'' can be computed as the [[Calculus/VectorOperations#Dot_Product|dot product]] of row '''''A''',,i,,'' and column '''''B''',,j,,''.

Referencing the complete solution above:
Line 65: Line 68:
row 1 = 1(column 1 of B) + 2(column 2 of B)
      = 1[1 0] + 2[0 1]
      = [1 0] + [0 2]
      = [1 2]
julia> A[1, :]
2-element Vector{Int64}:
 1
 2
Line 70: Line 73:
row 2 = 3(column 1 of B) 4(column 2 of B)
      = 3[1 0] + 4[0 1]
      = [3 0] + [0 4]
      = [3 4]
julia> B[:, 1]
2-element Vector{Int64}:
 1
 2

julia> using LinearAlgebra

julia> dot(A[1, :], B[:, 1])
5
Line 75: Line 83:

----
Line 78: Line 88:
=== Column-wise === == Column-wise Computation ==
Line 80: Line 90:
Column ''j'' of '''''C''''' is a linear combination of the rows of '''''A'''''. Column '''''C''',,j,,'' is a linear combination of all columns in '''''A''''' taken according to the column '''''B''',,j,,''.
Line 82: Line 92:
Solving for each column as: Referencing the complete solution above and recall that '''''B''',,1,, = [1 2]'':
Line 85: Line 95:
column 1 = 1(row 1 of A) + 0(row 2 of A)
         = 1[1 2] + 0
         = [1 2]
C = 1*A + 2*A
 1 1 2
Line 89: Line 98:
column 2 = 0(row 1 of A) + 1(row 2 of A)
         = 0 + 1[3 4]
         = [3 4]
C = 1*[1 0 0] + 2*[2 0 0]
 1

C = [1 0 0] + [4 0 0]
 1

C = [5 0 0]
 1
Line 93: Line 107:

----
Line 96: Line 112:
=== Summation === == Row-wise Computation ==
Line 98: Line 114:
'''''C''''' can be evaluated as a summation of the columns of '''''A''''' by the rows of '''''B'''''. Row '''''C''',,i,,'' is a linear combination of all rows in '''''B''''' taken according to the row '''''A''',,i,,''.

Referencing the complete solution above and recall that '''''A''',,1,, = [1 2]'':
Line 101: Line 119:
julia> [1; 2] * [1 0]
2×2 Matrix{Int64}:
 1 0
 2 0
C = 1*B + 2*B
 1 1 2
Line 106: Line 122:
julia> [3; 4] * [0 1]
2×2 Matrix{Int64}:
 0 3
 0 4
C = 1*[1 0 0] + 2*[2 0 0]
 1
Line 111: Line 125:
julia> [1; 2] * [1 0] + [3; 4] * [0 1]
2×2 Matrix{Int64}:
 1 3
 2 4
C = [1 0 0] + [4 0 0]
 1

C = [5 0 0]
 1
Line 116: Line 131:

----
Line 119: Line 136:
=== Block-wise === == Block-wise Computation ==
Line 121: Line 138:
'''''C''''' can be evaluated block-wise. Suppose '''''A''''' and '''''B''''' are 20x20 matrices; they can be divided each into 10x10 quadrants. Matrix multiplication can be evaluated in blocks. Suppose '''''A''''' and '''''B''''' are 20x20 matrices; they can be divided each into 10x10 quadrants.
Line 155: Line 172:
The entire product could be computed, as in: The entire product could be computed:
Line 166: Line 183:
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^'''''. But if a specific block of the product is of interest, it can be solved like '''''C'''^1^ = '''A'''^1^'''B'''^1^ + '''A'''^2^'''B'''^3^''.

Matrix Multiplication

Matrix multiplication is a fundamental operation corresponding to homomorphisms.

See also vector operations.


Dimensions

To multiply two matrices (i.e., AB = C), they must have a common dimension. A must be as wide as B is tall, and the product will be as wide as B and as tall as A. Alternatively: A has shape m x n (rows by columns), and B has shape n x p, so the product C will have shape m x p.

This can be visualized as:

                              ┌      ┐
                              │  1 . │
                              │  2 . │
                              │  3 . │
                              │  4 . │
       ┌   ┐ <=>              └      ┘
       │ B │     ┌         ┐  ┌      ┐
       └   ┘     │ 1 2 3 4 │  │ 30 . │
┌   ┐  ┌   ┐     │ . . . . │  │  . . │
│ A │  │ C │     │ . . . . │  │  . . │
└   ┘  └   ┘     └         ┘  └      ┘

Vectors are columns by convention, so a matrix can only be multiplied by a vector on the right (i.e., Ax = y). The only workaround is to denote the transposed vector (i.e., xTA).

Violations of these dimensional requirements mean the product DNE.


Description

The product of two matrices (i.e., AB = C) is a linear combination of the rows of A according to the columns of B, or a linear combination of the columns of B according to the rows of A.

Properties

Matrix multiplication is not commutative.

Matrix multiplication is associative: (AB)X = A(BX). Furthermore, scalar multiplication is associative within matrix multiplication: r(AB) = (rA)B = A(rB).

Matrix multiplication has distinct left and right distributive properties: A(B+C) = AB + AC and (B+C)A = BA + CA respectively.


Cell-wise Computation

A cell in a matrix is expressed as Aij where i is a row index and j is a column index. Indexing starts at 1.

For C = AB: Cij can be computed as the dot product of row Ai and column Bj.

Referencing the complete solution above:

julia> A[1, :]
2-element Vector{Int64}:
 1
 2

julia> B[:, 1]
2-element Vector{Int64}:
 1
 2

julia> using LinearAlgebra

julia> dot(A[1, :], B[:, 1])
5


Column-wise Computation

Column Cj is a linear combination of all columns in A taken according to the column Bj.

Referencing the complete solution above and recall that B1 = [1 2]:

C  = 1*A  + 2*A
 1      1      2

C  = 1*[1 0 0] + 2*[2 0 0]
 1

C  = [1 0 0] + [4 0 0]
 1

C  = [5 0 0]
 1


Row-wise Computation

Row Ci is a linear combination of all rows in B taken according to the row Ai.

Referencing the complete solution above and recall that A1 = [1 2]:

C  = 1*B  + 2*B
 1      1      2

C  = 1*[1 0 0] + 2*[2 0 0]
 1

C  = [1 0 0] + [4 0 0]
 1

C  = [5 0 0]
 1


Block-wise Computation

Matrix multiplication can be evaluated in blocks. 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  1

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:

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 like C1 = A1B1 + A2B3.

julia> A1 * B1 + A2 * B3
2×2 Matrix{Int64}:
 2  4
 6  8


CategoryRicottone

LinearAlgebra/MatrixMultiplication (last edited 2026-01-21 16:22:47 by DominicRicottone)