001package algs9; // section 9.9
002import stdlib.*;
003import Jama.EigenvalueDecomposition;
004import Jama.Matrix;
005
006/* ***********************************************************************
007 *  Compilation:  javac -classpath .:jama.jar XEigenvalues.java
008 *  Execution:    java -classpath .:jama.jar XEigenvalues
009 *  Dependencies: jama.jar
010 *
011 *  Test client for computing eigenvalues and eigenvectors of a real
012 *  symmetric matrix A = V D V^T.
013 *
014 *       http://math.nist.gov/javanumerics/jama/
015 *       http://math.nist.gov/javanumerics/jama/Jama-1.0.1.jar
016 *
017 *************************************************************************/
018
019public class XEigenvalues {
020        public static void main(String[] args) {
021                int N = 5;
022
023                // create a symmetric positive definite matrix
024                Matrix A = Matrix.random(N, N);
025                A = A.transpose().times(A);
026
027                // compute the spectral decomposition
028                EigenvalueDecomposition e = A.eig();
029                Matrix V = e.getV();
030                Matrix D = e.getD();
031
032                StdOut.print("A =");
033                A.print(9, 6);
034                StdOut.print("D =");
035                D.print(9, 6);
036                StdOut.print("V =");
037                V.print(9, 6);
038
039                // check that V is orthogonal
040                StdOut.print("||V * V^T - I|| = ");
041                StdOut.println(V.times(V.transpose()).minus(Matrix.identity(N, N)).normInf());
042
043                // check that A V = D V
044                StdOut.print("||AV - DV|| = ");
045                StdOut.println(A.times(V).minus(V.times(D)).normInf());
046        }
047
048}