TExcelTAD
Home › Linear Algebra & Matrix Theory › Linear Transformations

Linear Transformations

A linear transformation is a function between vector spaces that respects addition and scaling. Rotations, reflections, projections and scalings are linear; translations and anything with squares are not. Every matrix gives a linear transformation, and the next lesson shows that every linear transformation Rn→Rm\mathbb{R}^n \to \mathbb{R}^m is a matrix.

Definition

A function T:Rn→RmT : \mathbb{R}^n \to \mathbb{R}^m is linear if for all vectors u,v\mathbf{u}, \mathbf{v} in Rn\mathbb{R}^n and all scalars cc:

1.T(u+v)=T(u)+T(v)(additive)2.T(cu)=c⋅T(u)(homogeneous)\begin{aligned} &1.\quad T(\mathbf{u} + \mathbf{v}) = T(\mathbf{u}) + T(\mathbf{v}) && \text{(additive)} \\ &2.\quad T(c\mathbf{u}) = c \cdot T(\mathbf{u}) && \text{(homogeneous)} \end{aligned}

Equivalently, TT preserves every linear combination: T(c1v1+⋯+ckvk)=c1T(v1)+⋯+ckT(vk)T(c_1\mathbf{v}_1 + \cdots + c_k\mathbf{v}_k) = c_1T(\mathbf{v}_1) + \cdots + c_kT(\mathbf{v}_k).

Two quick consequences:

  • T(0)=0T(\mathbf{0}) = \mathbf{0}, because T(0)=T(0⋅0)=0⋅T(0)=0T(\mathbf{0}) = T(0 \cdot \mathbf{0}) = 0 \cdot T(\mathbf{0}) = \mathbf{0}. A function that moves the origin cannot be linear.
  • T(−v)=−T(v)T(-\mathbf{v}) = -T(\mathbf{v}) and T(u−v)=T(u)−T(v)T(\mathbf{u} - \mathbf{v}) = T(\mathbf{u}) - T(\mathbf{v}).

Examples

TT Formula Linear?
Matrix map T(x)=AxT(\mathbf{x}) = A\mathbf{x} yes: A(u+v)=Au+AvA(\mathbf{u} + \mathbf{v}) = A\mathbf{u} + A\mathbf{v}, A(cu)=cAuA(c\mathbf{u}) = cA\mathbf{u}
Scaling by 3 T(x)=3xT(\mathbf{x}) = 3\mathbf{x} yes
Projection to the xx-axis T([x,y])=[x,0]T([x, y]) = [x, 0] yes
Rotation by θ\theta about 0\mathbf{0} see geometric interpretation yes
Dot with a fixed a\mathbf{a} T(x)=a⋅xT(\mathbf{x}) = \mathbf{a} \cdot \mathbf{x} (Rn→R\mathbb{R}^n \to \mathbb{R}) yes
Translation T(x)=x+[1,2]T(\mathbf{x}) = \mathbf{x} + [1, 2] no: T(0)≠0T(\mathbf{0}) \ne \mathbf{0}
Squaring T([x,y])=[x2,y]T([x, y]) = [x^2, y] no: T(2⋅[1,0])=[4,0]≠2⋅[1,0]T(2 \cdot [1, 0]) = [4, 0] \ne 2 \cdot [1, 0]
Length T(x)=∥x∥T(\mathbf{x}) = \lVert \mathbf{x} \rVert no: ∥−x∥≠−∥x∥\lVert -\mathbf{x} \rVert \ne -\lVert \mathbf{x} \rVert

Functions of the form T(x)=Ax+bT(\mathbf{x}) = A\mathbf{x} + \mathbf{b} with b≠0\mathbf{b} \ne \mathbf{0} are called affine. They are "linear plus a shift", the right model for moving objects around in a game.

Checking linearity

To prove linearity you show the two rules for all inputs. To disprove it, one counterexample suffices, and random testing finds one quickly:

python · try it yourself
import numpy as np

rng = np.random.default_rng(2)

def is_linear(T, n, trials=200):
    for _ in range(trials):
        u, v = rng.normal(size=n), rng.normal(size=n)
        c = float(rng.normal())
        if not (np.allclose(T(u + v), T(u) + T(v)) and np.allclose(T(c * u), c * T(u))):
            return False
    return True

A = np.array([[2.0, -1.0, 0.0], [0.5, 0.0, 3.0]])
tests = {
    "x -> A x":            (lambda x: A @ x, 3),
    "[x, y] -> [x, 0]":    (lambda p: np.array([p[0], 0.0]), 2),
    "x -> x + [1, 2]":     (lambda p: p + np.array([1.0, 2.0]), 2),
    "[x, y] -> [x^2, y]":  (lambda p: np.array([p[0] ** 2, p[1]]), 2),
    "[x, y] -> [y, x]":    (lambda p: p[::-1].copy(), 2),
}
for name, (T, n) in tests.items():
    print(f"{name:20s} linear: {is_linear(T, n)}")
x -> A x             linear: True
[x, y] -> [x, 0]     linear: True
x -> x + [1, 2]      linear: False
[x, y] -> [x^2, y]   linear: False
[x, y] -> [y, x]     linear: True

Note: Passing a random test is evidence, not proof. A proof for "x↦Ax\mathbf{x} \mapsto A\mathbf{x}" is the one-line calculation in the table above.

A linear map is determined by a few values

Because TT preserves combinations, knowing TT on a spanning set tells you TT everywhere. In R2\mathbb{R}^2, if T([1,0])=[2,1]T([1, 0]) = [2, 1] and T([0,1])=[−1,3]T([0, 1]) = [-1, 3], then

T([4,5])=T(4⋅[1,0]+5⋅[0,1])=4⋅[2,1]+5⋅[−1,3]=[3,19]T([4, 5]) = T(4 \cdot [1, 0] + 5 \cdot [0, 1]) = 4 \cdot [2, 1] + 5 \cdot [-1, 3] = [3, 19]

This observation is exactly how the matrix of TT is built in The Matrix of a Linear Transformation.

Composition

If S:Rn→RmS : \mathbb{R}^n \to \mathbb{R}^m and T:Rm→RpT : \mathbb{R}^m \to \mathbb{R}^p are linear, so is T∘ST \circ S (first SS, then TT):

T(S(u+v))=T(S(u)+S(v))=T(S(u))+T(S(v)),and similarly for scalars.T(S(\mathbf{u} + \mathbf{v})) = T(S(\mathbf{u}) + S(\mathbf{v})) = T(S(\mathbf{u})) + T(S(\mathbf{v})), \quad \text{and similarly for scalars.}

For matrix maps, T∘ST \circ S corresponds to the product of the matrices, which is the real reason matrix multiplication is defined the way it is.

In software

  • Why neural networks need activations. A layer computes xW+b\mathbf{x}W + \mathbf{b}. Stacking two layers without a non-linear activation gives (xW1+b1)W2+b2=x(W1W2)+(b1W2+b2)(\mathbf{x}W_1 + \mathbf{b}_1)W_2 + \mathbf{b}_2 = \mathbf{x}(W_1W_2) + (\mathbf{b}_1W_2 + \mathbf{b}_2): still a single affine map. However deep, a purely linear network is no more expressive than one layer. Non-linear activations such as ReLU break the collapse:
python · try it yourself
import numpy as np

rng = np.random.default_rng(8)
W1, b1 = rng.normal(size=(3, 4)), rng.normal(size=4)
W2, b2 = rng.normal(size=(4, 2)), rng.normal(size=2)
X = rng.normal(size=(5, 3))

two_linear = (X @ W1 + b1) @ W2 + b2
one_layer = X @ (W1 @ W2) + (b1 @ W2 + b2)
print("two linear layers == one layer:", np.allclose(two_linear, one_layer))

relu = lambda z: np.maximum(z, 0)
with_relu = relu(X @ W1 + b1) @ W2 + b2
print("with ReLU, still equal to it:", np.allclose(with_relu, one_layer))
two linear layers == one layer: True
with ReLU, still equal to it: False
  • Graphics applies linear maps (rotation, scale, shear) to millions of vertices per frame. Translation is affine, not linear, so engines use 4×4 matrices on homogeneous coordinates to make it a matrix product too (Homogeneous Coordinates).
  • Signal processing: filters such as blurs, convolutions and the discrete Fourier transform are linear in the signal; that is what lets you analyse them with matrices.

Exercises

Exercise 1: Linear or not?

Decide which are linear: (a) T([x,y])=[3x−y,x]T([x, y]) = [3x - y, x], (b) T([x,y])=[xy,0]T([x, y]) = [xy, 0], (c) T([x,y,z])=x+y+zT([x, y, z]) = x + y + z, (d) T([x,y])=[x+1,y−1]T([x, y]) = [x + 1, y - 1].

Answer

(a) and (c) are linear; (b) and (d) are not.

Solution

(a) T(x)=[3−110][xy]T(\mathbf{x}) = \begin{bmatrix} 3 & -1 \\ 1 & 0 \end{bmatrix} \begin{bmatrix} x \\ y \end{bmatrix}, a matrix map. (b) T(2⋅[1,1])=[4,0]T(2 \cdot [1, 1]) = [4, 0] but 2⋅T([1,1])=[2,0]2 \cdot T([1, 1]) = [2, 0]. (c) T(x)=[1,1,1]⋅xT(\mathbf{x}) = [1, 1, 1] \cdot \mathbf{x}, a dot product with a fixed vector. (d) T(0)=[1,−1]≠0T(\mathbf{0}) = [1, -1] \ne \mathbf{0}.

Exercise 2: Use linearity

T:R2→R3T : \mathbb{R}^2 \to \mathbb{R}^3 is linear with T([1,0])=[1,2,0]T([1, 0]) = [1, 2, 0] and T([0,1])=[−1,0,3]T([0, 1]) = [-1, 0, 3]. Compute T([3,−2])T([3, -2]).

Answer

T([3,−2])=[5,6,−6]T([3, -2]) = [5, 6, -6].

Solution
T([3,−2])=3⋅T([1,0])−2⋅T([0,1])=[3,6,0]−[−2,0,6]=[5,6,−6]T([3, -2]) = 3 \cdot T([1, 0]) - 2 \cdot T([0, 1]) = [3, 6, 0] - [-2, 0, 6] = [5, 6, -6]

Exercise 3: Values on a different basis

T:R2→R2T : \mathbb{R}^2 \to \mathbb{R}^2 is linear with T([1,1])=[3,1]T([1, 1]) = [3, 1] and T([1,−1])=[1,5]T([1, -1]) = [1, 5]. Find T([1,0])T([1, 0]) and T([4,2])T([4, 2]).

Answer

T([1,0])=[2,3]T([1, 0]) = [2, 3]; T([4,2])=[10,8]T([4, 2]) = [10, 8].

Solution
[1,0]=12[1,1]+12[1,−1]→T([1,0])=12[3,1]+12[1,5]=[2,3][4,2]=3[1,1]+1[1,−1]→T([4,2])=3[3,1]+[1,5]=[10,8]\begin{aligned} [1, 0] &= \tfrac{1}{2}[1, 1] + \tfrac{1}{2}[1, -1] &&\to\quad T([1, 0]) = \tfrac{1}{2}[3, 1] + \tfrac{1}{2}[1, 5] = [2, 3] \\ [4, 2] &= 3[1, 1] + 1[1, -1] &&\to\quad T([4, 2]) = 3[3, 1] + [1, 5] = [10, 8] \end{aligned}

The same result in code: the matrix of TT is M=[T-outputs]⋅[inputs]−1M = [T\text{-outputs}] \cdot [\text{inputs}]^{-1} (columns), as the next lesson explains.

python · try it yourself
import numpy as np
B = np.array([[1, 1], [1, -1]]).T          # inputs as columns
TB = np.array([[3, 1], [1, 5]]).T          # outputs as columns
M = TB @ np.linalg.inv(B)                  # the matrix of T
print(M.tolist(), (M @ [4, 2]).tolist())
[[2.0, 1.0], [3.0, -2.0]] [10.0, 8.0]

Exercise 4: Prove T(0)=0T(\mathbf{0}) = \mathbf{0} from additivity alone

Show that additivity T(u+v)=T(u)+T(v)T(\mathbf{u} + \mathbf{v}) = T(\mathbf{u}) + T(\mathbf{v}) by itself already forces T(0)=0T(\mathbf{0}) = \mathbf{0}.

Answer

T(0)=T(0+0)=T(0)+T(0)T(\mathbf{0}) = T(\mathbf{0} + \mathbf{0}) = T(\mathbf{0}) + T(\mathbf{0}), so T(0)=0T(\mathbf{0}) = \mathbf{0}.

Solution

Apply additivity with u=v=0\mathbf{u} = \mathbf{v} = \mathbf{0}: T(0)=T(0+0)=T(0)+T(0)T(\mathbf{0}) = T(\mathbf{0} + \mathbf{0}) = T(\mathbf{0}) + T(\mathbf{0}). Subtract T(0)T(\mathbf{0}) from both sides to get 0=T(0)\mathbf{0} = T(\mathbf{0}).

Test yourself

Which map R2→R2\mathbb{R}^2 \to \mathbb{R}^2 is linear?

  1. [x,y]↦[x+y,1][x, y] \mapsto [x + y, 1]
  2. [x,y]↦[2x,x−y][x, y] \mapsto [2x, x - y]
  3. [x,y]↦[x2,y2][x, y] \mapsto [x^2, y^2]
  4. [x,y]↦[∣x∣,y][x, y] \mapsto [\lvert x \rvert, y]

Why is a translation x↦x+b\mathbf{x} \mapsto \mathbf{x} + \mathbf{b} (b≠0\mathbf{b} \ne \mathbf{0}) not linear?

  1. It sends 0\mathbf{0} to b\mathbf{b} instead of 0\mathbf{0}
  2. It changes lengths
  3. It is not invertible
  4. It is only defined in R2\mathbb{R}^2

What do two stacked neural-network layers without an activation function compute?

  1. A quadratic function
  2. Nothing useful at all
  3. A rotation only
  4. A single affine map, like one layer