Java matrix problem

I use jama to calculate SVD. It works very well. If I pass the square matrix. For example, a 2x2 or 3x3 matrix, etc. But when I transfer something like this 2x3 or 4x8, it gives an error. I used all of their examples. They have another constructor for the job. Also my second question: I use a 3x3 matrix, and it gave

double[][] vals = {{1.,1.,0},{1.,0.,1.},{1.,3.,4.},{6.,4.,8.}};
  Matrix A = new Matrix(vals);

He made the following error:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3

After that I tried to use another constructor following

double[][] vals = {{1.,1.,0,4},{1.,0.,1.,2},{1.,3.,4.,8},{1.,3.,4.,8}};
  Matrix A = new Matrix(vals,4,3);

He produced the following result:

A = 
 1.0 1.0 0.0
 1.0 0.0 1.0
 1.0 3.0 4.0
 6.0 4.0 8.0

A = U S V^T

U = 
 0.078 -0.115 -0.963
 0.107 -0.281 0.260
 0.402 0.886 -0.018
 0.906 -0.351 0.060

Sigma = 
 11.861881 0.000000 0.000000
 0.000000 2.028349 0.000000
 0.000000 0.000000 1.087006

V = 
 0.507705 -0.795196 -0.331510
 0.413798 0.562579 -0.715735
 0.755650 0.226204 0.614675

rank = 3
condition number = 10.912437186202627
2-norm = 11.86188091889931
singular values = 
 11.861881 2.028349 1.087006

It worked for a non-square matrix. But this led to incorrect results for svd, since V and S do not have the same lines = 4 (sorry if I couldn’t parse the result correctly, as I am new to SVD). Any ideas? What should I do?

+3
4

, JAMA SVD , "readme", , (m < n) .

, ArrayIndexOutOfBounds 486 SingularValueDecomposition:

return new Matrix(U,m,Math.min(m+1,n));

:

return new Matrix(U);

. , ( , vicatcu), , m=4 n=5, U m=4 n=4. SingularValueDecomposition, :

m-by-n A m >= n m--n U, n--n S n--n V, A = USV '.

, m=4 n=5 m<n. , , , U , SVD-, :

new Matrix(U, m, Math.min(m+1,n))

m, 4 ( ) n, Math.min(4+1,5)=5 ( ). : , getColumnDimension, U 5, , .

, , , U , , .

+4

wiki SVD. 2.

import Jama.Matrix; 
import Jama.SingularValueDecomposition; 

public class JAMATest { 

    static public void printMatrix(Matrix m){
        double[][] d = m.getArray();

        for(int row = 0; row < d.length; row++){
            for(int col = 0; col < d[row].length; col++){
                System.out.printf("%6.4f\t", m.get(row, col));
            }
            System.out.println();
        }
        System.out.println();
    }

    public static void main(String[] args) { 
        double[][] vals = { {1., 0., 0., 0., 2.}, 
                            {0., 0., 3., 0., 0.}, 
                            {0., 0., 0., 0., 0.}, 
                            {0., 4., 0., 0., 0.} 
                          };  
        Matrix A = new Matrix(vals);         
        SingularValueDecomposition svd = new SingularValueDecomposition(A); 

        System.out.println("A = ");
        printMatrix(A);

        System.out.println("U = ");
        printMatrix(svd.getU());

        System.out.println("Sigma = ");
        printMatrix(svd.getS());

        System.out.println("V = ");
        printMatrix(svd.getV());
    } 
} 

outputL:

A = 
1.0000  0.0000  0.0000  0.0000  2.0000  
0.0000  0.0000  3.0000  0.0000  0.0000  
0.0000  0.0000  0.0000  0.0000  0.0000  
0.0000  4.0000  0.0000  0.0000  0.0000  

U = 
0.0000  0.0000  -1.0000 0.0000  
0.0000  1.0000  -0.0000 0.0000  
0.0000  0.0000  -0.0000 1.0000  
1.0000  0.0000  -0.0000 0.0000  

Sigma = 
4.0000  0.0000  0.0000  0.0000  0.0000  
0.0000  3.0000  0.0000  0.0000  0.0000  
0.0000  0.0000  2.2361  0.0000  0.0000  
0.0000  0.0000  0.0000  0.0000  0.0000  
0.0000  0.0000  0.0000  0.0000  0.0000  

V = 
0.0000  -0.0000 -0.4472 -0.8944 -0.0000 
0.0000  -0.0000 -0.0000 -0.0000 -0.0000 
0.0000  1.0000  -0.0000 -0.0000 -0.0000 
0.0000  -0.0000 -0.0000 -0.0000 1.0000  
1.0000  -0.0000 -0.8944 0.4472  -0.0000 

, . , FWIW - Matlab :

>> A = [1.0000,  0.0000,  0.0000,  0.0000,  2.0000; 0, 0, 3, 0, 0; 0, 0, 0, 0, 0; 0, 4, 0, 0, 0];
>> A

A =

     1     0     0     0     2
     0     0     3     0     0
     0     0     0     0     0
     0     4     0     0     0

>> [U, S, V] = svd(A);
>> U

U =

     0     0     1     0
     0     1     0     0
     0     0     0    -1
     1     0     0     0

>> S

S =

    4.0000         0         0         0         0
         0    3.0000         0         0         0
         0         0    2.2361         0         0
         0         0         0         0         0

>> V

V =

         0         0    0.4472         0   -0.8944
    1.0000         0         0         0         0
         0    1.0000         0         0         0
         0         0         0    1.0000         0
         0         0    0.8944         0    0.4472

, :

import Jama.Matrix;

public class JAMATest {

    /**
     * @param args
     */
    public static void main(String[] args) {
        double[][] vals = {{1.,1.,0},{1.,0.,1.},{1.,3.,4.},{6.,4.,8.}}; 
        Matrix A = new Matrix(vals); 

    }
}

, - , , . printMatrix , , , .

+1

U, S V , A. U , V ^ T . .

Another dimension (columns U, rows V ^ T and rows / columns S) will be “rank” A (in your example 3). Roughly speaking, this is the dimension of your data ... how many axes are needed to uniquely represent a column or row in A. It will be the biggest min(rows, cols), but it can often be much smaller. This is normal.

0
source

Jama does not support full SVD, but only reduces SVD. This is equivalent to Matlab svd (B, 0) or svd (B, 'econ'). Bye

0
source

Source: https://habr.com/ru/post/1728935/


All Articles