Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

2026-09-16 Projections, rotations, and reflections

Last time

  • Algebra of linear transformations

  • Polynomial evaluation and fitting

Today

  • Orthogonality

  • Projections

  • Rotations

  • Reflections

  • Oh my!

using LinearAlgebra
using Plots
using Polynomials
default(linewidth=4, legendfontsize=12)

function vander(x, k=nothing)
    if isnothing(k)
        k = length(x)
    end
    m = length(x)
    V = ones(m, k)
    for j in 2:k
        V[:, j] = V[:, j-1] .* x
    end
    V
end
vander (generic function with 2 methods)

Matrices as linear transformations

Linear algebra is the study of linear transformations on vectors, which represent points in a finite dimensional space. The matrix-vector product y=Axy = A x is a linear combination of the columns of AA. The familiar definition,

yi=∑jAi,jxjy_i = \sum_j A_{i,j} x_j

can also be viewed as

y=[A:,1∣A:,2∣⋯][x1x2⋮]=[A:,1]x1+[A:,2]x2+⋯ .y = \Bigg[ A_{:,1} \Bigg| A_{:,2} \Bigg| \dotsm \Bigg] \begin{bmatrix} x_1 \\ x_2 \\ \vdots \end{bmatrix} = \Bigg[ A_{:,1} \Bigg] x_1 + \Bigg[ A_{:,2} \Bigg] x_2 + \dotsb .

Some common terminology

  • The range of AA is the space spanned by its columns. This definition coincides with the range of a function f(x)f(x) when f(x)=Axf(x) = A x.

  • The (right) nullspace of AA is the space of vectors xx such that Ax=0A x = 0.

  • The rank of AA is the dimension of its range.

  • A matrix has full rank if the nullspace of either AA or ATA^T is empty (only the 0 vector). Equivalently, if all the columns of AA (or ATA^T) are linearly independent.

  • A nonsingular (or invertible) matrix is a square matrix of full rank. We call the inverse A−1A^{-1} and it satisfies A−1A=AA−1=IA^{-1} A = A A^{-1} = I.

\DeclareMathOperator{\rank}{rank} \DeclareMathOperator{\null}{null} If A∈Rm×mA \in \mathbb{R}^{m\times m}, which of these doesn’t belong?

  1. AA has an inverse A−1A^{-1}

  2. \rank (A) = m

  3. \null(A) = \{0\}

  4. AAT=ATAA A^T = A^T A

  5. det⁡(A)≠0\det(A) \ne 0

  6. Ax=0A x = 0 implies that x=0x = 0

A = rand(3, 3)
B = A' * A - A * A'
@show B
det(A)
B = [0.9896857143454902 0.42963952112909776 0.3351984470282128; 0.42963952112909776 -0.6628051229999194 -0.4536796078590086; 0.3351984470282128 -0.4536796078590086 -0.3268805913455709]
-0.47063758000428857

What is an inverse?

When we write x=A−1yx = A^{-1} y, we mean that xx is the unique vector such that Ax=yA x = y. (It is rare that we explicitly compute a matrix A−1A^{-1}, though it’s not as “bad” as people may have told you.) A vector yy is equivalent to ∑ieiyi\sum_i \mathbf e_i y_i where ei\mathbf e_i are columns of the identity. Meanwhile, x=A−1yx = A^{-1} y means that we are expressing that same vector yy in the basis of the columns of AA, i.e., ∑iA:,ixi\sum_i A_{:,i} x_i.

using LinearAlgebra
A = rand(4, 4)
4×4 Matrix{Float64}: 0.581397 0.0432775 0.759339 0.35374 0.392727 0.407303 0.340973 0.109206 0.651127 0.179339 0.497567 0.695352 0.1031 0.794159 0.680134 0.842037
A / A
4×4 Matrix{Float64}: 1.0 0.0 0.0 -0.0 0.0 1.0 -1.01321e-17 -3.29056e-18 -8.19434e-17 -1.43118e-16 1.0 2.90336e-17 0.0 0.0 0.0 1.0
A * inv(A)
4×4 Matrix{Float64}: 1.0 6.90195e-17 -2.96793e-16 5.00129e-17 5.84087e-17 1.0 1.03444e-17 2.05469e-16 2.15014e-16 -9.61318e-17 1.0 2.11968e-16 1.38784e-16 -3.78035e-17 -1.99258e-16 1.0

Inner products and orthogonality

The inner product

xTy=∑ixiyix^T y = \sum_i x_i y_i

of vectors (or columns of a matrix) tell us about their magnitude and about the angle. The norm is induced by the inner product,

∥x∥=xTx\lVert x \rVert = \sqrt{x^T x}

and the angle θ\theta is defined by

cos⁡θ=xTy∥x∥ ∥y∥.\cos \theta = \frac{x^T y}{\lVert x \rVert \, \lVert y \rVert} .

Inner products are bilinear, which means that they satisfy some convenient algebraic properties

(x+y)Tz=xTz+yTzxT(y+z)=xTy+xTz(αx)T(βy)=αβxTy.\begin{split} (x + y)^T z &= x^T z + y^T z \\ x^T (y + z) &= x^T y + x^T z \\ (\alpha x)^T (\beta y) &= \alpha \beta x^T y \\ \end{split} .

Examples with inner products

x = [0, 1]
y = [1, 2]
@show x' * y
@show y' * x;
x' * y = 2
y' * x = 2
ϕ = pi/6
y = [cos(ϕ), sin(ϕ)]
@show x' * y
cos_θ = x'*y / (norm(x) * norm(y))
@show cos_θ
@show cos(ϕ-pi/2);
acosd(cos_θ)
x' * y = 0.49999999999999994
cos_θ = 0.49999999999999994
cos(ϕ - pi / 2) = 0.4999999999999999
60.00000000000001

Polynomials can be orthogonal too!

x = LinRange(-1, 1, 50)
A = vander(x, 4)
M = A * [.5 0 0 0; # 0.5
         0  1 0 0;  # x
         0  0 1 0]' # x^2
scatter(x, M, legend=:none)
plot!(x, 0*x, label=:none, color=:black)
Loading...
  • Which inner product will be zero?

    • Which functions are even and odd?

Polynomial inner products

M[:,1]' * M[:,2]
-2.220446049250313e-16
acosd(M[:,1]' * M[:,3] / (norm(M[:,1]) * norm(M[:,3])))
41.79321606483336
M[:,2]' * M[:,3]
-4.440892098500626e-16

Geometry of Linear Algebra

We’d like to develop intuition about transformations on vectors. We’ll start with a peanut-shaped cluster of points and see how they transform.

default(aspect_ratio=:equal)

function peanut()
    theta = LinRange(0, 2*pi, 50)
    r = 1 .+ .4*sin.(3*theta) + .6*sin.(2*theta)
    x = r .* cos.(theta)
    y = r .* sin.(theta)
    x, y
end
x, y = peanut()
scatter(x, y)
Loading...

We’ll group all these points into a 2×n2\times n matrix XX. Note that multiplication by any matrix AA is applied to each column separately, i.e.,

A[x1∣x2∣⋯]⏟X=[Ax1∣Ax2∣⋯]⏟AXA \underbrace{\Bigg[ \mathbf x_1 \Bigg| \mathbf x_2 \Bigg| \dotsb \Bigg]}_X = \underbrace{\Bigg[ A \mathbf x_1 \Bigg| A \mathbf x_2 \Bigg| \dotsb \Bigg]}_{AX}
X = [x y]'
size(X)
(2, 50)

Inner products and projections

Consider the operation f(x)=v(vTx)f(x) = v (v^T x) for some vector vv. We’ll apply this same operation to every point in the original cluster XX.

function tplot(X, Y)
    "Plot a transformation from X to Y"
    scatter(X[1,:], X[2,:], label="X")
    scatter!(Y[1,:], Y[2,:], label="Y")
end

v = [1, 2]
tplot(X, v * (v' * X))
Loading...

Are the parentheses important? Why or why not?

v(vTX)=?vvTXv (v^T X) \overset?= v v^T X

Questions

v = [1, 1] / sqrt(2)

# What happens if v is not normalized?
# Discuss with your group and make a prediction before uncommenting and running the line below.
tplot(X,  v * v' * X)
Loading...
# Vector transformations are "nice" if norm(v) = 1

theta = pi * .3
v = [cos(theta), sin(theta)]
@show norm(v)
#tplot(X, (I - v * v') * X)
tplot(X, (I - v * v') * v * v' * X)
norm(v) = 1.0
Loading...

Compare to the operation I−vvTI - v v^T. Note that (I−vvT)X=X−vvTX(I - v v^T) X = X - v v^T X.

tplot(X, X - v * v' * X)

# This also works since I is the automatically-sized identity.
#tplot(X, (I - v * v') * X)
Loading...

Discuss

Discuss with your neighbor what the vector vv looks like in these diagrams.

  • What is the null space of each transformation?

  • What is the range of each transformation?

  • For each figure, what happens if you apply the transformation vvTv v^T to the red points?

  • What about I−vvTI - v v^T?

Givens Rotation

We can rotate the input using a 2×22\times 2 matrix, parametrized by θ\theta.

function givens(theta)
    s = sin(theta)
    c = cos(theta)
    A = [c -s; s c]
    A
end

givens(0)
2×2 Matrix{Float64}: 1.0 -0.0 0.0 1.0
G = givens(0.2*π)
Gn = givens(-0.2*π)
G' * G
2×2 Matrix{Float64}: 1.0 0.0 0.0 1.0
tplot(X, givens(.8) * X)
Loading...
  • What is the null space and range of the Givens matrix?

  • Is the Givens matrix orthogonal? What does that mean?

Reflection

Using a construct of the form I−αvvTI - \alpha v v^T, create a reflection across the plane defined by unit normal vector v=[cos⁡θ,sin⁡θ]v = [\cos\theta, \sin\theta]. Try different values of α\alpha and convince yourself of why it works or sketch a diagram for the projections vvTv v^T and I−vvTI - v v^T and decide how to modify it to make a reflection.

function reflect(theta)
    v = [cos(theta), sin(theta)]
    I - 2 * v * v'
end

tplot(X, reflect(pi/2) * X)
Loading...
using Test
@test mapslices(norm, X; dims=1) ≈ mapslices(norm, reflect(0.3) * X; dims=1)
@test X ≈ reflect(0.5) * reflect(0.5) * X
Test Passed
  • Where is the vector vv on the figure above?

  • Does the reflection matrix change if you replace v(θ) v(\theta) with v(θ+π)v(\theta + \pi)? Why or why not?

  • What happens if you reflect twice?

  • What is the null space and range of the reflection matrix?

  • Is the reflection matrix orthogonal?