For what its worth, I tested the Mahout SVD which shares code lineage with
the Commons Math implementation.

The results I got were:


>
>
>
>
>
>
>
>
>
>
>
> *sum(abs(m - u * s * v')) = 4.31946146e-16S =    1.002319690998
>  1.002319690998    1.000000000000 U =    0.994059401897 0.067747631244
> -0.085183120170    0.106158721369 -0.776140124790 0.621559999170
>  0.024004819899 0.626910492138 0.778721390145 V =    0.996365312543
> 0.000000000000 -0.085183120170    0.053139565816 -0.781562124195
> 0.621559999170    0.066575900346 0.623827416858 0.778721390145*


Note that the residue of the reconstruction is excellently small.  This
indicates that the result is correct.


If you compare these to the R results,


>
>
>
>
>
>
>
>
>
>
> *[1] 1.0023196909980066 1.0023196909980066 1.0000000000000000$u
>           [,1]                  [,2]                  [,3][1,]
>  0.067747631244291326 -0.994059401896534967  0.085183120169970525 [2,]
> -0.776140124789635122 -0.106158721369163295 -0.621559999170469113[3,]
>  0.626910492137687125 -0.024004819898688426 -0.778721390144969994$v
>              [,1]                  [,2]                  [,3] [1,]
>  0.00000000000000000 -0.996365312542597747  0.085183120169970497[2,]
> -0.78156212419496163 -0.053139565815546450 -0.621559999170469668[3,]
>  0.62382741685810772 -0.066575900345596822 -0.778721390144969550*


These are identical to the previous results except that the first two
singular values are equal which means that the order of the corresponding
left and right singular vectors are different and there are sign changes in
the singular vectors.

My guess is that you will get the same results in Apache Commons Math.



On Fri, Feb 14, 2014 at 6:07 PM, Patrick Meyer <meyer...@gmail.com> wrote:

> Hi,
>
>
>
> I am using the SingularValueDecomposition class with a matrix but it gives
> me a different result than R. My knowledge of SVD is limited, so any advice
> is welcomed.
>
>
>
> Here's the method in Java
>
>
>
> public void svdTest(){
>
>
>
>         double[][] x = {
>
>                 {1.0,  -0.053071807862720116,  0.04236086650321309},
>
>                 {0.05307180786272012,  1.0,  0.0058054424137053435},
>
>                 {-0.04236086650321309,  -0.005805442413705342,  1.0}
>
>         };
>
>
>
>         RealMatrix X = new Array2DRowRealMatrix(x);
>
>
>
>         SingularValueDecomposition svd = new SingularValueDecomposition(X);
>
>
>
>         RealMatrix U = svd.getU();
>
>         for(int i=0;i<U.getRowDimension();i++){
>
>             for(int j=0;j<U.getColumnDimension();j++){
>
>                 System.out.print(U.getEntry(i,j) + "  ");
>
>             }
>
>             System.out.println();
>
>         }
>
>
>
>         System.out.println();
>
>         System.out.println();
>
>         RealMatrix V = svd.getV();
>
>         for(int i=0;i<V.getRowDimension();i++){
>
>             for(int j=0;j<V.getColumnDimension();j++){
>
>                 System.out.print(V.getEntry(i,j) + "  ");
>
>             }
>
>             System.out.println();
>
>         }
>
>
>
>
>
>     }
>
>
>
>
>
> And here's the function in R.
>
>
>
> x<-matrix(c(
>
>                 1.0,  -0.053071807862720116,  0.04236086650321309,
>
>       0.05307180786272012,  1.0,  0.0058054424137053435,
>
>       -0.04236086650321309,  -0.005805442413705342,  1.0),
>
>                 nrow=3, byrow=TRUE)
>
> svd(x)
>
>
>
> Does anyone know why I am getting different results for U and V? I am using
> commons math 3.1.
>
>
>
> Thanks,
>
> Patrick
>
>
>
>
>
>
>
>

Reply via email to