001package algs9; // section 9.9
002import stdlib.*;
003import Jama.Matrix;
004import Jama.SingularValueDecomposition;
005
006/* ***********************************************************************
007 *  Compilation:  javac -classpath .:jama.jar XSVD.java
008 *  Execution:    java  -classpath .:jama.jar XSVD
009 *  Dependencies: jama.jar
010 *
011 *  Test client for computing singular values of a matrix.
012 *
013 *       http://math.nist.gov/javanumerics/jama/
014 *       http://math.nist.gov/javanumerics/jama/Jama-1.0.1.jar
015 *
016 *************************************************************************/
017
018public class XSVD {
019        public static void main(String[] args) {
020
021                // create M-by-N matrix that doesn't have full rank
022                int M = 8, N = 5;
023                Matrix B = Matrix.random(5, 3);
024                Matrix A = Matrix.random(M, N).times(B).times(B.transpose());
025                StdOut.print("A = ");
026                A.print(9, 6);
027
028                // compute the singular vallue decomposition
029                StdOut.println("A = U S V^T");
030                StdOut.println();
031                SingularValueDecomposition s = A.svd();
032                StdOut.print("U = ");
033                Matrix U = s.getU();
034                U.print(9, 6);
035                StdOut.print("Sigma = ");
036                Matrix S = s.getS();
037                S.print(9, 6);
038                StdOut.print("V = ");
039                Matrix V = s.getV();
040                V.print(9, 6);
041                StdOut.println("rank = " + s.rank());
042                StdOut.println("condition number = " + s.cond());
043                StdOut.println("2-norm = " + s.norm2());
044
045                // print out singular values
046                StdOut.print("singular values = ");
047                Matrix svalues = new Matrix(s.getSingularValues(), 1);
048                svalues.print(9, 6);
049        }
050
051}