using LinearAlgebra
function fullsvd(A)
U,s,V = svd(A, full = true) # compute svd
Σ = zeros(size(A)) # container for Σ
for i=1:length(s)
Σ[i,i] = s[i] # place singular values in Σ
end # a practical svd would never store all these zeros
display(U);display(Σ);display(V) # display the answer
return(U,Σ,V) # return the answer
end
function rankrsvd(A)
U,s,V = svd(A, full = true) # compute svd
r = sum(s.>1e-8) # rank = how many positive?
U₁ = U[:,1:r]
Σᵣ = Diagonal(s[1:r]) # Diagonal matrix of singular values
V₁ = V[:,1:r]
display(U₁);display(Σᵣ);display(V₁) # display the answer
return(U₁,Σᵣ,V₁) # return the answer
end
rankrsvd (generic function with 1 method)
A = rand(2,2)
2×2 Array{Float64,2}: 0.259439 0.075927 0.898109 0.918728
fullsvd(A);
2×2 Array{Float64,2}: -0.18222 -0.983258 -0.983258 0.18222
2×2 Array{Float64,2}: 1.30643 0.0 0.0 0.13025
2×2 Adjoint{Float64,Array{Float64,2}}: -0.712128 -0.70205 -0.70205 0.712128
rankrsvd(A);
2×2 Array{Float64,2}: -0.18222 -0.983258 -0.983258 0.18222
2×2 Diagonal{Float64,Array{Float64,1}}: 1.30643 ⋅ ⋅ 0.13025
2×2 Array{Float64,2}: -0.712128 -0.70205 -0.70205 0.712128
A = rand(3,2)
3×2 Array{Float64,2}: 0.464581 0.051883 0.9702 0.533329 0.601868 0.413574
fullsvd(A);
3×3 Array{Float64,2}: -0.311792 0.90137 0.300529 -0.794641 -0.0739732 -0.602555 -0.520894 -0.426685 0.73933
3×2 Array{Float64,2}: 1.39313 0.0 0.0 0.191691 0.0 0.0
2×2 Adjoint{Float64,Array{Float64,2}}: -0.882421 0.47046 -0.47046 -0.882421
rankrsvd(A);
3×2 Array{Float64,2}: -0.311792 0.90137 -0.794641 -0.0739732 -0.520894 -0.426685
2×2 Diagonal{Float64,Array{Float64,1}}: 1.39313 ⋅ ⋅ 0.191691
2×2 Array{Float64,2}: -0.882421 0.47046 -0.47046 -0.882421
A = rand(2,3)
2×3 Array{Float64,2}: 0.0451975 0.242917 0.405185 0.477637 0.8663 0.725397
fullsvd(A);
2×2 Array{Float64,2}: -0.337277 -0.941405 -0.941405 0.337277
2×3 Array{Float64,2}: 1.30125 0.0 0.0 0.0 0.191821 0.0
3×3 Adjoint{Float64,Array{Float64,2}}: -0.357268 0.618008 -0.700304 -0.689699 0.331038 0.643994 -0.629821 -0.713078 -0.30797
rankrsvd(A);
2×2 Array{Float64,2}: -0.337277 -0.941405 -0.941405 0.337277
2×2 Diagonal{Float64,Array{Float64,1}}: 1.30125 ⋅ ⋅ 0.191821
3×2 Array{Float64,2}: -0.357268 0.618008 -0.689699 0.331038 -0.629821 -0.713078
A = rand(7,3)*rand(3,10) # this should be rank 3
7×10 Array{Float64,2}: 0.409585 0.550866 0.729917 0.396048 … 0.491844 0.870667 0.327258 0.824315 0.457974 0.887333 0.932895 0.34797 1.19361 0.266938 0.897034 0.338666 0.678985 0.943025 0.418739 1.0996 0.258406 0.862495 0.661816 0.926009 0.816466 0.771865 1.33408 0.463019 0.878864 0.887087 1.42046 0.99785 0.602456 1.66596 0.472172 0.765583 0.423397 0.731421 0.800129 … 0.446203 1.06758 0.287248 0.710849 0.713791 1.12556 0.793167 0.511149 1.33563 0.388595
fullsvd(A);
7×7 Array{Float64,2}: -0.277937 -0.522311 -0.0744039 … 0.466151 -0.560653 0.264395 -0.34423 0.518096 -0.0720404 -0.227442 -0.460716 -0.319634 -0.335511 0.428918 0.464644 0.604044 0.0486161 -0.0705107 -0.436358 -0.497343 0.506287 -0.350516 0.117976 -0.410459 -0.487624 0.0167782 -0.553854 -0.204899 -0.105282 -0.000703896 -0.327734 0.157075 0.264594 … -0.34661 0.116364 0.806582 -0.395088 -0.0499189 -0.374552 0.284817 0.657649 -0.0625039
7×10 Array{Float64,2}: 6.92226 0.0 0.0 0.0 … 0.0 0.0 0.0 0.0 0.0 0.755198 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.683053 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 5.91088e-16 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 … 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 8.21229e-17 0.0 0.0 0.0
10×10 Adjoint{Float64,Array{Float64,2}}: -0.294011 0.35548 0.312083 … 0.0465244 0.0514862 0.039223 -0.2263 -0.249712 -0.334077 0.181558 0.595424 0.0233641 -0.363646 -0.0109916 -0.510499 -0.0359443 -0.220601 0.140677 -0.312911 0.500153 0.171036 -0.359502 -0.207792 0.0736223 -0.237955 -0.64866 0.255552 -0.601864 -0.131402 -0.104849 -0.251608 0.0397458 -0.437401 … 0.164673 -0.485916 -0.221939 -0.477004 -0.0809271 0.436915 0.478804 0.100636 0.0399163 -0.198741 -0.299539 0.17074 0.373694 -0.353637 -0.236875 -0.475836 0.133415 -0.153594 -0.275708 0.389516 -0.22132 -0.137166 -0.156822 -0.0295032 0.00811763 -0.103379 0.897647
rankrsvd(A);
7×3 Array{Float64,2}: -0.277937 -0.522311 -0.0744039 -0.34423 0.518096 -0.0720404 -0.335511 0.428918 0.464644 -0.436358 -0.497343 0.506287 -0.487624 0.0167782 -0.553854 -0.327734 0.157075 0.264594 -0.395088 -0.0499189 -0.374552
3×3 Diagonal{Float64,Array{Float64,1}}: 6.92226 ⋅ ⋅ ⋅ 0.755198 ⋅ ⋅ ⋅ 0.683053
10×3 Array{Float64,2}: -0.294011 0.35548 0.312083 -0.2263 -0.249712 -0.334077 -0.363646 -0.0109916 -0.510499 -0.312911 0.500153 0.171036 -0.237955 -0.64866 0.255552 -0.251608 0.0397458 -0.437401 -0.477004 -0.0809271 0.436915 -0.198741 -0.299539 0.17074 -0.475836 0.133415 -0.153594 -0.137166 -0.156822 -0.0295032