Search in sources :

Example 1 with RobustSingularValueDecomposition

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;
        }
    }
}
Also used : Matrix(dr.math.matrixAlgebra.Matrix) RobustSingularValueDecomposition(dr.math.matrixAlgebra.RobustSingularValueDecomposition) DoubleMatrix2D(cern.colt.matrix.DoubleMatrix2D) DenseDoubleMatrix2D(cern.colt.matrix.impl.DenseDoubleMatrix2D) DoubleMatrix1D(cern.colt.matrix.DoubleMatrix1D) DenseDoubleMatrix2D(cern.colt.matrix.impl.DenseDoubleMatrix2D) RobustEigenDecomposition(dr.math.matrixAlgebra.RobustEigenDecomposition)

Example 2 with RobustSingularValueDecomposition

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);
}
Also used : RobustSingularValueDecomposition(dr.math.matrixAlgebra.RobustSingularValueDecomposition) RobustEigenDecomposition(dr.math.matrixAlgebra.RobustEigenDecomposition) DenseDoubleMatrix2D(cern.colt.matrix.impl.DenseDoubleMatrix2D) DoubleMatrix2D(cern.colt.matrix.DoubleMatrix2D) DenseDoubleMatrix2D(cern.colt.matrix.impl.DenseDoubleMatrix2D) RobustEigenDecomposition(dr.math.matrixAlgebra.RobustEigenDecomposition)

Aggregations

DoubleMatrix2D (cern.colt.matrix.DoubleMatrix2D)2 DenseDoubleMatrix2D (cern.colt.matrix.impl.DenseDoubleMatrix2D)2 RobustEigenDecomposition (dr.math.matrixAlgebra.RobustEigenDecomposition)2 RobustSingularValueDecomposition (dr.math.matrixAlgebra.RobustSingularValueDecomposition)2 DoubleMatrix1D (cern.colt.matrix.DoubleMatrix1D)1 Matrix (dr.math.matrixAlgebra.Matrix)1