Untitled14
Copyright By PowCoder代写 加微信 powcoder
using LinearAlgebra, Plots
m,n = 10,5
A = randn(m,n)
U, σ, V = svd(A)
Σ = Diagonal(σ)
norm(U*Σ*V’ – A)
7.30870483561447e-15
opnorm(A, 2)
4.7467503873992145
4.746750387399214
norm(A), norm(vec(A))
(7.04035069114532, 7.04035069114532)
m,n = 100,100
A = randn(m,n)
U, σ, V = svd(A)
Σ = Diagonal(σ)
scatter(σ)
k = 50 # rank 50
opnorm(A – U[:,1:k] * Diagonal(σ[1:k]) * V[:,1:k]’)
7.877258159796626
7.877258159796627
function hilbertmatrix(m, n)
ret = zeros(m, n)
for j = 1:n, k=1:m
ret[k,j] = 1/(k+j-1)
hilbertmatrix(4,5)
4×5 Matrix{Float64}:
1.0 0.5 0.333333 0.25 0.2
0.5 0.333333 0.25 0.2 0.166667
0.333333 0.25 0.2 0.166667 0.142857
0.25 0.2 0.166667 0.142857 0.125
scatter(svdvals(hilbertmatrix(1000,1001)); yscale=:log10)
H = hilbertmatrix(1000,1001)
U,σ,V = svd(H)
H_k = U[:,1:k] * Diagonal(σ[1:k]) * V[:,1:k]’
opnorm(H_k – H)
4.940002559405851e-8
# H uses 1000*1001 = 1_001_000 floats to store
# H_k uses 1000*15 + 15 + 1001*15 = 30_030 floats
1000*15 + 15 + 1001*15
### WARNING svd _never_ assumes you have a kernel so r = min(m,n)
A = [1 2 3;
3×3 Matrix{Int64}:
svdvals(A)
3-element Vector{Float64}:
16.84810335261421
1.0683695145547103
4.418424751193367e-16
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com