Last time¶
Forward and backward stability
Beyond IEEE double precision
Mixed-precision algorithms
Today¶
Algebra of linear transformations
Polynomial evaluation and fitting
Orthogonality
using Plots
default(linewidth=4, legendfontsize=12)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 is a linear combination of the columns of . The familiar definition,
can also be viewed as
Math and Julia Notation¶
The notation corresponds to the Julia syntax A[i,j] and the colon : means the entire range (row or column). So is the th column and is the th row. The corresponding Julia syntax is A[:,j] and A[i,:].
Julia has syntax for row vectors, column vectors, and arrays.
[1. 2 3; 4 5 6]
# np.array([[1, 2, 3], [4, 5, 6]])2×3 Matrix{Float64}:
1.0 2.0 3.0
4.0 5.0 6.0[1 2 ; 4 3]2×2 Matrix{Int64}:
1 2
4 3[1 0; 0 2; 10 3]3×2 Matrix{Int64}:
1 0
0 2
10 3@show transpose([1; 2 + 1im; 3])
[1; 2 + 1im; 3]' # adjointtranspose([1; 2 + 1im; 3]) = transpose(Complex{Int64}[1 + 0im, 2 + 1im, 3 + 0im])
1×3 adjoint(::Vector{Complex{Int64}}) with eltype Complex{Int64}:
1+0im 2-1im 3+0imImplementing multiplication by row¶
function matmult1(A, x)
m, n = size(A)
y = zeros(m)
for i in 1:m
for j in 1:n
y[i] += A[i,j] * x[j]
end
end
y
end
A = reshape(1.:12, 3, 4) # 3x4 matrix
x = [10., 0, 0, 0]
matmult1(A, x)3-element Vector{Float64}:
10.0
20.0
30.0# Dot product
A[2, :]' * x
# dot(A[2,:], x)20.0function matmult2(A, x)
m, n = size(A)
y = zeros(m)
for i in 1:m
y[i] = A[i,:]' * x
end
y
end
matmult2(A, x)3-element Vector{Float64}:
10.0
20.0
30.0Implementing multiplication by column¶
function matmult3(A, x)
m, n = size(A)
y = zeros(m)
for j in 1:n
y += A[:, j] * x[j]
end
y
end
matmult3(A, x)3-element Vector{Float64}:
10.0
20.0
30.0A * x # We'll use this version 3-element Vector{Float64}:
10.0
20.0
30.0Polynomial evaluation is (continuous) linear algebra¶
We can evaluate polynomials using matrix-vector multiplication. For example,
using Polynomials
P(x) = Polynomial(x)
p = [0, -3, 0, 5]
q = [1, 2, 3, 4]
f = P(p) + P(q)
@show f
@show P(p+q)
x = [0., 1, 2]
f.(x)f = 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.0plot(f, legend=:bottomright, xlim=(-2, 2))Polynomial evaluation is (discrete) linear algebra¶
V = [one.(x) x x.^2 x.^3]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.0V * p + V * q3-element Vector{Float64}:
1.0
12.0
83.0V * (p + q)3-element Vector{Float64}:
1.0
12.0
83.0Vandermonde matrices¶
A Vandermonde matrix is one whose columns are functions evaluated at discrete points.
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
endvander (generic function with 2 methods)x = LinRange(-1, 1, 50)
V = vander(x, 4)
scatter(x, V, legend=:bottomright)x1 = [-.9, 0.1, .5, .8]
y1 = [1, 2.4, -.2, 1.3]
scatter(x1, y1, markersize=8)V = vander(x1)
@show size(V)
p = V \ y1 # write y1 in the polynomial basis
scatter(x1, y1, markersize=8, xlims=(-1, 1))
#plot!(P(p), label="P(p)" )
plot!(x, vander(x, 4) * p, label="\$ V(x) p\$", linestyle=:dash)size(V) = (4, 4)
Some common terminology¶
The range of is the space spanned by its columns. This definition coincides with the range of a function when .
The (right) nullspace of is the space of vectors such that .
The rank of is the dimension of its range.
A matrix has full rank if the nullspace of either or is empty (only the 0 vector). Equivalently, if all the columns of (or ) are linearly independent.
A nonsingular (or invertible) matrix is a square matrix of full rank. We call the inverse and it satisfies .
\DeclareMathOperator{\rank}{rank} \DeclareMathOperator{\null}{null} If , which of these doesn’t belong?
has an inverse
\rank (A) = m
\null(A) = \{0\}
implies that
A = rand(4, 4)
B = A' * A - A * A'
@show norm(B)
det(A)norm(B) = 2.3667612155830358
0.020772845265188848What is an inverse?¶
When we write , we mean that is the unique vector such that . (It is rare that we explicitly compute a matrix , though it’s not as “bad” as people may have told you.) A vector is equivalent to where are columns of the identity. Meanwhile, means that we are expressing that same vector in the basis of the columns of , i.e., .
using LinearAlgebra
A = rand(4, 4)4×4 Matrix{Float64}:
0.131719 0.388988 0.937221 0.782611
0.144091 0.402657 0.448666 0.608461
0.609457 0.675322 0.451023 0.407834
0.942562 0.886168 0.18919 0.700531A \ A4×4 Matrix{Float64}:
1.0 0.0 -1.65884e-16 -2.01201e-16
-4.731e-16 1.0 2.07761e-16 3.273e-16
-1.398e-16 -1.74751e-17 1.0 5.61582e-17
3.69161e-16 4.61452e-17 -0.0 1.0inv(A) * A4×4 Matrix{Float64}:
1.0 3.53342e-16 -4.32371e-16 -2.36207e-16
-3.03796e-16 1.0 1.52398e-16 1.05071e-15
-7.10794e-17 2.81874e-17 1.0 -1.33911e-16
2.2304e-16 -2.634e-17 5.66022e-17 1.0Inner products and orthogonality¶
The inner product
of vectors (or columns of a matrix) tell us about their magnitude and about the angle. The norm is induced by the inner product,
and the angle is defined by
Inner products are bilinear, which means that they satisfy some convenient algebraic properties
Examples with inner products¶
x = [0, 1]
y = [1, 1]
@show x' * y
@show y' * x;x' * y = 1
y' * x = 1
ϕ = pi/6
y = [cos(ϕ), sin(ϕ)]
cos_θ = x'*y / (norm(x) * norm(y))
@show cos_θ
@show cos(ϕ-pi/2);cos_θ = 0.49999999999999994
cos(ϕ - pi / 2) = 0.4999999999999999
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)
plot!(x, 0*x, label=:none, color=:black)Which inner product will be zero?
Which functions are even and odd?
Polynomial inner products¶
M[:,1]' * M[:,2]-2.220446049250313e-16M[:,1]' * M[:,3]8.673469387755102M[:,2]' * M[:,3]-4.440892098500626e-16