TExcelTAD
Home › Linear Algebra & Matrix Theory › The Matrix of a Linear Transformation

The Matrix of a Linear Transformation

Every linear transformation T:Rn→RmT : \mathbb{R}^n \to \mathbb{R}^m is multiplication by a unique m×nm \times n matrix, its standard matrix, and the recipe for finding it is short: the columns are the images of the standard basis vectors. Composition of transformations then becomes matrix multiplication.

The recipe

Let e1,…,en\mathbf{e}_1, \ldots, \mathbf{e}_n be the standard basis of Rn\mathbb{R}^n. Any x=[x1,…,xn]\mathbf{x} = [x_1, \ldots, x_n] is x1e1+⋯+xnenx_1\mathbf{e}_1 + \cdots + x_n\mathbf{e}_n, so by linearity

T(x)=x1T(e1)+x2T(e2)+⋯+xnT(en)=[T(e1) T(e2) ⋯ T(en)]⋅xT(\mathbf{x}) = x_1T(\mathbf{e}_1) + x_2T(\mathbf{e}_2) + \cdots + x_nT(\mathbf{e}_n) = [T(\mathbf{e}_1)\ T(\mathbf{e}_2)\ \cdots\ T(\mathbf{e}_n)] \cdot \mathbf{x}

The last step is the column view of a matrix–vector product (Matrix Multiplication). So

A=[ T(e1)∣T(e2)∣⋯∣T(en) ](column j=where T sends ej)A = [\, T(\mathbf{e}_1) \mid T(\mathbf{e}_2) \mid \cdots \mid T(\mathbf{e}_n) \,] \qquad (\text{column } j = \text{where } T \text{ sends } \mathbf{e}_j)

satisfies T(x)=AxT(\mathbf{x}) = A\mathbf{x} for every x\mathbf{x}, and it is the only such matrix (two matrices that agree on every ej\mathbf{e}_j have the same columns).

Example. The reflection of R2\mathbb{R}^2 across the line y=xy = x swaps coordinates: T(e1)=[0,1]T(\mathbf{e}_1) = [0, 1] and T(e2)=[1,0]T(\mathbf{e}_2) = [1, 0], so A=[0110]A = \begin{bmatrix} 0 & 1 \\ 1 & 0 \end{bmatrix}.

A gallery of standard matrices (R2\mathbb{R}^2)

Transformation T(e1)T(\mathbf{e}_1) T(e2)T(\mathbf{e}_2) Matrix
Rotation by θ\theta counter-clockwise [cos⁡θ,sin⁡θ][\cos\theta, \sin\theta] [−sin⁡θ,cos⁡θ][-\sin\theta, \cos\theta] [cos⁡θ−sin⁡θsin⁡θcos⁡θ]\begin{bmatrix} \cos\theta & -\sin\theta \\ \sin\theta & \cos\theta \end{bmatrix}
Reflection across the xx-axis [1,0][1, 0] [0,−1][0, -1] [100−1]\begin{bmatrix} 1 & 0 \\ 0 & -1 \end{bmatrix}
Scaling by aa (xx) and bb (yy) [a,0][a, 0] [0,b][0, b] [a00b]\begin{bmatrix} a & 0 \\ 0 & b \end{bmatrix}
Horizontal shear by kk [1,0][1, 0] [k,1][k, 1] [1k01]\begin{bmatrix} 1 & k \\ 0 & 1 \end{bmatrix}
Projection onto the xx-axis [1,0][1, 0] [0,0][0, 0] [1000]\begin{bmatrix} 1 & 0 \\ 0 & 0 \end{bmatrix}
Projection onto the line through unit u=[a,b]\mathbf{u} = [a, b] a⋅ua \cdot \mathbf{u} b⋅ub \cdot \mathbf{u} [a2ababb2]=uuT\begin{bmatrix} a^2 & ab \\ ab & b^2 \end{bmatrix} = \mathbf{u}\mathbf{u}^T

The projection entry comes from proj⁡u(x)=(u⋅x)u\operatorname{proj}_{\mathbf{u}}(\mathbf{x}) = (\mathbf{u} \cdot \mathbf{x})\mathbf{u} (The Dot Product): e1\mathbf{e}_1 goes to (u⋅e1)u=a⋅u(\mathbf{u} \cdot \mathbf{e}_1)\mathbf{u} = a \cdot \mathbf{u}.

python · try it yourself
from sympy import Matrix, Rational, sqrt, cos, sin, pi, simplify

def standard_matrix(T, n):
    cols = [T(Matrix.eye(n).col(j)) for j in range(n)]
    return Matrix.hstack(*cols)

# projection onto the line spanned by [3, 4] (unit vector [3/5, 4/5])
u = Matrix([Rational(3, 5), Rational(4, 5)])
proj = lambda x: (u.dot(x)) * u
P = standard_matrix(proj, 2)
print("projection matrix:", P.tolist())
print("P == u u^T:", P == u * u.T, "  P^2 == P:", P * P == P)

theta = pi / 6
R = Matrix([[cos(theta), -sin(theta)], [sin(theta), cos(theta)]])
print("rotation by 30 degrees:", R.tolist())
projection matrix: [[9/25, 12/25], [12/25, 16/25]]
P == u u^T: True   P^2 == P: True
rotation by 30 degrees: [[sqrt(3)/2, -1/2], [1/2, sqrt(3)/2]]

P2=PP^2 = P says "projecting twice is the same as projecting once".

Composition is multiplication

If SS has matrix BB and TT has matrix AA, then T(S(x))=A(Bx)=(AB)xT(S(\mathbf{x})) = A(B\mathbf{x}) = (AB)\mathbf{x}, so T∘ST \circ S has matrix ABAB. The rightmost matrix acts first. Rotating by α\alpha and then by β\beta is rotating by α+β\alpha + \beta, and multiplying the rotation matrices proves the angle-addition formulas of trigonometry:

python · try it yourself
from sympy import symbols, cos, sin, Matrix, simplify, expand_trig

a, b = symbols("alpha beta")
rot = lambda t: Matrix([[cos(t), -sin(t)], [sin(t), cos(t)]])
product = rot(b) * rot(a)
print("entry (1,1):", product[0, 0])
print("equals cos(alpha+beta):", simplify(product[0, 0] - cos(a + b)) == 0)
print("whole matrix equals rot(alpha+beta):", simplify(product - rot(a + b)) == Matrix.zeros(2, 2))
entry (1,1): -sin(alpha)*sin(beta) + cos(alpha)*cos(beta)
equals cos(alpha+beta): True
whole matrix equals rot(alpha+beta): True

When TT is known on another basis

Sometimes you know TT on a basis b1,…,bn\mathbf{b}_1, \ldots, \mathbf{b}_n that is not the standard one. With B=[b1 ⋯ bn]B = [\mathbf{b}_1\ \cdots\ \mathbf{b}_n] and C=[T(b1) ⋯ T(bn)]C = [T(\mathbf{b}_1)\ \cdots\ T(\mathbf{b}_n)], the equation AB=CAB = C gives

A=CB−1A = CB^{-1}

For example, a reflection is easiest to describe on a basis adapted to its mirror line: across the line y=2xy = 2x, the direction [1,2][1, 2] is fixed and the perpendicular [−2,1][-2, 1] flips. So B=[1−221]B = \begin{bmatrix} 1 & -2 \\ 2 & 1 \end{bmatrix} and C=[122−1]C = \begin{bmatrix} 1 & 2 \\ 2 & -1 \end{bmatrix}, and A=CB−1=15[−3443]A = CB^{-1} = \frac{1}{5}\begin{bmatrix} -3 & 4 \\ 4 & 3 \end{bmatrix} (see Exercise 3).

In software

  • Vertex shaders multiply every vertex by a matrix, and the columns of that matrix are literally where the object's local xx, yy, zz axes end up in the world. Reading a transform matrix column by column tells you the object's orientation and scale at a glance (Matrices and Multiplication).
  • Row vs column vectors. Math and OpenGL/GLM write T(x)=AxT(\mathbf{x}) = A\mathbf{x} with column vectors; DirectX/HLSL and many older engines use row vectors, x′=xM\mathbf{x}' = \mathbf{x}M, with M=ATM = A^T. Products then compose left to right instead of right to left. Mixing conventions transposes your matrices, so check which one a library uses (Handedness, Up Axes and Units).
  • Learned linear maps. A neural network layer is a learned standard matrix: column jj of WW (in the y=Wx\mathbf{y} = W\mathbf{x} convention) is the output pattern produced by the jj-th input feature on its own.

Exercises

Exercise 1: Write the matrix

Find the standard matrix of T([x,y,z])=[x−2z,3y,x+y+z]T([x, y, z]) = [x - 2z, 3y, x + y + z].

Answer

A=[10−2030111]A = \begin{bmatrix} 1 & 0 & -2 \\ 0 & 3 & 0 \\ 1 & 1 & 1 \end{bmatrix}.

Solution
T(e1)=T([1,0,0])=[1,0,1]T(e2)=T([0,1,0])=[0,3,1]T(e3)=T([0,0,1])=[−2,0,1]A=[T(e1)∣T(e2)∣T(e3)]=[10−2030111]\begin{aligned} T(\mathbf{e}_1) &= T([1, 0, 0]) = [1, 0, 1] \\ T(\mathbf{e}_2) &= T([0, 1, 0]) = [0, 3, 1] \\ T(\mathbf{e}_3) &= T([0, 0, 1]) = [-2, 0, 1] \\ A &= [T(\mathbf{e}_1) \mid T(\mathbf{e}_2) \mid T(\mathbf{e}_3)] = \begin{bmatrix} 1 & 0 & -2 \\ 0 & 3 & 0 \\ 1 & 1 & 1 \end{bmatrix} \end{aligned}

Exercise 2: Compose two maps

SS rotates R2\mathbb{R}^2 by 90° counter-clockwise, TT reflects across the xx-axis. Find the matrices of T∘ST \circ S and S∘TS \circ T. Are they equal?

Answer

T∘S=[0−1−10]T \circ S = \begin{bmatrix} 0 & -1 \\ -1 & 0 \end{bmatrix}, S∘T=[0110]S \circ T = \begin{bmatrix} 0 & 1 \\ 1 & 0 \end{bmatrix}; they are not equal.

Solution
S=[0−110],T=[100−1]T∘S→TS=[1⋅0+0⋅11⋅(−1)+0⋅00⋅0+(−1)⋅10⋅(−1)+(−1)⋅0]=[0−1−10]S∘T→ST=[0⋅1+(−1)⋅00⋅0+(−1)(−1)1⋅1+0⋅01⋅0+0⋅(−1)]=[0110]\begin{aligned} &S = \begin{bmatrix} 0 & -1 \\ 1 & 0 \end{bmatrix}, \quad T = \begin{bmatrix} 1 & 0 \\ 0 & -1 \end{bmatrix} \\ &T \circ S \to TS = \begin{bmatrix} 1 \cdot 0 + 0 \cdot 1 & 1 \cdot (-1) + 0 \cdot 0 \\ 0 \cdot 0 + (-1) \cdot 1 & 0 \cdot (-1) + (-1) \cdot 0 \end{bmatrix} = \begin{bmatrix} 0 & -1 \\ -1 & 0 \end{bmatrix} \\ &S \circ T \to ST = \begin{bmatrix} 0 \cdot 1 + (-1) \cdot 0 & 0 \cdot 0 + (-1)(-1) \\ 1 \cdot 1 + 0 \cdot 0 & 1 \cdot 0 + 0 \cdot (-1) \end{bmatrix} = \begin{bmatrix} 0 & 1 \\ 1 & 0 \end{bmatrix} \end{aligned}

TSTS is the reflection across y=−xy = -x and STST the reflection across y=xy = x.

Exercise 3: Reflection across y=2xy = 2x

Using A=CB−1A = CB^{-1} with B=[1−221]B = \begin{bmatrix} 1 & -2 \\ 2 & 1 \end{bmatrix} and C=[122−1]C = \begin{bmatrix} 1 & 2 \\ 2 & -1 \end{bmatrix}, compute the reflection matrix and check that applying it twice gives II.

Answer

A=[−35454535]A = \begin{bmatrix} -\frac{3}{5} & \frac{4}{5} \\ \frac{4}{5} & \frac{3}{5} \end{bmatrix}, and A2=IA^2 = I.

Solution
det⁡B=1+4=5,B−1=15[12−21]CB−1=15[1⋅1+2(−2)1⋅2+2⋅12⋅1+(−1)(−2)2⋅2+(−1)⋅1]=15[−3443]\begin{aligned} \det B &= 1 + 4 = 5, \quad B^{-1} = \frac{1}{5}\begin{bmatrix} 1 & 2 \\ -2 & 1 \end{bmatrix} \\ CB^{-1} &= \frac{1}{5}\begin{bmatrix} 1 \cdot 1 + 2(-2) & 1 \cdot 2 + 2 \cdot 1 \\ 2 \cdot 1 + (-1)(-2) & 2 \cdot 2 + (-1) \cdot 1 \end{bmatrix} \\ &= \frac{1}{5}\begin{bmatrix} -3 & 4 \\ 4 & 3 \end{bmatrix} \end{aligned}
python · try it yourself
from sympy import Matrix, eye
B = Matrix([[1, -2], [2, 1]]); C = Matrix([[1, 2], [2, -1]])
A = C * B.inv()
print(A.tolist(), A * A == eye(2))
[[-3/5, 4/5], [4/5, 3/5]] True

Exercise 4: A projection in code

Using standard_matrix from the lesson (or by hand), find the matrix of the projection of R3\mathbb{R}^3 onto the line spanned by [1,1,1][1, 1, 1].

Answer

Every entry is 13\frac{1}{3}: A=13⋅[111111111]A = \frac{1}{3} \cdot \begin{bmatrix} 1 & 1 & 1 \\ 1 & 1 & 1 \\ 1 & 1 & 1 \end{bmatrix}.

Solution

The unit vector is u=[1,1,1]/3\mathbf{u} = [1, 1, 1]/\sqrt{3}, and A=uuTA = \mathbf{u}\mathbf{u}^T has every entry (1/3)2=13(1/\sqrt{3})^2 = \frac{1}{3}.

python · try it yourself
from sympy import Matrix
v = Matrix([1, 1, 1])
A = v * v.T / v.dot(v)
print(A.tolist(), list(A * Matrix([3, 0, 6])))
[[1/3, 1/3, 1/3], [1/3, 1/3, 1/3], [1/3, 1/3, 1/3]] [3, 3, 3]

Test yourself

T:R2→R2T : \mathbb{R}^2 \to \mathbb{R}^2 sends e1\mathbf{e}_1 to [2,5][2, 5] and e2\mathbf{e}_2 to [−1,0][-1, 0]. What is its standard matrix?

  1. [25−10]\begin{bmatrix} 2 & 5 \\ -1 & 0 \end{bmatrix}
  2. [−1205]\begin{bmatrix} -1 & 2 \\ 0 & 5 \end{bmatrix}
  3. [520−1]\begin{bmatrix} 5 & 2 \\ 0 & -1 \end{bmatrix}
  4. [2−150]\begin{bmatrix} 2 & -1 \\ 5 & 0 \end{bmatrix}

SS has matrix BB and TT has matrix AA. What is the matrix of 'first SS, then TT'?

  1. BABA
  2. A+BA + B
  3. ABAB
  4. BTATB^TA^T

What size is the standard matrix of a linear map R4→R2\mathbb{R}^4 \to \mathbb{R}^2?

  1. 4×2
  2. 2×4
  3. 4×4
  4. 2×2