2023-02-15 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=Ax is a linear combination of the columns of A. The familiar definition,

yi=jAi,jxj

can also be viewed as

y=[A:,1|A:,2|][x1x2]=[A:,1]x1+[A:,2]x2+.

Inner products and orthogonality#

The inner product

xTy=ixiyi
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
and the angle θ is defined by
cosθ=xTyxy.
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.

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)
../_images/2023-02-15-projections_8_0.svg
  • 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)
../_images/2023-02-15-projections_15_0.svg

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

A[x1|x2|]X=[Ax1|Ax2|]AX
X = [x y]'
size(X)
(2, 50)

Inner products and projections#

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

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, 0]
tplot(X, v * (v' * X))
../_images/2023-02-15-projections_19_0.svg

Are the parentheses important? Why or why not?

v(vTX)=?vvTX

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)
../_images/2023-02-15-projections_22_0.svg
# Vector transformations are "nice" if norm(v) = 1

theta = pi * .7
v = [cos(theta), sin(theta)]
@show norm(v)
tplot(X, v * v' * X)
norm(v) = 0.9999999999999999
../_images/2023-02-15-projections_23_1.svg

Discuss#

Discuss with your group what the vector v 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 vvT to the red points?

  • What about IvvT?

Givens Rotation#

We can rotate the input using a 2×2 matrix, parametrized by θ.

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
givens(1*π)
2×2 Matrix{Float64}:
 -1.0          -1.22465e-16
  1.22465e-16  -1.0
tplot(X, givens(.8) * X)
../_images/2023-02-15-projections_30_0.svg
  • 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αvvT, create a reflection across the plane defined by unit normal vector v=[cosθ,sinθ]. Try different values of α and convince yourself of why it works or sketch a diagram for the projections vvT and IvvT and decide how to modify it to make a reflection.

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

tplot(X, reflect(pi/2) * X)
../_images/2023-02-15-projections_33_0.svg
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
  Expression: X ≈ reflect(0.5) * reflect(0.5) * X
   Evaluated: [1.0 1.291607690975099 … 0.6919723366713914 0.9999999999999993; 0.0 0.16653437041080127 … -0.08921995295821092 -2.449293598294705e-16] ≈ [1.0 1.291607690975099 … 0.6919723366713914 0.9999999999999993; 0.0 0.16653437041080127 … -0.08921995295821092 -2.449293598294705e-16]
  • Where is the vector v on the figure above?

  • Does the reflection matrix change if you replace v(θ) with v(θ+π)? 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?