use of dr.math.matrixAlgebra.RobustSingularValueDecomposition in project beast-mcmc by beast-dev.
the class ComplexSubstitutionModel method setupMatrix.
public void setupMatrix() {
if (!eigenInitialised) {
initialiseEigen();
storedEvalImag = new double[stateCount];
}
int i = 0;
storeIntoAmat();
makeValid(amat, stateCount);
// compute eigenvalues and eigenvectors
// EigenvalueDecomposition eigenDecomp = new EigenvalueDecomposition(new DenseDoubleMatrix2D(amat));
RobustEigenDecomposition eigenDecomp;
try {
eigenDecomp = new RobustEigenDecomposition(new DenseDoubleMatrix2D(amat), maxIterations);
} catch (ArithmeticException ae) {
System.err.println(ae.getMessage());
wellConditioned = false;
System.err.println("amat = \n" + new Matrix(amat));
return;
}
DoubleMatrix2D eigenV = eigenDecomp.getV();
DoubleMatrix1D eigenVReal = eigenDecomp.getRealEigenvalues();
DoubleMatrix1D eigenVImag = eigenDecomp.getImagEigenvalues();
DoubleMatrix2D eigenVInv;
if (checkConditioning) {
RobustSingularValueDecomposition svd;
try {
svd = new RobustSingularValueDecomposition(eigenV, maxIterations);
} catch (ArithmeticException ae) {
System.err.println(ae.getMessage());
wellConditioned = false;
return;
}
if (svd.cond() > maxConditionNumber) {
wellConditioned = false;
return;
}
}
try {
eigenVInv = alegbra.inverse(eigenV);
} catch (IllegalArgumentException e) {
wellConditioned = false;
return;
}
Ievc = eigenVInv.toArray();
Evec = eigenV.toArray();
Eval = eigenVReal.toArray();
EvalImag = eigenVImag.toArray();
// Check for valid decomposition
for (i = 0; i < stateCount; i++) {
if (Double.isNaN(Eval[i]) || Double.isNaN(EvalImag[i]) || Double.isInfinite(Eval[i]) || Double.isInfinite(EvalImag[i])) {
wellConditioned = false;
return;
} else if (Math.abs(Eval[i]) < 1e-10) {
Eval[i] = 0.0;
}
}
updateMatrix = false;
wellConditioned = true;
// compute normalization and rescale eigenvalues
computeStationaryDistribution();
if (doNormalization) {
double subst = 0.0;
for (i = 0; i < stateCount; i++) subst += -amat[i][i] * stationaryDistribution[i];
for (i = 0; i < stateCount; i++) {
Eval[i] /= subst;
EvalImag[i] /= subst;
}
}
}
use of dr.math.matrixAlgebra.RobustSingularValueDecomposition in project beast-mcmc by beast-dev.
the class ColtEigenSystem method decomposeMatrix.
public EigenDecomposition decomposeMatrix(double[][] matrix) {
final int stateCount = matrix.length;
RobustEigenDecomposition eigenDecomp = new RobustEigenDecomposition(new DenseDoubleMatrix2D(matrix), maxIterations);
DoubleMatrix2D eigenV = eigenDecomp.getV();
DoubleMatrix2D eigenVInv;
if (checkConditioning) {
RobustSingularValueDecomposition svd;
try {
svd = new RobustSingularValueDecomposition(eigenV, maxIterations);
} catch (ArithmeticException ae) {
System.err.println(ae.getMessage());
return getEmptyDecomposition(stateCount);
}
if (svd.cond() > maxConditionNumber) {
return getEmptyDecomposition(stateCount);
}
}
try {
eigenVInv = alegbra.inverse(eigenV);
} catch (IllegalArgumentException e) {
return getEmptyDecomposition(stateCount);
}
double[][] Evec = eigenV.toArray();
double[][] Ievc = eigenVInv.toArray();
double[] Eval = getAllEigenValues(eigenDecomp);
if (checkConditioning) {
for (int i = 0; i < Eval.length; i++) {
if (Double.isNaN(Eval[i]) || Double.isInfinite(Eval[i])) {
return getEmptyDecomposition(stateCount);
} else if (Math.abs(Eval[i]) < 1e-10) {
Eval[i] = 0.0;
}
}
}
double[] flatEvec = new double[stateCount * stateCount];
double[] flatIevc = new double[stateCount * stateCount];
for (int i = 0; i < Evec.length; i++) {
System.arraycopy(Evec[i], 0, flatEvec, i * stateCount, stateCount);
System.arraycopy(Ievc[i], 0, flatIevc, i * stateCount, stateCount);
}
return new EigenDecomposition(flatEvec, flatIevc, Eval);
}
Aggregations