# Obtaining a thin/compact SVD

I'm using SingularValueDecomposition for a least-squares regression, the instruction that works fine for what I need is

SingularValueDecomposition[X,Length[SingularValueList[X]]]


Is there a way to do the same thing but without calling SingularValueList as I suppose similar computations are done two times in SingularValueDecomposition and SingularValueList ?

-

You can find the full svd, then use the number of nonzero singular values to recover the thin svd.

thinSVD[mat_] := Module[
{u, w, v, wprime, len},
{u, w, v} = SingularValueDecomposition[mat];
wprime = DeleteCases[w, {_?(# == 0 &) ..}];
len = Length[wprime];
wprime = wprime[[All, 1 ;; len]];
{u[[All, 1 ;; len]], wprime, v[[All, 1 ;; len]]}
]


Here is a fairly standard example.

In[66]:= m = N[{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}];
{ut, wt, vt} = thinSVD[m]
Chop[ut.wt.Transpose[vt] - m](*want zeros*)

Out[67]= {{{-0.214837, -0.887231}, {-0.520587, -0.249644}, {-0.826338,
0.387943}}, {{16.8481, 0.}, {0., 1.06837}}, {{-0.479671,
0.776691}, {-0.572368, 0.0756865}, {-0.665064, -0.625318}}}

Out[68]= {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}}

-
Thanks for the quick answer. – faysou Mar 6 '12 at 23:47
Daniel, would {(0 | 0.) ..} work as well? – Mr.Wizard Mar 6 '12 at 23:54
Often. If the matrix is machine numbers or integers then probably yes. Though I'm not certain the exact case will always figure out it has exact zeros (I guess it will). But don't expect it to work if bignums are used. – Daniel Lichtblau Mar 7 '12 at 0:02
I think this could be improved by letting your thinSVD[] function take a Tolerance option as well. From that, you'll need to modify the DeleteCases[] line... – J. M. Apr 18 '12 at 6:02