Differences between revisions 14 and 15
Revision 14 as of 2025-09-25 15:49:50
Size: 4837
Comment: Associativity
Revision 15 as of 2026-01-06 22:27:21
Size: 4665
Comment: Cleanup
Deletions are marked like this. Additions are marked like this.
Line 15: Line 15:
To multiply '''a matrix by another matrix''' as '''''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'' rows by ''n'' columns, and '''''B''''' has shape ''n'' rows by ''p'' columns, so the product '''''C''''' will have ''m'' rows and ''p'' columns. 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''.
Line 33: Line 33:
To multiply '''a matrix by a vector''' as '''''A'''x = y'', the vector can be seen as a matrix with ''n'' rows and 1 column. The product will also have 1 column, i.e. be a vector. 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''''').
Line 35: Line 35:
To multiply '''a vector by a matrix''', the vector must be [[LinearAlgebra/Transposition|transposed]] so that it has ''n'' columns and 1 row. In other words, the multiplication is as ''x^T^'''A''' = y^T^''. Alternatively, the multiplication is as ''('''A'''^T^x)^T^ = y^T^''. Violations of these dimensional requirements mean the product DNE.
Line 41: Line 41:
== Properties == == Description ==
Line 43: Line 43:
Matrix multiplication is taking linear combinations of the rows of '''''A''''' according to the columns of '''''B''''', or vice versa. 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 45:
Matrix multiplication is not commutative.
Line 47: Line 46:
{{{
julia> A = [1 2; 0 0; 0 0]
3×2 Matrix{Int64}:
 1 2
 0 0
 0 0
Line 54: Line 47:
julia> B = [1 0 0; 2 0 0]
2×3 Matrix{Int64}:
 1 0 0
 2 0 0
=== Properties ===
Line 59: Line 49:
julia> A * B
3×3 Matrix{Int64}:
 5 0 0
 0 0 0
 0 0 0
Matrix multiplication is ''not'' commutative.
Line 65: Line 51:
julia> B * A
2×2 Matrix{Int64}:
 1 2
 2 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''')''.
Line 71: Line 53:
Matrix multiplication ''is'' however associative: ''('''AB''')'''X''' = '''A'''('''BX''')''. The above non-commutativity is necessary for this to hold. The interpretation should be that '''''X''''' is transformed linearly by '''''B''''' then '''''A'''''. Matrix multiplication has distinct left and right distributive properties: '''''A'''('''B'''+'''C''') = '''AB''' + '''AC''''' and ''('''B'''+'''C''')'''A''' = '''BA''' + '''CA''''' respectively.

Matrix Multiplication

Matrix multiplication is a fundamental operation.

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)