Debian Codegraph @

src/dr/evomodel/substmodel/ColtEigenSystem.java

raw symbols SCIP
1 /*
2 * ColtEigenSystem.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.evomodel.substmodel;
27
28 import cern.colt.matrix.DoubleMatrix2D;
29 import cern.colt.matrix.impl.DenseDoubleMatrix2D;
30 import cern.colt.matrix.linalg.Algebra;
31 import cern.colt.matrix.linalg.Property;
32 import dr.math.matrixAlgebra.RobustEigenDecomposition;
33 import dr.math.matrixAlgebra.RobustSingularValueDecomposition;
34
35 import java.util.Arrays;
36
37 /**
38 * @author Marc Suchard
39 */
40 public class ColtEigenSystem implements EigenSystem {
41
42 public ColtEigenSystem(int stateCount) {
43 this(stateCount, defaultCheckConditioning, defaultMaxConditionNumber, defaultMaxIterations);
44 }
45
46 public ColtEigenSystem(int stateCount, boolean checkConditioning, int maxConditionNumber, int maxIterations) {
47 this.stateCount = stateCount;
48 this.checkConditioning = checkConditioning;
49 this.maxConditionNumber = maxConditionNumber;
50 this.maxIterations = maxIterations;
51 }
52
53 public EigenDecomposition decomposeMatrix(double[][] matrix) {
54
55 final int stateCount = matrix.length;
56
57 RobustEigenDecomposition eigenDecomp = new RobustEigenDecomposition(
58 new DenseDoubleMatrix2D(matrix), maxIterations);
59
60 DoubleMatrix2D eigenV = eigenDecomp.getV();
61 DoubleMatrix2D eigenVInv;
62
63 if (checkConditioning) {
64 RobustSingularValueDecomposition svd;
65 try {
66 svd = new RobustSingularValueDecomposition(eigenV, maxIterations);
67 } catch (ArithmeticException ae) {
68 System.err.println(ae.getMessage());
69 return getEmptyDecomposition(stateCount);
70 }
71 if (svd.cond() > maxConditionNumber) {
72 return getEmptyDecomposition(stateCount);
73 }
74 }
75
76 try {
77 eigenVInv = alegbra.inverse(eigenV);
78 } catch (IllegalArgumentException e) {
79 return getEmptyDecomposition(stateCount);
80 }
81
82 double[][] Evec = eigenV.toArray();
83 double[][] Ievc = eigenVInv.toArray();
84 double[] Eval = getAllEigenValues(eigenDecomp);
85
86 if (checkConditioning) {
87 for (int i = 0; i < Eval.length; i++) {
88 if (Double.isNaN(Eval[i]) ||
89 Double.isInfinite(Eval[i])) {
90 return getEmptyDecomposition(stateCount);
91 } else if (Math.abs(Eval[i]) < 1e-10) {
92 Eval[i] = 0.0;
93 }
94 }
95 }
96
97 double[] flatEvec = new double[stateCount * stateCount];
98 double[] flatIevc = new double[stateCount * stateCount];
99
100 for (int i = 0; i < Evec.length; i++) {
101 System.arraycopy(Evec[i], 0, flatEvec, i * stateCount, stateCount);
102 System.arraycopy(Ievc[i], 0, flatIevc, i * stateCount, stateCount);
103 }
104
105 return new EigenDecomposition(flatEvec, flatIevc, Eval);
106 }
107
108
109 public double computeExponential(EigenDecomposition eigen, double distance, int i, int j) {
110 if (eigen == null) {
111 return 0.0;
112 }
113
114 double[] Evec = eigen.getEigenVectors();
115 double[] Eval = eigen.getEigenValues();
116 double[] Ievc = eigen.getInverseEigenVectors();
117
118 double temp = 0.0;
119 for (int k = 0; k < stateCount; ++k) {
120 temp += Evec[i * stateCount + k] * Math.exp(distance * Eval[k]) * Ievc[k * stateCount + j];
121 }
122 return Math.abs(temp);
123 }
124
125 public void computeExponential(EigenDecomposition eigen, double distance, double[] matrix) {
126 double temp;
127
128 if (eigen == null) {
129 Arrays.fill(matrix, 0.0);
130 return;
131 }
132
133 double[] Evec = eigen.getEigenVectors();
134 double[] Eval = eigen.getEigenValues();
135 double[] Ievc = eigen.getInverseEigenVectors();
136
137 double[][] iexp = new double[stateCount][stateCount];
138
139 // Eigenvalues and eigenvectors of a real matrix A.
140 //
141 // If A is symmetric, then A = V*D*V' where the eigenvalue matrix D is diagonal
142 // and the eigenvector matrix V is orthogonal. I.e. A = V D V^t and V V^t equals
143 // the identity matrix.
144 //
145 // If A is not symmetric, then the eigenvalue matrix D is block diagonal with
146 // the real eigenvalues in 1-by-1 blocks and any complex eigenvalues,
147 // lambda + i*mu, in 2-by-2 blocks, [lambda, mu; -mu, lambda]. The columns
148 // of V represent the eigenvectors in the sense that A*V = V*D. The matrix
149 // V may be badly conditioned, or even singular, so the validity of the
150 // equation A = V D V^{-1} depends on the conditioning of V.
151
152 for (int i = 0; i < stateCount; i++) {
153
154 // if (EvalImag[i] == 0) {
155 // 1x1 block
156 temp = Math.exp(distance * Eval[i]);
157 for (int j = 0; j < stateCount; j++) {
158 iexp[i][j] = Ievc[i * stateCount + j] * temp;
159 }
160 // } else {
161 // // 2x2 conjugate block
162 // // If A is 2x2 with complex conjugate pair eigenvalues a +/- bi, then
163 // // exp(At) = exp(at)*( cos(bt)I + \frac{sin(bt)}{b}(A - aI)).
164 // int i2 = i + 1;
165 // double b = EvalImag[i];
166 // double expat = Math.exp(distance * Eval[i]);
167 // double expatcosbt = expat * Math.cos(distance * b);
168 // double expatsinbt = expat * Math.sin(distance * b);
169 //
170 // for (int j = 0; j < stateCount; j++) {
171 // iexp[i][j] = expatcosbt * Ievc[i * stateCount + j] +
172 // expatsinbt * Ievc[i2 * stateCount + j];
173 // iexp[i2][j] = expatcosbt * Ievc[i2 * stateCount + j] -
174 // expatsinbt * Ievc[i * stateCount + j];
175 // }
176 // i++; // processed two conjugate rows
177 // }
178 }
179
180 int u = 0;
181 for (int i = 0; i < stateCount; i++) {
182 for (int j = 0; j < stateCount; j++) {
183 temp = 0.0;
184 for (int k = 0; k < stateCount; k++) {
185 temp += Evec[i * stateCount + k] * iexp[k][j];
186 }
187 matrix[u] = Math.abs(temp);
188 u++;
189 }
190 }
191 }
192
193 protected double[] getAllEigenValues(RobustEigenDecomposition decomposition) {
194 return decomposition.getRealEigenvalues().toArray();
195 }
196
197 protected double[] getEmptyAllEigenValues(int dim) {
198 return new double[dim];
199 }
200
201 protected EigenDecomposition getEmptyDecomposition(int dim) {
202 return new EigenDecomposition(
203 new double[dim * dim],
204 new double[dim * dim],
205 getEmptyAllEigenValues(dim)
206 );
207 }
208
209 private boolean checkConditioning;
210 private int maxConditionNumber;
211 private int maxIterations;
212
213 protected final int stateCount;
214
215 private static final double minProb = Property.DEFAULT.tolerance();
216 private static final Algebra alegbra = new Algebra(minProb);
217
218 public static final boolean defaultCheckConditioning = true;
219 public static final int defaultMaxConditionNumber = 1000000;
220 public static final int defaultMaxIterations = 1000000;
221
222 }