Decompositions¶
PLU: Linear Solve (used by )
QR: Least squares, usefule for linear solve (more reliable, but slower than PLU)
Copyright By PowCoder代写 加微信 powcoder
Cholesky: Only works for positive definite, useful for linear solve (faster)
SVD: low rank approximation, nullspace, defined 2-norm, we did not show how to compute (too advanced)
using LinearAlgebra
# PLU, linear solve
A = randn(n,n)
L,U,p = lu(A)
# Solve A*x = b, i.e., x = A\b, using lu
b = rand(n)
U\(L\b[p]) == A\b
P = I(n)[p,:]
U\(L\(P*b)) == A\b
A ≈ P’*L*U
# QR linear solve
Q, R = qr(A)
norm(R\(Q’*b) – A\b)
3.3422138886441676e-16
cholesky(A) # fails because not symmetric
PosDefException: matrix is not Hermitian; Cholesky factorization failed.
Stacktrace:
[1] checkpositivedefinite(info::Int64)
@ LinearAlgebra /Applications/Julia-1.7.app/Contents/Resources/julia/share/julia/stdlib/v1.7/LinearAlgebra/src/factorization.jl:18
[2] cholesky!(A::Matrix{Float64}, ::Val{false}; check::Bool)
@ LinearAlgebra /Applications/Julia-1.7.app/Contents/Resources/julia/share/julia/stdlib/v1.7/LinearAlgebra/src/cholesky.jl:295
[3] #cholesky#143
@ /Applications/Julia-1.7.app/Contents/Resources/julia/share/julia/stdlib/v1.7/LinearAlgebra/src/cholesky.jl:394 [inlined]
[4] cholesky (repeats 2 times)
@ /Applications/Julia-1.7.app/Contents/Resources/julia/share/julia/stdlib/v1.7/LinearAlgebra/src/cholesky.jl:394 [inlined]
[5] top-level scope
@ In[26]:1
@ ./boot.jl:373 [inlined]
[7] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)
@ Base ./loading.jl:1196
S = Symmetric(A)
cholesky(S) # fails because not positive definite
PosDefException: matrix is not positive definite; Cholesky factorization failed.
Stacktrace:
[1] checkpositivedefinite
@ /Applications/Julia-1.7.app/Contents/Resources/julia/share/julia/stdlib/v1.7/LinearAlgebra/src/factorization.jl:18 [inlined]
[2] cholesky!(A::Symmetric{Float64, Matrix{Float64}}, ::Val{false}; check::Bool)
@ LinearAlgebra /Applications/Julia-1.7.app/Contents/Resources/julia/share/julia/stdlib/v1.7/LinearAlgebra/src/cholesky.jl:266
[3] #cholesky#143
@ /Applications/Julia-1.7.app/Contents/Resources/julia/share/julia/stdlib/v1.7/LinearAlgebra/src/cholesky.jl:394 [inlined]
[4] cholesky (repeats 2 times)
@ /Applications/Julia-1.7.app/Contents/Resources/julia/share/julia/stdlib/v1.7/LinearAlgebra/src/cholesky.jl:394 [inlined]
[5] top-level scope
@ In[28]:2
@ ./boot.jl:373 [inlined]
[7] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)
@ Base ./loading.jl:1196
eigvals(S) # negative eigenvalues means not positive definite, but computing eigenvalues is _expansive_
5-element Vector{Float64}:
-2.755386221006669
-1.4496784592955416
0.6251637425474665
2.2513537236585375
3.823625240753941
L = cholesky(S + 10I).L
L’\(L\b) ≈ (S+10I)\b
SVD for images (images non-examinable)¶
using Images, TestImages
img = testimage(“camerman”)
┌ Warning: “camerman” not found in `TestImages.remotefiles`. Load “cameraman.tif” instead.
└ @ TestImages /Users/sheehanolver/.julia/packages/TestImages/OjQVx/src/TestImages.jl:134
Float64.(img) # numbers between 0 and 1, 0 being black, 1 being white
512×512 Matrix{Float64}:
0.611765 0.615686 0.627451 0.623529 … 0.596078 0.596078 0.596078
0.611765 0.615686 0.623529 0.619608 0.596078 0.596078 0.596078
0.619608 0.615686 0.611765 0.611765 0.596078 0.596078 0.596078
0.627451 0.615686 0.603922 0.603922 0.6 0.596078 0.596078
0.619608 0.615686 0.611765 0.611765 0.6 0.596078 0.596078
0.611765 0.615686 0.623529 0.623529 … 0.6 0.6 0.6
0.619608 0.615686 0.611765 0.611765 0.6 0.6 0.6
0.627451 0.615686 0.603922 0.603922 0.6 0.6 0.6
0.619608 0.607843 0.6 0.6 0.596078 0.596078 0.596078
0.611765 0.603922 0.6 0.6 0.592157 0.596078 0.596078
0.607843 0.603922 0.603922 0.6 … 0.588235 0.596078 0.596078
0.607843 0.607843 0.607843 0.603922 0.592157 0.596078 0.596078
0.607843 0.603922 0.6 0.6 0.592157 0.588235 0.588235
⋮ ⋱ ⋮
0.509804 0.490196 0.466667 0.466667 … 0.364706 0.380392 0.384314
0.443137 0.462745 0.482353 0.482353 0.360784 0.392157 0.4
0.396078 0.431373 0.462745 0.462745 0.403922 0.4 0.403922
0.384314 0.415686 0.447059 0.447059 0.458824 0.419608 0.411765
0.435294 0.439216 0.443137 0.45098 0.470588 0.447059 0.439216
0.494118 0.47451 0.454902 0.466667 … 0.478431 0.470588 0.466667
0.47451 0.482353 0.490196 0.490196 0.486275 0.458824 0.45098
0.447059 0.486275 0.521569 0.509804 0.490196 0.443137 0.431373
0.454902 0.482353 0.509804 0.501961 0.482353 0.439216 0.427451
0.47451 0.482353 0.494118 0.494118 0.47451 0.443137 0.435294
0.47451 0.482353 0.494118 0.494118 … 0.47451 0.443137 0.435294
0.47451 0.482353 0.494118 0.494118 0.47451 0.443137 0.435294
U,σ,V = svd(Float64.(img))
Gray.(U[:,1:r] * Diagonal(σ[1:r]) * V[:,1:r]’)
# 512 x 512 pixels, original storage:
# low rank is 2 * 512 * r storage
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com