using LinearAlgebra: svd, Diagonal, norm, opnorm, eigen
using Interact # @manipulate
using Plots: plot, plot!, default
default(markerstrokecolor=:auto)
using LaTeXStrings
The WebIO Jupyter extension was not detected. See the WebIO Jupyter integration documentation for more information.
A = [2 1; 0 3] # asymmetric matrix - no problem for SVD!
#A = [1 1; 0 0] # rank1 matrix: u2 is not in range of A
#A = [2 1; 0 2] # defective (not diagonalizable)
#A = [cos(π/3) sin(π/3); -sin(π/3) cos(π/3)] # rotation
2×2 Matrix{Int64}: 2 1 0 3
U, s, V = svd(A)
display(U)
display(V)
display(U * Diagonal(s) * V') # equals A, almost
@show s;
2×2 Matrix{Float64}: 0.471858 -0.881675 0.881675 0.471858
2×2 adjoint(::Matrix{Float64}) with eltype Float64: 0.289784 -0.957092 0.957092 0.289784
2×2 Matrix{Float64}: 2.0 1.0 1.11022e-16 3.0
s = [3.25661653798294, 1.8424029756098448]
Φ = range(0, 2π, 181)
xc = [cos.(Φ) sin.(Φ)]' # for drawing unit circle
yc = A * xc
# angles = (π/4, π/2) # generic initialization
angles = mod.(atan.(V[2,:] ./ V[1,:]), π) # start by showing right sing. vectors
@manipulate for
angle1 in slider(Φ, value=angles[1]),
angle2 in slider(Φ, value=angles[2])
x1 = [cos(angle1), sin(angle1)] # 1st vector
x2 = [cos(angle2), sin(angle2)] # 2nd vector
y1 = A * x1 # matrix-vector multiplication of interest
y2 = A * x2
plot(xc[1,:], xc[2,:], line=(:black), label="",
xlabel=L"x_1", ylabel=L"x_2",
xlim=(-1.1, 1.1) .* norm(A), xtick=-4:4,
ylim=(-1.1, 1.1) .* norm(A), ytick=-4:4,
legend=:bottomright, aspect_ratio=:equal)
plot!(yc[1,:], yc[2,:], line=(:green,:dash), label="")
plot!([0, x1[1]], [0, x1[2]], line=(:blue),
markershape=:rect, markercolor=:blue, label="x1")
plot!([0, y1[1]], [0, y1[2]], line=(:blue,:dash),
markershape=:circle, markercolor=:blue, label="y1 = A x1")
plot!([0, x2[1]], [0, x2[2]], line=(:red),
markershape=:rect, markercolor=:red, label="x2")
plot!([0, y2[1]], [0, y2[2]], line=(:red,:dash),
markershape=:circle, markercolor=:red, label="y2 = A x2")
end
plot!() # for html
#savefig("tmp.pdf")
U[:,1]*s[1], A * V[:,1], U[:,2]*s[2], A * V[:,2]
([1.536660323865913, 2.8712760794671586], [1.5366603238659127, 2.8712760794671577], [-1.6243999042896753, 0.86935244606529], [-1.624399904289675, 0.8693524460652899])
norm(A), opnorm(A)
(3.7416573867739413, 3.2566165379829393)
eigen(A)
LinearAlgebra.Eigen{Float64, Float64, Matrix{Float64}, Vector{Float64}} values: 2-element Vector{Float64}: 2.0 3.0 vectors: 2×2 Matrix{Float64}: 1.0 0.707107 0.0 0.707107
sign(10*exp(im*π/4)), sign(0)
(0.7071067811865476 + 0.7071067811865475im, 0)