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:
$$v\cdot w=\sum _{i=1}^{k}{v}_{i}{w}_{i}$$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:
b_{11} | b_{12} | ||
b_{21} | b_{22} | ||
a_{11} | a_{12} | a_{11}b_{11} + a_{12}b_{21} | a_{11}b_{12} + a_{12}b_{22} |
a_{21} | a_{22} | a_{21}b_{11} + a_{22}b_{21} | a_{21}b_{12} + a_{22}b_{22} |
w_{1} | |||
w_{2} | |||
w_{3} | |||
v_{1} | v_{2} | v_{3} | v_{1}w_{1} + v_{2}w_{2} + v_{3}w_{3} |
w_{1} | w_{2} | w_{3} | |
v_{1} | v_{1}w_{1} | v_{1}w_{2} | v_{1}w_{3} |
v_{2} | v_{2}w_{1} | v_{2}w_{2} | v_{2}w_{3} |
v_{3} | v_{3}w_{1} | v_{3}w_{2} | v_{3}w_{3} |
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:
x_{1} | |||
x_{2} | |||
x_{3} | |||
3 | 1 | 0 | 3x_{1} + 1x_{2} + 0 |
0 | -2 | 5 | 0 − 2x_{2} + 5x_{} |
0 | 0 | 6 | 0 + 0 + 6x_{3} |
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.