Singular Value Decomposition - Definitions and Facts

Prerequisites

The reader should be familiar with basic linear algebra concepts and notebooks related to eigenvalue decomposition.

Competences

The reader should be able to undestand and check the facts about singular value decomposition.

Selected references

There are many excellent books on the subject. Here we list a few:

J. W. Demmel, Applied Numerical Linear Algebra

G. H. Golub and C. F. Van Loan, Matrix Computations

N. Higham, Accuracy and Stability of Numerical Algorithms

L. Hogben, ed., Handbook of Linear Algebra

G. W. Stewart, Matrix Algorithms, Vol. II: Eigensystems

L. N. Trefethen and D. Bau, III, Numerical Linear Algebra

Singular value problems

For more details and the proofs of the Facts below, see R. C. Li, Matrix Perturbation Theory and R. Mathias, Singular Values and Singular Value Inequalities and the references therein.

Definitions

Let $A\in\mathbb{C}^{m\times n}$ and let $q=\min\{m,n\}$.

The singular value decomposition (SVD) of $A$ is

$$A=U\Sigma V^*,$$

where $U\in\mathbb{C}^{m\times m}$ and $V\in\mathbb{C}^{n\times n}$ are unitary, and $\Sigma=\mathop{\mathrm{diag}}(\sigma_1,\sigma_2,\ldots)\in\mathbb{R}^{m\times n}$ with all $\sigma_j\geq 0$.

Here $\sigma_j$ is the singular value, $u_j\equiv U_{:,j}$ is the corresponding left singular vector, and $v_j\equiv V_{:,j}$ is the corresponding right singular vector.

The set of singular values is $sv(A)=\{\sigma_1,\sigma_2,\ldots,\sigma_{q}\}$.

We assume that singular values are ordered, $\sigma_1\geq\sigma_2\geq\cdots\sigma_q\geq 0$.

The Jordan-Wielandt matrix is the Hermitian matrix $$ J=\begin{bmatrix}0 & A \\ A^* & 0 \end{bmatrix} \in \mathbb{C}^{(m+n) \times (m+n)}. $$

Facts

There are many facts related to the singular value problem for general matrices. We state some basic ones:

  1. If $A\in\mathbb{R}^{m\times n}$, then $U$ and $V$ are real.

  2. Singular values are unique (uniquely determined by the matrix).

  3. $\sigma_j(A^T)=\sigma_j(A^*)=\sigma_j(\bar A)=\sigma_j(A)$ for $j=1,2,\ldots,q$.

  4. $A v_j=\sigma_j u_{j}$ and $A^* u_{j}=\sigma_j v_{j}$ for $j=1,2,\ldots,q$.

  5. $A=\sigma_1 u_{1} v_{1}^* + \sigma_2 u_{2} v_{2}^* +\cdots + \sigma_q u_{q} v_{q}^*$.

  6. Unitary invariance. For any unitary $U\in\mathbb{C}^{m\times m}$ and $V\in\mathbb{C}^{n\times n}$, $sv(A)=sv(UAV)$.

  7. There exist unitary matrices $U\in\mathbb{C}^{m\times m}$ and $V\in\mathbb{C}^{n\times n}$ such that $A=UBV$ if and only if $sv(A)=sv(B)$.

  8. SVD of $A$ is related to eigenvalue decompositions of Hermitian matrices $A^*A=V\Sigma^T\Sigma V^*$ and $AA^*=U\Sigma\Sigma^TU^*$. Thus, $\sigma_j^2(A)=\lambda_j(A^*A)=\lambda_j(AA^*)$ for $j=1,2,\ldots,q$.

  9. The eigenvalues of Jordan-Wielandt matrix are $\pm \sigma_1(A), \pm\sigma_2(A), \cdots,\pm\sigma_q(A)$ together with $|m-n|$ zeros. The eigenvectors are obtained from an SVD of $A$. This relationship is used to deduce singular value results from the results for eigenvalues of Hermitian matrices.

  10. $\mathop{\mathrm{trace}}(|A|_{spr})=\sum_{i=1}^q \sigma_i$, where $|A|_{spr}=(A^*A)^{1/2}$.

  11. If $A$ is square, then $|\det(A)|=\prod_{i=1}^n \sigma_i$.

  12. If $A$ is square, then $A$ is singular $\Leftrightarrow$ $\sigma_j(A)=0$ for some $j$.

  13. Min-max Theorem. It holds: \begin{align*} \sigma_k& =\max_{\dim(W)=k}\min_{x\in W, \ \|x\|_2=1} \|Ax\|_2\\ & =\min_{\dim(W)=n-k+1}\max_{x\in W, \ \|x\|_2=1} \|Ax\|_2. \end{align*}

  14. $\|A\|_2=\sigma_1(A)$.

  15. For $B\in\mathbb{C}^{m\times n}$, $$ |\mathop{\mathrm{trace}}(AB^*)|\leq \sum_{j=1}^q \sigma_j(A)\sigma_j(B). $$

  16. Interlace Theorems. Let $B$ denote $A$ with the one of its rows or columns deleted. Then $$ \sigma_{j+1}(A)\leq \sigma_j(B)\leq \sigma_j(A),\quad j=1,\ldots,q-1. $$ Let $B$ denote $A$ with a row and a column deleted. Then $$ \sigma_{j+2}(A)\leq \sigma_j(B)\leq \sigma_j(A),\quad j=1,\ldots,q-2. $$

  17. Weyl Inequalities. For $B\in\mathbb{C}^{m\times n}$, it holds: \begin{align*} \sigma_{j+k-1}(A+B)& \leq \sigma_j(A)+\sigma_k(B), \quad j+k\leq q+1,\\ \sum_{j=1}^k \sigma_j(A+B)& \leq \sum_{j=1}^k \sigma_j(A) + \sum_{j=1}^k \sigma_j(B), \quad k=1,\ldots,q. \end{align*}

Example - Symbolic computation


In [1]:
using SymPy

In [2]:
A=[  3   2   1
 -5  -1  -4
  5   0   2]


Out[2]:
3×3 Array{Int64,2}:
  3   2   1
 -5  -1  -4
  5   0   2

In [3]:
@vars x


Out[3]:
(x,)

In [4]:
B=A'*A


Out[4]:
3×3 Array{Int64,2}:
 59  11  33
 11   5   6
 33   6  21

In [5]:
using LinearAlgebra

In [6]:
# Characteristic polynomial p_B(λ)
eye(n)=Matrix{Int}(I,n,n)
p(x)=simplify(det(B-x*eye(3)))
p(x)


Out[6]:
\begin{equation*}- x^{3} + 85 x^{2} - 393 x + 441\end{equation*}

In [7]:
λ=solve(p(x),x)


Out[7]:
\[ \left[ \begin{array}{r}3\\41 - \sqrt{1534}\\\sqrt{1534} + 41\end{array} \right] \]

In [8]:
map(Float64,λ)


Out[8]:
3-element Array{Float64,1}:
  3.0
  1.8336879448677221
 80.16631205513228

In [9]:
V=Array{Any}(undef,3,3)
for j=1:3
    V[:,j]=nullspace(B-map(Float64,λ[j])*I)
end
V


Out[9]:
3×3 Array{Any,2}:
 -0.0        0.519818  0.854277
  0.948683  -0.270146  0.164381
 -0.316228  -0.810438  0.493143

In [10]:
U=Array{Any}(undef,3,3)
for j=1:3
    U[:,j]=nullspace(A*A'-map(Float64,λ[j])*I)
end
U


Out[10]:
3×3 Array{Any,2}:
 -0.912871  0.154137   0.378032
 -0.182574  0.67409   -0.71573
  0.365148  0.722388   0.587215

In [11]:
σ=sqrt.(λ)


Out[11]:
\[ \left[ \begin{array}{r}\sqrt{3}\\\sqrt{41 - \sqrt{1534}}\\\sqrt{\sqrt{1534} + 41}\end{array} \right] \]

In [12]:
V'*A'*A*V


Out[12]:
3×3 Array{Any,2}:
  3.0          -2.22045e-16  -2.38698e-15
 -7.21645e-16   1.83369      -6.99441e-15
 -1.77636e-15   0.0          80.1663

In [13]:
V*=Diagonal(sign.(U'*A*V))


Out[13]:
3×3 Array{Any,2}:
  0.0        0.519818  0.854277
 -0.948683  -0.270146  0.164381
  0.316228  -0.810438  0.493143

In [14]:
A-U*Diagonal(map(Float64,σ))*V'


Out[14]:
3×3 Array{Float64,2}:
  4.44089e-16  -8.88178e-16  -3.10862e-15
 -8.88178e-16   8.88178e-16   0.0
 -8.88178e-16   9.99201e-16  -4.44089e-16

In [15]:
S=svd(A)


Out[15]:
SVD{Float64,Float64,Array{Float64,2}}
U factor:
3×3 Array{Float64,2}:
 -0.378032  -0.912871  -0.154137
  0.71573   -0.182574  -0.67409
 -0.587215   0.365148  -0.722388
singular values:
3-element Array{Float64,1}:
 8.95356420958337
 1.7320508075688772
 1.3541373434285466
Vt factor:
3×3 Array{Float64,2}:
 -0.854277  -0.164381  -0.493143
  0.0       -0.948683   0.316228
 -0.519818   0.270146   0.810438

In [16]:
S.Vt


Out[16]:
3×3 Array{Float64,2}:
 -0.854277  -0.164381  -0.493143
  0.0       -0.948683   0.316228
 -0.519818   0.270146   0.810438

In [17]:
typeof(S)


Out[17]:
SVD{Float64,Float64,Array{Float64,2}}

In [18]:
U₁=S.U
σ₁=S.S
V₁=S.V


Out[18]:
3×3 Adjoint{Float64,Array{Float64,2}}:
 -0.854277   0.0       -0.519818
 -0.164381  -0.948683   0.270146
 -0.493143   0.316228   0.810438

In [19]:
A-S.U*Diagonal(S.S)*S.Vt


Out[19]:
3×3 Array{Float64,2}:
 -8.88178e-16  -4.44089e-16  -1.77636e-15
  0.0          -1.11022e-16   1.77636e-15
 -8.88178e-16  -6.66134e-16  -1.77636e-15

Observe the signs of the columns!


In [20]:
V


Out[20]:
3×3 Array{Any,2}:
  0.0        0.519818  0.854277
 -0.948683  -0.270146  0.164381
  0.316228  -0.810438  0.493143

In [21]:
U₁


Out[21]:
3×3 Array{Float64,2}:
 -0.378032  -0.912871  -0.154137
  0.71573   -0.182574  -0.67409
 -0.587215   0.365148  -0.722388

In [22]:
U


Out[22]:
3×3 Array{Any,2}:
 -0.912871  0.154137   0.378032
 -0.182574  0.67409   -0.71573
  0.365148  0.722388   0.587215

Example - Random complex matrix


In [23]:
?svd;

In [24]:
import Random
Random.seed!(421)
m=5
n=3
q=min(m,n)
A=rand(ComplexF64,m,n)


Out[24]:
5×3 Array{Complex{Float64},2}:
 0.345443+0.68487im   0.958365+0.560486im  0.198694+0.854638im
 0.650991+0.973053im  0.608612+0.346561im  0.905889+0.0936446im
 0.105135+0.77247im   0.561248+0.915812im  0.651562+0.37833im
  0.17008+0.525208im  0.605095+0.83639im   0.834811+0.353274im
 0.785847+0.135538im  0.766264+0.810683im  0.831302+0.217897im

In [25]:
U,σ,V=svd(A,full=true)
U


Out[25]:
5×5 Array{Complex{Float64},2}:
 -0.175438-0.396416im    -0.652606+0.0288689im  …  -0.156605-0.114846im
 -0.326593-0.329427im     0.285249+0.417969im       0.116565-0.566605im
 -0.147555-0.426007im  0.000868154-0.118294im      -0.420412+0.282056im
 -0.198077-0.38428im     0.0574047-0.144118im      -0.275862+0.154246im
 -0.336765-0.305483im      0.29689-0.43704im        0.518458-0.0516146im

In [26]:
V


Out[26]:
3×3 Adjoint{Complex{Float64},Array{Complex{Float64},2}}:
  -0.52509-0.0im        0.466117-0.0im         -0.71205-0.0im
 -0.636547-0.150138im  -0.705949-0.264487im  0.00728845-0.0624193im
 -0.481283-0.254781im   0.342947+0.311136im    0.579412+0.391558im

In [27]:
U,σ,V=svd(A)
U


Out[27]:
5×3 Array{Complex{Float64},2}:
 -0.175438-0.396416im    -0.652606+0.0288689im  -0.572856+0.0400237im
 -0.326593-0.329427im     0.285249+0.417969im   0.0686383-0.431971im
 -0.147555-0.426007im  0.000868154-0.118294im    0.291866-0.140757im
 -0.198077-0.38428im     0.0574047-0.144118im    0.379928+0.17032im
 -0.336765-0.305483im      0.29689-0.43704im    -0.144769+0.423807im

In [28]:
σ


Out[28]:
3-element Array{Float64,1}:
 3.335456167848843
 0.8658601013427657
 0.7393073150147738

In [29]:
V


Out[29]:
3×3 Adjoint{Complex{Float64},Array{Complex{Float64},2}}:
  -0.52509-0.0im        0.466117-0.0im         -0.71205-0.0im
 -0.636547-0.150138im  -0.705949-0.264487im  0.00728845-0.0624193im
 -0.481283-0.254781im   0.342947+0.311136im    0.579412+0.391558im

In [30]:
norm(A-U[:,1:q]*Diagonal(σ)*V'), norm(U'*U-I), norm(V'*V-I)


Out[30]:
(1.70922296165275e-15, 1.0290580852698109e-15, 4.1214927040449093e-16)

In [31]:
# Fact 4
@show k=rand(1:q)
norm(A*V[:,k]-σ[k]*U[:,k],Inf), norm(A'*U[:,k]-σ[k]*V[:,k],Inf)


k = rand(1:q) = 2
Out[31]:
(6.210310043996969e-16, 4.910462595695867e-16)

In [32]:
λ₁,V₁=eigen(A'*A)


Out[32]:
Eigen{Complex{Float64},Float64,Array{Complex{Float64},2},Array{Float64,1}}
values:
3-element Array{Float64,1}:
  0.5465753060343534
  0.7497137150973043
 11.125267847640885
vectors:
3×3 Array{Complex{Float64},2}:
 -0.589967+0.398691im   -0.345216+0.313195im  -0.464074+0.245671im
 -0.028911-0.0557983im   0.700555-0.278459im  -0.632825+0.165126im
   0.69931+0.0im        -0.463053-0.0im       -0.544561-0.0im

In [33]:
sqrt.(λ₁)


Out[33]:
3-element Array{Float64,1}:
 0.7393073150147734
 0.8658601013427656
 3.335456167848842

In [34]:
λU,U₁=eigen(A*A')


Out[34]:
Eigen{Complex{Float64},Float64,Array{Complex{Float64},2},Array{Float64,1}}
values:
5-element Array{Float64,1}:
 -2.547330152234144e-16
  1.1537724491561576e-15
  0.5465753060343549
  0.7497137150973037
 11.125267847640888
vectors:
5×5 Array{Complex{Float64},2}:
  -0.218689-0.000658708im  0.00539783+0.0879033im  …  -0.396281-0.175742im
 -0.0554401-0.398117im      -0.216133+0.359381im       -0.46323-0.0245693im
   0.221021+0.572926im       0.443286+0.322733im       -0.39551-0.216393im
  -0.486547-0.26579im        0.269626-0.485391im      -0.404896-0.151543im
   0.325786+0.0im           -0.455428-0.0im           -0.454676-0.0im

In [35]:
V


Out[35]:
3×3 Adjoint{Complex{Float64},Array{Complex{Float64},2}}:
  -0.52509-0.0im        0.466117-0.0im         -0.71205-0.0im
 -0.636547-0.150138im  -0.705949-0.264487im  0.00728845-0.0624193im
 -0.481283-0.254781im   0.342947+0.311136im    0.579412+0.391558im

In [36]:
V₁


Out[36]:
3×3 Array{Complex{Float64},2}:
 -0.589967+0.398691im   -0.345216+0.313195im  -0.464074+0.245671im
 -0.028911-0.0557983im   0.700555-0.278459im  -0.632825+0.165126im
   0.69931+0.0im        -0.463053-0.0im       -0.544561-0.0im

In [37]:
abs.(V'*V₁)


Out[37]:
3×3 Array{Float64,2}:
 2.22045e-16  8.77708e-17  1.0
 1.68967e-15  1.0          8.77708e-17
 1.0          1.61841e-15  1.14439e-16

Explain non-uniqueness of $U$ and $V$!


In [38]:
# Jordan-Wielandt matrix
J=[zero(A*A') A; A' zero(A'*A)]


Out[38]:
8×8 Array{Complex{Float64},2}:
      0.0+0.0im            0.0+0.0im        …  0.198694+0.854638im
      0.0+0.0im            0.0+0.0im           0.905889+0.0936446im
      0.0+0.0im            0.0+0.0im           0.651562+0.37833im
      0.0+0.0im            0.0+0.0im           0.834811+0.353274im
      0.0+0.0im            0.0+0.0im           0.831302+0.217897im
 0.345443-0.68487im   0.650991-0.973053im   …       0.0+0.0im
 0.958365-0.560486im  0.608612-0.346561im           0.0+0.0im
 0.198694-0.854638im  0.905889-0.0936446im          0.0+0.0im

In [39]:
round.(abs.(J),digits=2)


Out[39]:
8×8 Array{Float64,2}:
 0.0   0.0   0.0   0.0   0.0   0.77  1.11  0.88
 0.0   0.0   0.0   0.0   0.0   1.17  0.7   0.91
 0.0   0.0   0.0   0.0   0.0   0.78  1.07  0.75
 0.0   0.0   0.0   0.0   0.0   0.55  1.03  0.91
 0.0   0.0   0.0   0.0   0.0   0.8   1.12  0.86
 0.77  1.17  0.78  0.55  0.8   0.0   0.0   0.0
 1.11  0.7   1.07  1.03  1.12  0.0   0.0   0.0
 0.88  0.91  0.75  0.91  0.86  0.0   0.0   0.0

In [40]:
λJ,UJ=eigen(J)


Out[40]:
Eigen{Complex{Float64},Float64,Array{Complex{Float64},2},Array{Float64,1}}
values:
8-element Array{Float64,1}:
 -3.335456167848846
 -0.8658601013427636
 -0.7393073150147749
 -6.551110489870999e-18
  9.379638816410472e-17
  0.7393073150147731
  0.8658601013427667
  3.335456167848843
vectors:
8×8 Array{Complex{Float64},2}:
 -0.240785-0.189696im    -0.328052+0.325186im   …  -0.240785-0.189696im
 -0.313086-0.0978259im     0.34797+0.0833616im     -0.313086-0.0978259im
 -0.233149-0.217414im   -0.0557494-0.0623629im     -0.233149-0.217414im
 -0.250918-0.174623im   -0.0384106-0.102748im      -0.250918-0.174623im
 -0.311521-0.0794967im  -0.0521663-0.369936im      -0.311521-0.0794967im
   0.32815-0.173716im    -0.244105+0.221462im   …   -0.32815+0.173716im
  0.447475-0.116761im     0.495367-0.1969im        -0.447475+0.116761im
  0.385063-0.0im         -0.327428-0.0im           -0.385063-0.0im

In [41]:
λJ


Out[41]:
8-element Array{Float64,1}:
 -3.335456167848846
 -0.8658601013427636
 -0.7393073150147749
 -6.551110489870999e-18
  9.379638816410472e-17
  0.7393073150147731
  0.8658601013427667
  3.335456167848843

Example - Random real matrix


In [42]:
m=8
n=5
q=min(m,n)
A=rand(-9:9,m,n)


Out[42]:
8×5 Array{Int64,2}:
  4  -9   9   9   4
  6  -6   4   5   7
  5   4   3  -3   0
 -8   6  -1  -6  -1
 -5   1  -2  -9  -7
 -4   9  -6  -5  -9
  4   2  -4  -3  -9
  2  -9   8  -8   2

In [43]:
U,σ,V=svd(A)


Out[43]:
SVD{Float64,Float64,Array{Float64,2}}
U factor:
8×5 Array{Float64,2}:
 -0.52051    -0.00637623   0.0818925   0.371645    0.75484
 -0.410187   -0.0457767    0.0840241  -0.205334   -0.311161
  0.0168078   0.104332     0.302453   -0.802742    0.379587
  0.309284    0.16034     -0.5493     -0.179291    0.292071
  0.340599    0.443716     0.0164029   0.335447    0.0174456
  0.500772   -0.0927259    0.148085    0.0299671   0.307785
  0.233164    0.0155959    0.752819    0.165196   -0.040422
 -0.20912     0.869221     0.0639655  -0.0496671  -0.0856345
singular values:
5-element Array{Float64,1}:
 30.385676465296772
 15.251140072988441
 11.426337586227328
  7.917239011913147
  5.18358254629035
Vt factor:
5×5 Array{Float64,2}:
 -0.333218    0.535272   -0.423741   -0.435647    -0.482809
 -0.0726555  -0.424309    0.424392   -0.792837    -0.0773504
  0.80544    -0.0917222  -0.0779845  -0.00985778  -0.580237
 -0.44971    -0.633676   -0.203895    0.320187    -0.502119
 -0.180871    0.351408    0.769857    0.281076    -0.414866

In [44]:
# Fact 10
tr(sqrt(A'*A)), sum(σ)


Out[44]:
(70.163975682716, 70.16397568271604)

In [45]:
# Fact 11
B=rand(n,n)
det(B), prod(svdvals(B))


Out[45]:
(-0.028017553424504286, 0.028017553424504266)

In [46]:
# Fact 14
norm(A), opnorm(A), σ[1]


Out[46]:
(37.094473981982816, 30.385676465296758, 30.385676465296772)

In [47]:
# Fact 15
B=rand(m,n)
abs(tr(A*B')), sum(svdvals(A)svdvals(B))


Out[47]:
(17.021465467822562, 126.73068798856356)

In [48]:
# Interlace Theorems (repeat several times)
j=rand(1:q)
σBrow=svdvals(A[[1:j-1;j+1:m],:])
σBcol=svdvals(A[:,[1:j-1;j+1:n]])
j, σ, σBrow, σBcol


Out[48]:
(3, [30.385676465296772, 15.251140072988441, 11.426337586227328, 7.917239011913147, 5.18358254629035], [30.381664988551634, 15.190790575637333, 11.111121043327122, 5.829582595973563, 2.3985975335180783], [27.80450958074093, 13.658590712400924, 11.366367773120052, 7.756147908064868])

In [49]:
σ[1:end].>=σBrow, σ[1:end-1].>=σBcol, 
σ[2:end].<=σBrow[1:end-1], σ[2:end].<=σBcol


Out[49]:
(Bool[1, 1, 1, 1, 1], Bool[1, 1, 1, 1], Bool[1, 1, 1, 1], Bool[1, 1, 1, 1])

In [50]:
# Weyl Inequalities
B=rand(m,n)
μ=svdvals(B)
γ=svdvals(A+B)
[γ σ μ]


Out[50]:
5×3 Array{Float64,2}:
 30.2108   30.3857   3.11734
 14.6781   15.2511   0.913401
 11.4898   11.4263   0.629493
  8.04166   7.91724  0.456276
  5.29955   5.18358  0.308923

In [51]:
@show k=rand(1:q)
sum(γ[1:k]),sum(σ[1:k])+sum(μ[1:k])


k = rand(1:q) = 1
Out[51]:
(30.210849533162563, 33.503015705116624)

Matrix approximation

Let $A=U\Sigma V^*$, let $\tilde \Sigma$ be equal to $\Sigma$ except that $\tilde \Sigma_{jj}=0$ for $j>k$, and let $\tilde A=U\tilde \Sigma V^*$. Then $\mathop{\mathrm{rank}}(\tilde A)\leq k$ and

\begin{align*} \min\{\|A-B\|_2: \mathop{\mathrm{rank}}(B)\leq k\} & =\|A-\tilde A\|_2=\sigma_{k+1}(A)\\ \min\{\|A-B\|_F: \mathop{\mathrm{rank}}(B)\leq k\} & =\|A-\tilde A\|_F= \bigg(\sum_{j=k+1}^{q}\sigma_{j}^2(A)\bigg)^{1/2}. \end{align*}

This is the Eckart-Young-Mirsky Theorem.


In [52]:
A


Out[52]:
8×5 Array{Int64,2}:
  4  -9   9   9   4
  6  -6   4   5   7
  5   4   3  -3   0
 -8   6  -1  -6  -1
 -5   1  -2  -9  -7
 -4   9  -6  -5  -9
  4   2  -4  -3  -9
  2  -9   8  -8   2

In [53]:
σ


Out[53]:
5-element Array{Float64,1}:
 30.385676465296772
 15.251140072988441
 11.426337586227328
  7.917239011913147
  5.18358254629035

In [54]:
# @show k=rand(1:q-1)
k=3
B=U*Diagonal([σ[1:k];zeros(q-k)])*V'
# B=U[:,1:k]*Diagonal(σ[1:k])*V[:,1:k]'


Out[54]:
8×5 Array{Float64,2}:
  6.03094  -8.51045    6.58766    6.95809   7.10071
  4.97718  -6.46336    4.91026    5.97388   5.51457
  2.49776  -0.718763   0.189362  -1.5181   -2.37492
 -8.36452   4.56848   -2.45497   -5.97104  -1.08465
 -3.7893    2.65115   -1.52811   -9.87577  -5.62895
 -3.60474   8.5897    -7.17988   -5.5244   -8.21898
  4.55028   2.90241   -3.57202   -3.35988  -8.43021
  1.74287  -9.09319    8.26156   -7.74933   1.6184

In [55]:
A


Out[55]:
8×5 Array{Int64,2}:
  4  -9   9   9   4
  6  -6   4   5   7
  5   4   3  -3   0
 -8   6  -1  -6  -1
 -5   1  -2  -9  -7
 -4   9  -6  -5  -9
  4   2  -4  -3  -9
  2  -9   8  -8   2

In [56]:
opnorm(A-B), σ[k+1]


Out[56]:
(7.91723901191314, 7.917239011913147)

In [57]:
norm(A-B),norm(σ[k+1:q])


Out[57]:
(9.463202501582924, 9.46320250158293)