TExcelTAD
Home › Math for Machine Learning › Linear Mappings

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

Φ:V→W\Phi: V \to W is linear if for all vectors x,y\mathbf{x}, \mathbf{y} and scalars λ,ψ\lambda, \psi:

Φ(λx+ψy)=λΦ(x)+ψΦ(y)\Phi(\lambda\mathbf{x} + \psi\mathbf{y}) = \lambda\Phi(\mathbf{x}) + \psi\Phi(\mathbf{y})

Consequences: Φ(0)=0\Phi(\mathbf{0}) = \mathbf{0}, and Φ\Phi is completely determined by what it does to a basis. If b1,…,bn\mathbf{b}_1, \ldots, \mathbf{b}_n is a basis of VV, then Φ(x)=∑jxjΦ(bj)\Phi(\mathbf{x}) = \sum_j x_j \Phi(\mathbf{b}_j).

The transformation matrix

Pick a basis B=(b1,…,bn)B = (\mathbf{b}_1, \ldots, \mathbf{b}_n) of VV and C=(c1,…,cm)C = (\mathbf{c}_1, \ldots, \mathbf{c}_m) of WW. Write each image Φ(bj)\Phi(\mathbf{b}_j) in CC-coordinates and put those coordinates in column jj. The result is the transformation matrix AΦ∈Rm×nA_\Phi \in \mathbb{R}^{m \times n}, and

y^=AΦx^\hat{\mathbf{y}} = A_\Phi \hat{\mathbf{x}}

where x^\hat{\mathbf{x}} and y^\hat{\mathbf{y}} are coordinate vectors. "Column jj is the image of basis vector jj" 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.

python · try it yourself
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 SS holds the new basis vectors of VV as columns (in old coordinates) and TT does the same for WW, the matrix of the same map in the new bases is

A~Φ=T−1AΦS\tilde{A}_\Phi = T^{-1} A_\Phi S

Read right to left: convert new coordinates to old (SS), apply the map (AΦA_\Phi), convert back to new coordinates (T−1T^{-1}). For a map from a space to itself with S=TS = T, this is the similarity transform S−1ASS^{-1}AS. Choosing SS to be the eigenvectors makes the matrix diagonal, which is the whole point of eigendecomposition.

python · try it yourself
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 (1,1)(1,1), (1,−1)(1,-1) the map just stretches the first axis by 3 and keeps the second: the diagonal matrix reveals what AA really does.

Image and kernel of a mapping

ker⁡(Φ)\ker(\Phi) and Im⁡(Φ)\operatorname{Im}(\Phi) are the null space and column space of any transformation matrix of Φ\Phi, so rank-nullity applies: dim⁡ker⁡(Φ)+dim⁡Im⁡(Φ)=dim⁡V\dim\ker(\Phi) + \dim\operatorname{Im}(\Phi) = \dim V. Φ\Phi is injective exactly when ker⁡(Φ)={0}\ker(\Phi) = \{\mathbf{0}\}.

In software

  • A dense neural layer without its activation, x↦Wx\mathbf{x} \mapsto W\mathbf{x}, 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 R2→R2\mathbb{R}^2 \to \mathbb{R}^2 are linear? (a) (x,y)↦(2x,x−y)(x, y) \mapsto (2x, x - y); (b) (x,y)↦(x+1,y)(x, y) \mapsto (x + 1, y); (c) (x,y)↦(xy,0)(x, y) \mapsto (xy, 0).

Answer

Only (a).

Solution

(a) is [201−1]x\begin{bmatrix} 2 & 0 \\ 1 & -1 \end{bmatrix}\mathbf{x}, a matrix product. (b) sends 0\mathbf{0} to (1,0)≠0(1, 0) \ne \mathbf{0}. (c) fails scaling: (2x)(2y)=4xy≠2xy(2x)(2y) = 4xy \ne 2xy.

Exercise 2: Write the matrix

Find the matrix of the map that reflects R2\mathbb{R}^2 across the line y=xy = x.

Answer

[0110]\begin{bmatrix} 0 & 1 \\ 1 & 0 \end{bmatrix}.

Solution

Column 1 is the image of e1=(1,0)\mathbf{e}_1 = (1, 0), which is (0,1)(0, 1); column 2 is the image of e2=(0,1)\mathbf{e}_2 = (0, 1), which is (1,0)(1, 0).

Exercise 3: Integration as a matrix (code)

Antidifferentiation with zero constant maps degree ≤2\le 2 polynomials (basis 1,t,t21, t, t^2) to degree ≤3\le 3 polynomials (basis 1,t,t2,t31, t, t^2, t^3). Build its 4×34 \times 3 matrix JJ and check that DJ=IDJ = I for the derivative matrix DD from degree 3 to degree 2.

Answer

JJ has entries 1,12,131, \frac{1}{2}, \frac{1}{3} just below the diagonal, and DJ=I3DJ = I_3.

Solution

1↦t1 \mapsto t, t↦t2/2t \mapsto t^2/2, t2↦t3/3t^2 \mapsto t^3/3 give the columns of JJ.

python · try it yourself
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.]]

JD≠IJD \ne I, because differentiating first loses the constant term.

Test yourself

Column jj of a transformation matrix is…

  1. the jj-th row of the identity
  2. the image of the jj-th basis vector, in output coordinates
  3. the kernel of the map
  4. always a unit vector

Two linear layers W2(W1x)W_2(W_1\mathbf{x}) without activations are equivalent to…

  1. a nonlinear function
  2. a single linear layer with matrix W2W1W_2W_1
  3. the identity
  4. a quadratic form

In the change of basis A~=S−1AS\tilde{A} = S^{-1}AS, the matrix SS…

  1. converts new coordinates to old ones
  2. is always diagonal
  3. must be symmetric
  4. is the inverse of AA