Debian Codegraph @

src/dr/math/matrixAlgebra/LargestEigenvalueFinder.java

raw symbols SCIP
1 /*
2 * LargestEigenvalueFinder.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.math.matrixAlgebra;
27
28 import dr.math.iterations.IterativeProcess;
29
30 /**
31 * Object used to find the largest eigen value and the corresponding
32 * eigen vector of a matrix by successive approximations.
33 *
34 * @author Didier H. Besset
35 */
36 public class LargestEigenvalueFinder extends IterativeProcess
37 {
38 /**
39 * Eigenvalue
40 */
41 private double eigenvalue;
42 /**
43 * Eigenvector
44 */
45 private Vector eigenvector;
46 /**
47 * Eigenvector of transposed matrix
48 */
49 private Vector transposedEigenvector;
50 /**
51 * Matrix.
52 */
53 private Matrix matrix;
54
55 /**
56 * Constructor method.
57 * @param prec double
58 * @param a MatrixAlgebra.Matrix
59 */
60 public LargestEigenvalueFinder ( double prec, Matrix a)
61 {
62 this(a);
63 this.setDesiredPrecision ( prec);
64 }
65 /**
66 * Constructor method.
67 * @param a MatrixAlgebra.Matrix
68 */
69 public LargestEigenvalueFinder ( Matrix a)
70 {
71 matrix = a;
72 eigenvalue = Double.NaN;
73 }
74 /**
75 * Returns the eigen value found by the receiver.
76 * @return double
77 */
78 public double eigenvalue ( )
79 {
80 return eigenvalue;
81 }
82 /**
83 * Returns the normalized eigen vector found by the receiver.
84 * @return MatrixAlgebra.Vector
85 */
86 public Vector eigenvector ( )
87 {
88 return eigenvector.product( 1.0 / eigenvector.norm());
89 }
90 /**
91 * Iterate matrix product in eigenvalue information.
92 */
93 public double evaluateIteration()
94 {
95 double oldEigenvalue = eigenvalue;
96 transposedEigenvector =
97 transposedEigenvector.secureProduct( matrix);
98 transposedEigenvector = transposedEigenvector.product( 1.0
99 / transposedEigenvector.components[0]);
100 eigenvector = matrix.secureProduct( eigenvector);
101 eigenvalue = eigenvector.components[0];
102 eigenvector = eigenvector.product( 1.0 / eigenvalue);
103 return Double.isNaN( oldEigenvalue)
104 ? 10 * getDesiredPrecision()
105 : Math.abs( eigenvalue - oldEigenvalue);
106 }
107 /**
108 * Set result to undefined.
109 */
110 public void initializeIterations()
111 {
112 eigenvalue = Double.NaN;
113 int n = matrix.columns();
114 double [] eigenvectorComponents = new double[ n];
115 for ( int i = 0; i < n; i++) { eigenvectorComponents [i] = 1.0;}
116 eigenvector = new Vector( eigenvectorComponents);
117 n = matrix.rows();
118 eigenvectorComponents = new double[ n];
119 for ( int i = 0; i < n; i++) { eigenvectorComponents [i] = 1.0;}
120 transposedEigenvector = new Vector( eigenvectorComponents);
121 }
122 /**
123 * Returns a finder to find the next largest eigen value of the receiver's matrix.
124 * @return MatrixAlgebra.LargestEigenvalueFinder
125 */
126 public LargestEigenvalueFinder nextLargestEigenvalueFinder ( )
127 {
128 double norm = 1.0 / eigenvector.secureProduct(
129 transposedEigenvector);
130 Vector v1 = eigenvector.product( norm);
131 return new LargestEigenvalueFinder( getDesiredPrecision(),
132 matrix.secureProduct(SymmetricMatrix.identityMatrix(
133 v1.dimension()).secureSubtract(v1.tensorProduct(
134 transposedEigenvector))));
135 }
136 /**
137 * Returns a string representation of the receiver.
138 * @return java.lang.String
139 */
140 public String toString()
141 {
142 StringBuffer sb = new StringBuffer();
143 sb.append( eigenvalue);
144 sb.append(" (");
145 sb.append( eigenvector.toString());
146 sb.append(')');
147 return sb.toString();
148 }
149 }