12.07.2015 Views

COPYRIGHT 2008, PRINCETON UNIVERSITY PRESS

COPYRIGHT 2008, PRINCETON UNIVERSITY PRESS

COPYRIGHT 2008, PRINCETON UNIVERSITY PRESS

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

systems of equations with matrices; data fitting 171✞✝/∗ JamaEigen . java : eigenvalue problem with JAMA. JAMA must be in same directory orincluded in CLASSPATH . Uses Matrix . class ; see Matrix . java or documentation ∗/import Jama . ∗ ;import java . io .∗ ;public class JamaEigen {public static void main ( String [] argv ) {double [][] I = { {2./3,−1./4,−1./4}, {−1./4,2./3,−1./4}, { −1./4,−1./4,2./3}};Matrix MatI = new Matrix ( I ) ; / / Form Matrix from 2D arraysSystem . out . print ( "Input Matrix" );MatI . print (10 , 5) ; / / Jama Matrix printEigenvalueDecomposition E = new EigenvalueDecomposition (MatI ) ; // Eigenvalue finderdouble [ ] lambdaRe = E . getRealEigenvalues () ; / / Real , Imag eigensdouble [ ] lambdaIm = E . getImagEigenvalues () ; / / Imag eigensSystem . out . println ("Eigenvalues : \t lambda.Re[]="+ lambdaRe [0]+","+lambdaRe [1]+", "+lambdaRe [2]) ;Matrix V = E. getV () ; // Get matrix of eigenvectorsSystem . out . print ("\n Matrix with column eigenvectors ");V. print (10 , 5) ;Matrix Vec = new Matrix (3 ,1) ; // Extract single eigenvectorVec . set ( 0 , 0 , V. get (0 , 0) ) ;Vec . set ( 1 , 0 , V. get (1 , 0) ) ;Vec . set ( 2 , 0 , V. get (2 , 0) ) ;System . out . print ( "First Eigenvector , Vec" );Vec. print (10 ,5) ;Matrix LHS = MatI . times (Vec) ; / / Should get Vec as answerMatrix RHS = Vec. times (lambdaRe [0]) ;System . out . print ( "Does LHS = RHS?" );LHS. print (18 , 12) ;RHS. print (18 , 12) ;}}☎2468101214161820222426283032Listing 8.1 JamaEigen.java uses the JAMA matrix library to solve eigenvalue problems. Notethat JAMA defines and manipulates the new data type (object) Matrix, which differs from anarray but can be created from one.EigenDecompositionE=newEigenDecomposition(MatI); 13Then on line 14 we extract (get) a vector lambdaRe of length 3 containing the three(real) eigenvalues lambdaRe[0], lambdaRe[1], lambdaRe[2]:double[] lambdaRe = E.getRealEigenvalues(); 14On line 18 we create a 3 × 3 matrix V containing the eigenvectors in the threecolumns of the matrix with the JAMA command:Matrix V = E.getV(); 18which takes the eigenvector object E and gets the vectors from it. Then, on lines22–24 we form a vector Vec (a 3 × 1 Matrix) containing a single eigenvector byextracting the elements from V with a get method and assigning them with a setmethod:Vec.set(0,0,V.get(0,0)); 22−101<strong>COPYRIGHT</strong> <strong>2008</strong>, PRINCET O N UNIVE R S I T Y P R E S SEVALUATION COPY ONLY. NOT FOR USE IN COURSES.ALLpup_06.04 — <strong>2008</strong>/2/15 — Page 171

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!