Debian Codegraph @

src/dr/evomodel/substmodel/ComplexColtEigenSystem.java

raw symbols SCIP
1 /*
2 * ComplexColtEigenSystem.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 dr.math.matrixAlgebra.RobustEigenDecomposition;
30
31 import java.util.Arrays;
32
33 /**
34 * @author Marc Suchard
35 */
36 public class ComplexColtEigenSystem extends ColtEigenSystem {
37
38 public ComplexColtEigenSystem(int stateCount) {
39 super(stateCount);
40 }
41
42 public ComplexColtEigenSystem(int stateCount, boolean checkConditioning, int maxConditionNumber, int maxIterations) {
43 super(stateCount, checkConditioning, maxConditionNumber, maxIterations);
44 }
45
46 protected double[] getAllEigenValues(RobustEigenDecomposition decomposition) {
47 double[] realEval = decomposition.getRealEigenvalues().toArray();
48 double[] imagEval = decomposition.getImagEigenvalues().toArray();
49
50 final int dim = realEval.length;
51 double[] merge = new double[2 * dim];
52 System.arraycopy(realEval, 0, merge, 0, dim);
53 System.arraycopy(imagEval, 0, merge, dim, dim);
54 return merge;
55 }
56
57 protected double[] getEmptyAllEigenValues(int dim) {
58 return new double[2 * dim];
59 }
60
61 protected boolean validDecomposition(DoubleMatrix2D eigenV) {
62 return true;
63 }
64
65 public double computeExponential(EigenDecomposition eigen, double distance, int i, int j) {
66 throw new RuntimeException("Not yet implemented");
67 }
68
69 public void computeExponential(EigenDecomposition eigen, double distance, double[] matrix) {
70 double temp;
71
72 if (eigen == null) {
73 Arrays.fill(matrix, 0.0);
74 return;
75 }
76
77 double[] Evec = eigen.getEigenVectors();
78 double[] Eval = eigen.getEigenValues();
79 double[] EvalImag = new double[stateCount];
80 System.arraycopy(Eval, stateCount, EvalImag, 0, stateCount);
81 double[] Ievc = eigen.getInverseEigenVectors();
82
83 double[][] iexp = new double[stateCount][stateCount];
84
85 // Eigenvalues and eigenvectors of a real matrix A.
86 //
87 // If A is symmetric, then A = V*D*V' where the eigenvalue matrix D is diagonal
88 // and the eigenvector matrix V is orthogonal. I.e. A = V D V^t and V V^t equals
89 // the identity matrix.
90 //
91 // If A is not symmetric, then the eigenvalue matrix D is block diagonal with
92 // the real eigenvalues in 1-by-1 blocks and any complex eigenvalues,
93 // lambda + i*mu, in 2-by-2 blocks, [lambda, mu; -mu, lambda]. The columns
94 // of V represent the eigenvectors in the sense that A*V = V*D. The matrix
95 // V may be badly conditioned, or even singular, so the validity of the
96 // equation A = V D V^{-1} depends on the conditioning of V.
97
98 for (int i = 0; i < stateCount; i++) {
99
100 if (EvalImag[i] == 0) {
101 // 1x1 block
102 temp = Math.exp(distance * Eval[i]);
103 for (int j = 0; j < stateCount; j++) {
104 iexp[i][j] = Ievc[i * stateCount + j] * temp;
105 }
106 } else {
107 // 2x2 conjugate block
108 // If A is 2x2 with complex conjugate pair eigenvalues a +/- bi, then
109 // exp(At) = exp(at)*( cos(bt)I + \frac{sin(bt)}{b}(A - aI)).
110 int i2 = i + 1;
111 double b = EvalImag[i];
112 double expat = Math.exp(distance * Eval[i]);
113 double expatcosbt = expat * Math.cos(distance * b);
114 double expatsinbt = expat * Math.sin(distance * b);
115
116 for (int j = 0; j < stateCount; j++) {
117 iexp[i][j] = expatcosbt * Ievc[i * stateCount + j] +
118 expatsinbt * Ievc[i2 * stateCount + j];
119 iexp[i2][j] = expatcosbt * Ievc[i2 * stateCount + j] -
120 expatsinbt * Ievc[i * stateCount + j];
121 }
122 i++; // processed two conjugate rows
123 }
124 }
125
126 int u = 0;
127 for (int i = 0; i < stateCount; i++) {
128 for (int j = 0; j < stateCount; j++) {
129 temp = 0.0;
130 for (int k = 0; k < stateCount; k++) {
131 temp += Evec[i * stateCount + k] * iexp[k][j];
132 }
133 matrix[u] = Math.abs(temp);
134 u++;
135 }
136 }
137 }
138
139 }