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.

16) Linear Algebra

Today

  1. Matrices as linear transformations

  2. Polynomial evaluation and fitting

  3. Orthogonality

1. 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 .

That is, a linear combination of the columns of A.

Math and Julia Notation

The notation Ai,jA_{i,j} corresponds to the Julia syntax A[i,j] and the colon : means the entire range (row or column). So A:,jA_{:,j} is the jj-th column and Ai,:A_{i,:} is the ii-th row.

The corresponding Julia syntax is A[:,j] and A[i,:].

Julia has syntax for row vectors, column vectors, and arrays.

1×3 Matrix{Int64}: 1 2 3
3-element Vector{Int64}: 1 2 3
2×3 Matrix{Float64}: 1.0 2.0 3.0 4.0 5.0 6.0
2×2 Matrix{Int64}: 1 2 4 3
3×2 Matrix{Int64}: 1 0 0 2 10 3
1×3 adjoint(::Vector{Complex{Int64}}) with eltype Complex{Int64}: 1+0im 2-1im 3+0im

Implementing multiplication by row

matmult1 (generic function with 1 method)
3×4 reshape(::StepRangeLen{Float64, Base.TwicePrecision{Float64}, Base.TwicePrecision{Float64}, Int64}, 3, 4) with eltype Float64: 1.0 4.0 7.0 10.0 2.0 5.0 8.0 11.0 3.0 6.0 9.0 12.0
4-element Vector{Float64}: 10.0 0.0 0.0 0.0
3-element Vector{Float64}: 10.0 20.0 30.0
20.0
3-element Vector{Float64}: 10.0 20.0 30.0

Curiosity: which one is faster? Which one takes more allocations?

  0.000012 seconds (1 allocation: 80 bytes)
  0.000016 seconds (4 allocations: 368 bytes)
3-element Vector{Float64}: 10.0 20.0 30.0

Implementing multiplication by column

3-element Vector{Float64}: 10.0 20.0 30.0
  0.000011 seconds (13 allocations: 1.016 KiB)
3-element Vector{Float64}: 10.0 20.0 30.0
3-element Vector{Float64}: 10.0 20.0 30.0
  0.000010 seconds (1 allocation: 80 bytes)
3-element Vector{Float64}: 10.0 20.0 30.0

Check the standard operations documentation page.

2. Polynomial evaluation

Polynomial evaluation is (continuous) linear algebra

We can evaluate polynomials using matrix-vector multiplication. For example,

−3x+5x3=[1∣ x∣ x2 ∣ x3][0−305].- 3x + 5x^3 = \Bigg[ 1 \Bigg|\, x \Bigg|\, x^2 \,\Bigg|\, x^3 \Bigg] \begin{bmatrix}0 \\ -3 \\ 0 \\ 5 \end{bmatrix} .
   Resolving package versions...
  No Changes to `~/.julia/environments/v1.10/Project.toml`
  No Changes to `~/.julia/environments/v1.10/Manifest.toml`
f = P(p) = Polynomial(-3*x + 5*x^3)
g = P(q) = Polynomial(1 + 2*x + 3*x^2 + 4*x^3)
h = Polynomial(1 - x + 3*x^2 + 9*x^3)
P(p + q) = 
Polynomial(1 - x + 3*x^2 + 9*x^3)
3-element Vector{Float64}: 1.0 12.0 83.0
Loading...

Polynomial evaluation is (discrete) linear algebra

3×4 Matrix{Float64}: 1.0 0.0 0.0 0.0 1.0 1.0 1.0 1.0 1.0 2.0 4.0 8.0
3-element Vector{Float64}: 1.0 12.0 83.0
3-element Vector{Float64}: 1.0 12.0 83.0

Vandermonde matrices

A Vandermonde matrix is one whose columns are polynomials (monomials) evaluated at discrete points.

V(x)=[1∣x∣x2∣x3∣⋯]V(x) = \begin{bmatrix} 1 \Bigg| x \Bigg| x^2 \Bigg| x^3 \Bigg| \dotsb \end{bmatrix}
vander (generic function with 2 methods)
x = LinRange(-1, 1, 50) = LinRange{Float64}(-1.0, 1.0, 50)
Loading...

Fitting (polynomial interpolation) is linear algebra

[1∣x∣x2∣x3∣⋯]⏟V(x)[p]=[y]\underbrace{\begin{bmatrix} 1 \Bigg| x \Bigg| x^2 \Bigg| x^3 \Bigg| \dotsb \end{bmatrix}}_{V(x)} \Big[ p \Big] = \Bigg[ y \Bigg]
Loading...
size(V) = (4, 4)
Loading...

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.

Poll 15.1: 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\operatorname{rank} (A) = m

  3. null⁡(A)={0}\operatorname{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

   Resolving package versions...
  No Changes to `~/.julia/environments/v1.10/Project.toml`
  No Changes to `~/.julia/environments/v1.10/Manifest.toml`
B = [-0.7524380040011185 -0.13701483574932205 -0.29457515938763557 -0.06864902392835437; -0.13701483574932205 -0.21478294270764864 -0.23531565443422542 -0.06757256692336011; -0.29457515938763557 -0.23531565443422542 0.3627463157922426 0.5864642755136569; -0.06864902392835437 -0.06757256692336011 0.5864642755136569 0.604474630916525]
0.1701663345605843

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 e_i y_i where eie_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.

4×4 Matrix{Float64}: 0.601396 0.455792 0.595225 0.770196 0.482789 0.850813 0.868196 0.267611 0.912719 0.445778 0.0174431 0.807863 0.983887 0.303785 0.251255 0.0804293
4×4 Matrix{Float64}: 1.0 0.0 7.57181e-18 -1.87279e-17 3.62171e-17 1.0 1.58208e-16 3.41974e-16 -3.20193e-18 -0.0 1.0 -2.849e-16 1.42371e-16 0.0 0.0 1.0
4×4 Matrix{Float64}: 1.0 8.68596e-17 8.51652e-17 3.46604e-17 5.16831e-17 1.0 -4.49128e-17 1.46353e-18 -5.61439e-18 -5.3878e-17 1.0 -1.28552e-16 8.08992e-17 -4.39595e-17 8.40137e-17 1.0

3. Orthogonality

The inner product

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

of real vectors (or columns of a matrix) tells 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' * y = 1
y' * x = 1
cos_θ = 0.49999999999999994
cos(ϕ - pi / 2) = 0.4999999999999999

Polynomials can be orthogonal too!

Loading...
  • Which inner product will be zero?

    • Which functions are even and odd?

Polynomial inner products

-2.220446049250313e-16
8.673469387755102
-4.440892098500626e-16