A mathematical vector (or more precisely, a vector over the field of the real numbers) is a list of numbers with a set of operations. We use mathematical vectors in computer graphics to represent five things:
unit vectors(or
normalsif the direction is defined by a surface)
When dealing with a vector, it is common to call numbers
scalars
.
Let be a scalar, and be two vectors, and be the angle between and .
Operation | Result | Notes |
---|---|---|
Vector addition, element-wise addition | ||
Vector subtraction or difference, element-wise subtraction | ||
Scaling, multiplication by a scalar | ||
Scaling, division by a scalar | ||
Dot product, inner product; also written ; is a scalar equal to | ||
Magnitude, length, norm |
Depending on what vectors represent, their operations may represent specific things:
normalizing the vector
Two vectors are said to be orthogonal or perpendicular if their dot product is .
A vector is called a unit vector if its magnitude is .
Two-dimensional vectors have just two components . They have two special operations:
Three-dimension vectors have three components . They have one special operation:
const add = (x,y) => x.map((e,i)=>e+y[i])
const sub = (x,y) => x.map((e,i)=>e-y[i])
const mul = (x,s) => x.map(e=>e*s)
const div = (x,s) => x.map(e=>e/s)
const dot = (x,y) => x.map((e,i)=>e*y[i]).reduce((s,t)=>s+t)
const mag = (x) => Math.sqrt(dot(x,x))
const normalize = (x) => div(x,mag(x))
const cross = (x,y) => x.length == 2 ?
0]*y[1]-x[1]*y[0] :
x[.map((e,i)=> x[(i+1)%3]*y[(i+2)%3] - x[(i+2)%3]*y[(i+1)%3]) x
A matrix is a grid of numbers. They can be seen as a vector of vectors in two ways: either as a vector of columns or a vector of rows.
The matrix transpose is computed by treating the rows of as columns and vice-versa. If we say is symmetric. If we say is skew-symmetric or antisymmetric.
The matrix product is a matrix . The value in at row and column is equal to the dot product of row of and column of . Hence, the matrix product is only defined if has the same number of columns as has rows.
The matrix-vector product is computed by treating as if it were a matrix with just one column.
Some texts define the vector-matrix product to be treating as if it were a matrix with just one row, but others disagree and say vectors are always columns an write instead.
Any linear system of equations can be written as
The linear system of equations can be written as
A square matrix has the same number of rows and columns.
The main diagonal of a matrix are the cells with the same row and column index. A diagonal matrix is square and has zeros everywhere except along the main diagonal.
The following is a diagonal matrix
The following is not a diagonal matrix because it has non-zero values on the wrong diagonal:
In graphics1, we’d say the following is not a diagonal matrix because it is not square:
An identity matrix is a diagonal matrix with 1 as the value on its main diagonal. An identity matrix is often written as , and often called the identity matrix even though there are several of them (one for each square matrix size).
Two vectors and are said to be conjugate with respect
to a matrix if . If , conjugate is a
synonym for orthogonal or perpendicular. Note that conjugate
is
also a common term for complex numbers and quaternions with a different meaning in
those contexts.
An orthogonal matrix is a square matrix with the property that . Each row of an orthogonal matrix is a unit vector that is
orthogonal to all other rows. Each column of an orthogonal matrix is a
unit vector that is orthogonal to all other columns. Each orthogonal
matrix is either a rotation matrix or a reflection matrix. If is orthogonal, then . Orthogonal matrix
and real-valued unitary matrix
are synonyms.
Every matrix can be written as where and are both orthogonal matrices and is a diagonal matrix. This is called the singular value decomposition of . The values on the main diagonal of are called ’s singular values.
The condition number of a matrix is the ratio of its largest singular value to its smallest singular value. The farther from 1 the condition number of a matrix is, the less effective (less efficient and/or less precise) almost all algorithms using become. The condition number of any orthogonal matrix is exactly 1, making them computationally optimal in many situations.
Every matrix also has an eigendecomposition, based on eigenvalues and eigenvectors; this is somewhat like the singular value decomposition and can be easier to compute, but it generally has complex numbers in it and is rarely used in graphics.
The inverse of a square matrix is written and has the property that . has an inverse if and only all of its singular values are non-zero.
WebGL2 assumed 4×4 matrices in column-major order; this code only works for that specific case.
const m4row = (m,r) => new m.constructor(4).map((e,i)=>m[r+4*i])
const m4rowdot = (m,r,v) => m[r]*v[0] + m[r+4]*v[1] + m[r+8]*v[2] + m[r+12]*v[3]
const m4col = (m,c) => m.slice(c*4,(c+1)*4)
const m4transpose = (m) => m.map((e,i) => m[((i&3)<<2)+(i>>2)])
const m4mul = (...args) => args.reduce((m1,m2) => {
if(m2.length == 4) return m2.map((e,i)=>m4rowdot(m1,i,m2)) // m*v
if(m1.length == 4) return m1.map((e,i)=>m4rowdot(m2,i,m1)) // v*m
let ans = new m1.constructor(16)
for(let c=0; c<4; c+=1) for(let r=0; r<4; r+=1)
+c*4] = m4rowdot(m1,r,m4col(m2,c))
ans[rreturn ans // m*m
})