Example: Singular Value Decomposition of a Matrix

The singular value decomposition of a matrix is performed. The rank of the matrix is also computed.
import com.imsl.math.*;

public class SVDEx1 {
    public static void main(String args[]) throws SVD.DidNotConvergeException {
        double	a[][] = {
            {1, 2, 1, 4},
            {3, 2, 1, 3},
            {4, 3, 1, 4},
            {2, 1, 3, 1},
            {1, 5, 2, 2},
            {1, 2, 2, 3}
        };
        
        // Compute the SVD factorization of A
        SVD	svd = new SVD(a);
        
        // Print U, S and V.
        new PrintMatrix("U").print(svd.getU());
        new PrintMatrix("S").print(svd.getS());
        new PrintMatrix("V").print(svd.getV());
        
        // Find the rank of A.
        int rank = svd.getRank();
        System.out.println("rank = "+rank);
    }
}

Output

                         U
     0       1       2       3       4       5     
0  -0.38    0.12    0.439  -0.565   0.024  -0.573  
1  -0.404   0.345  -0.057   0.215   0.809   0.119  
2  -0.545   0.429   0.051   0.432  -0.572   0.04   
3  -0.265  -0.068  -0.884  -0.215  -0.063  -0.306  
4  -0.446  -0.817   0.142   0.321   0.062  -0.08   
5  -0.355  -0.102  -0.004  -0.546  -0.099   0.746  

     S
     0     
0  11.485  
1   3.27   
2   2.653  
3   2.089  

                 V
     0       1       2       3     
0  -0.444   0.556  -0.435   0.552  
1  -0.558  -0.654   0.277   0.428  
2  -0.324  -0.351  -0.732  -0.485  
3  -0.621   0.374   0.444  -0.526  

rank = 4
Link to Java source.