I sometimes have trouble remembering mathematical matrix syntax and the order of matrix multiplication, and I'm sure others do too, so here's a quick summary.
Matrix size and indexing is written row-first: an m-by-n matrix has m rows and n columns. (This is the exact opposite of the x,y notation in computer graphics, where the column x is specified first.) A vector of k elements is represented either as a 1-by-k row matrix or a k-by-1 column matrix.
An example 2-by-3 matrix:
−3 | 4 | −2 |
−8 | 7 | 4 |
Matrix multiplication multiplies an m-by-n matrix with an n-by-p matrix, producing an m-by-p matrix: the second matrix ("matrix B") must have as many rows as the first matrix ("matrix A") has columns, and the result has the same number of rows that matrix A has and the number of columns that B has. (AB has A's rows and B's columns.)
Recall the dot product (or scalar product), that takes in two equal-length vectors and returns one number. If the two vectors (of length k) are v and w, then to get their dot product, we multiply corresponding entries of v and w and sum the resulting products together. Mathematically:
Dot product example:
v = | 1 | 8 | −2 | 5 | −2 | ||||||
w = | 1 | 6 | 2 | −8 | −5 | ||||||
Product: | 1 | + | 48 | + | −4 | + | −40 | + | 10 | = | 15 |
To multiply two matrixes (A and B), we take single rows of A and single columns of B, then compute their dot product and put that in the intersection. Cell i,j of the product matrix AB is calculated with row i of A and column j of B.
A couple of examples, including an illustration of when a column vector is multiplied by a row vector and not vice-versa:
b11 | b12 | ||
b21 | b22 | ||
a11 | a12 | a11b11 + a12b21 | a11b12 + a12b22 |
a21 | a22 | a21b11 + a22b21 | a21b12 + a22b22 |
w1 | |||
w2 | |||
w3 | |||
v1 | v2 | v3 | v1w1 + v2w2 + v3w3 |
w1 | w2 | w3 | |
v1 | v1w1 | v1w2 | v1w3 |
v2 | v2w1 | v2w2 | v2w3 |
v3 | v3w1 | v3w2 | v3w3 |
In linear algebra, a linear function (or linear map) of n variables is often expressed as Ax, with x being a column vector of n variables and A being an n×n matrix. How this works is quite clear when the multiplication is carried out, in this example for n=3:
x1 | |||
x2 | |||
x3 | |||
3 | 1 | 0 | 3x1 + 1x2 + 0 |
0 | -2 | 5 | 0 − 2x2 + 5x |
0 | 0 | 6 | 0 + 0 + 6x3 |
Geometric rotations are also done with matrix multiplication. For 2d points, rotated around the origin, the following matrix is used (with θ being the rotation angle); it is then multiplied with the column vector of an (x, y) point. The result is a new column vector, with the newly rotated x and y. This is so common that GPUs have in-built hardware for this; it's what they're made for.
cos θ | −sin θ |
sin θ | cos θ |
2D rotation only has one axis and one angle, but for 3D rotation there are three axes that a point can be rotated around.
1 | 0 | 0 |
0 | cos θ | −sin θ |
0 | sin θ | cos θ |
cos θ | 0 | sin θ |
0 | 1 | 0 |
−sin θ | 0 | cos θ |
cos θ | −sin θ | 0 |
sin θ | cos θ | 0 |
0 | 0 | 1 |
created 2022-11-18, edited 2022-11-18. index.