Linear Mappings
A linear mapping is a function between vector spaces that respects addition and scaling. Every linear mapping between finite-dimensional spaces is a matrix once you pick bases, and changing the bases changes the matrix in a predictable way. This lesson connects functions, matrices and coordinates, the language in which a neural layer or a feature transform is described.
Definition
is linear if for all vectors and scalars :
Consequences: , and is completely determined by what it does to a basis. If is a basis of , then .
The transformation matrix
Pick a basis of and of . Write each image in -coordinates and put those coordinates in column . The result is the transformation matrix , and
where and are coordinate vectors. "Column is the image of basis vector " is the most useful sentence in linear algebra: it lets you write down the matrix of a rotation, a projection or a derivative by hand.
import numpy as np
# The derivative is linear on polynomials of degree <= 3, basis (1, t, t^2, t^3).
# d/dt maps 1 -> 0, t -> 1, t^2 -> 2t, t^3 -> 3t^2: these are the columns.
D = np.array([[0, 1, 0, 0],
[0, 0, 2, 0],
[0, 0, 0, 3],
[0, 0, 0, 0]], dtype=float)
p = np.array([5, -1, 4, 2], dtype=float) # 5 - t + 4t^2 + 2t^3
print("p' coefficients:", D @ p) # -1 + 8t + 6t^2
print("p'' coefficients:", D @ D @ p)
print("rank of D:", np.linalg.matrix_rank(D), " kernel = constants")p' coefficients: [-1. 8. 6. 0.]
p'' coefficients: [ 8. 12. 0. 0.]
rank of D: 3 kernel = constants
Change of basis
If holds the new basis vectors of as columns (in old coordinates) and does the same for , the matrix of the same map in the new bases is
Read right to left: convert new coordinates to old (), apply the map (), convert back to new coordinates (). For a map from a space to itself with , this is the similarity transform . Choosing to be the eigenvectors makes the matrix diagonal, which is the whole point of eigendecomposition.
import numpy as np
A = np.array([[2.0, 1.0],
[1.0, 2.0]]) # a map R^2 -> R^2 in the standard basis
S = np.array([[1.0, 1.0],
[1.0, -1.0]]) # new basis vectors (1,1) and (1,-1) as columns
A_new = np.linalg.inv(S) @ A @ S
print("matrix in the new basis:\n", np.round(A_new, 10) + 0)
x_new = np.array([3.0, 1.0]) # coordinates in the new basis
x_old = S @ x_new
print("same result both ways:", np.allclose(S @ (A_new @ x_new), A @ x_old))matrix in the new basis:
[[3. 0.]
[0. 1.]]
same result both ways: True
In the basis , the map just stretches the first axis by 3 and keeps the second: the diagonal matrix reveals what really does.
Image and kernel of a mapping
and are the null space and column space of any transformation matrix of , so rank-nullity applies: . is injective exactly when .
In software
- A dense neural layer without its activation, , is a linear mapping; stacking linear layers without nonlinearities collapses to one matrix, which is why activations are needed (Neural Network Layers as Matrices).
- Feature standardization and whitening are changes of basis (plus a shift).
- Convolution and the discrete Fourier transform are linear maps with special, structured matrices.
Exercises
Exercise 1: Linear or not?
Which maps are linear? (a) ; (b) ; (c) .
Answer
Only (a).
Solution
(a) is , a matrix product. (b) sends to . (c) fails scaling: .
Exercise 2: Write the matrix
Find the matrix of the map that reflects across the line .
Answer
.
Solution
Column 1 is the image of , which is ; column 2 is the image of , which is .
Exercise 3: Integration as a matrix (code)
Antidifferentiation with zero constant maps degree polynomials (basis ) to degree polynomials (basis ). Build its matrix and check that for the derivative matrix from degree 3 to degree 2.
Answer
has entries just below the diagonal, and .
Solution
, , give the columns of .
import numpy as np
J = np.array([[0, 0, 0], [1, 0, 0], [0, 1/2, 0], [0, 0, 1/3]])
D = np.array([[0, 1, 0, 0], [0, 0, 2, 0], [0, 0, 0, 3]], dtype=float)
print(D @ J)[[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]]
, because differentiating first loses the constant term.
Test yourself
Column of a transformation matrix is…
- the -th row of the identity
- the image of the -th basis vector, in output coordinates
- the kernel of the map
- always a unit vector
Two linear layers without activations are equivalent to…
- a nonlinear function
- a single linear layer with matrix
- the identity
- a quadratic form
In the change of basis , the matrix …
- converts new coordinates to old ones
- is always diagonal
- must be symmetric
- is the inverse of