| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 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 |
|
| 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 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
for (int i = 0; i < stateCount; i++) { |
| 153 |
|
| 154 |
|
| 155 |
|
| 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 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 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 |
} |