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 is a matrix.
Definition
A function is linear if for all vectors in and all scalars :
Equivalently, preserves every linear combination: .
Two quick consequences:
- , because . A function that moves the origin cannot be linear.
- and .
Examples
| Formula | Linear? | |
|---|---|---|
| Matrix map | yes: , | |
| Scaling by 3 | yes | |
| Projection to the -axis | yes | |
| Rotation by about | see geometric interpretation | yes |
| Dot with a fixed | () | yes |
| Translation | no: | |
| Squaring | no: | |
| Length | no: |
Functions of the form with 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:
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 "" is the one-line calculation in the table above.
A linear map is determined by a few values
Because preserves combinations, knowing on a spanning set tells you everywhere. In , if and , then
This observation is exactly how the matrix of is built in The Matrix of a Linear Transformation.
Composition
If and are linear, so is (first , then ):
For matrix maps, 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 . Stacking two layers without a non-linear activation gives : 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:
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) , (b) , (c) , (d) .
Answer
(a) and (c) are linear; (b) and (d) are not.
Solution
(a) , a matrix map. (b) but . (c) , a dot product with a fixed vector. (d) .
Exercise 2: Use linearity
is linear with and . Compute .
Answer
.
Solution
Exercise 3: Values on a different basis
is linear with and . Find and .
Answer
; .
Solution
The same result in code: the matrix of is (columns), as the next lesson explains.
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 from additivity alone
Show that additivity by itself already forces .
Answer
, so .
Solution
Apply additivity with : . Subtract from both sides to get .
Test yourself
Which map is linear?
Why is a translation () not linear?
- It sends to instead of
- It changes lengths
- It is not invertible
- It is only defined in
What do two stacked neural-network layers without an activation function compute?
- A quadratic function
- Nothing useful at all
- A rotation only
- A single affine map, like one layer