Java Code Examples for org.ejml.data.DenseMatrix64F#getNumCols()

The following examples show how to use org.ejml.data.DenseMatrix64F#getNumCols() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PermutationIndices.java    From beast-mcmc with GNU Lesser General Public License v2.1 7 votes vote down vote up
public PermutationIndices(DenseMatrix64F matrix) {

        this.matrix = matrix;
        dim = matrix.getNumCols();
        assert (dim == matrix.getNumRows());

        for (int i = 0; i < dim; ++i) {
            double diagonal = matrix.get(i, i);
            if (Double.isInfinite(diagonal)) {
                ++infiniteCount;
            } else if (diagonal == 0.0) {
                ++zeroCount;
            } else {
                ++nonZeroFiniteCount;
            }
        }
    }
 
Example 2
Source File: MissingOps.java    From beast-mcmc with GNU Lesser General Public License v2.1 6 votes vote down vote up
public static double det(DenseMatrix64F mat) {
    int numCol = mat.getNumCols();
    int numRow = mat.getNumRows();
    if (numCol != numRow) {
        throw new IllegalArgumentException("Must be a square matrix.");
    } else if (numCol <= 6) {
        return numCol >= 2 ? UnrolledDeterminantFromMinor.det(mat) : mat.get(0);
    } else {
        LUDecompositionAlt_D64 alg = new LUDecompositionAlt_D64();
        if (alg.inputModified()) {
            mat = mat.copy();
        }

        return !alg.decompose(mat) ? 0.0D : alg.computeDeterminant().real;
    }
}
 
Example 3
Source File: MissingOps.java    From beast-mcmc with GNU Lesser General Public License v2.1 6 votes vote down vote up
public static double invertAndGetDeterminant(DenseMatrix64F mat, DenseMatrix64F result, boolean log) {

        final int numCol = mat.getNumCols();
        final int numRow = mat.getNumRows();
        if (numCol != numRow) {
            throw new IllegalArgumentException("Must be a square matrix.");
        }

        if (numCol <= 5) {

            if (numCol >= 2) {
                UnrolledInverseFromMinor.inv(mat, result);
            } else {
                result.set(0, 1.0D / mat.get(0));
            }

            double det = numCol >= 2 ?
                    UnrolledDeterminantFromMinor.det(mat) :
                    mat.get(0);
            return log ? Math.log(det) : det;

        } else {

            LUDecompositionAlt_D64 alg = new LUDecompositionAlt_D64();
            LinearSolverLu_D64 solver = new LinearSolverLu_D64(alg);
            if (solver.modifiesA()) {
                mat = mat.copy();
            }

            if (!solver.setA(mat)) {
                return Double.NaN;
            }

            solver.invert(result);

            return log ? computeLogDeterminant(alg) : alg.computeDeterminant().real;

        }
    }
 
Example 4
Source File: MultipleLinearRegressionModel.java    From java-timeseries with MIT License 5 votes vote down vote up
private double[][] getXtXInverse(MatrixFormulation matrixFormulation) {
    DenseMatrix64F XtXInvMatrix = matrixFormulation.XtXInv.copy();
    int dim = XtXInvMatrix.getNumCols();
    double[][] XtXInvArray = new double[dim][dim];
    for (int i = 0; i < dim; i++) {
        for (int j = 0; j < dim; j++) {
            XtXInvArray[i][j] = XtXInvMatrix.get(i, j);
        }
    }
    return XtXInvArray;
}
 
Example 5
Source File: MissingOps.java    From beast-mcmc with GNU Lesser General Public License v2.1 5 votes vote down vote up
public static void blockUnwrap(final DenseMatrix64F block, final double[] destination,
                               final int destinationOffset,
                               final int offsetRow, final int offsetCol,
                               final int nCols) {
    for (int i = 0; i < block.getNumRows(); i++) { // Rows
        for (int j = 0; j < block.getNumCols(); j++) {
            destination[destinationOffset + (i + offsetRow) * nCols + j + offsetCol] = block.get(i, j);
        }
    }
}
 
Example 6
Source File: MissingOps.java    From beast-mcmc with GNU Lesser General Public License v2.1 5 votes vote down vote up
public static boolean anyDiagonalInfinities(DenseMatrix64F source) {
    boolean anyInfinities = false;
    for (int i = 0; i < source.getNumCols() && !anyInfinities; ++i) {
        if (Double.isInfinite(source.unsafe_get(i, i))) {
            anyInfinities = true;
        }
    }
    return anyInfinities;
}
 
Example 7
Source File: MissingOps.java    From beast-mcmc with GNU Lesser General Public License v2.1 5 votes vote down vote up
public static boolean allFiniteDiagonals(DenseMatrix64F source) {
    boolean allFinite = true;

    final int length = source.getNumCols();
    for (int i = 0; i < length; ++i) {
        allFinite &= !Double.isInfinite(source.unsafe_get(i, i));
    }
    return allFinite;
}
 
Example 8
Source File: MissingOps.java    From beast-mcmc with GNU Lesser General Public License v2.1 5 votes vote down vote up
public static int countFiniteDiagonals(DenseMatrix64F source) {
    final int length = source.getNumCols();

    int count = 0;
    for (int i = 0; i < length; ++i) {
        final double d = source.unsafe_get(i, i);
        if (!Double.isInfinite(d)) {
            ++count;
        }
    }
    return count;
}
 
Example 9
Source File: MissingOps.java    From beast-mcmc with GNU Lesser General Public License v2.1 5 votes vote down vote up
public static int countZeroDiagonals(DenseMatrix64F source) {
    final int length = source.getNumCols();

    int count = 0;
    for (int i = 0; i < length; ++i) {
        final double d = source.unsafe_get(i, i);
        if (d == 0.0) {
            ++count;
        }
    }
    return count;
}
 
Example 10
Source File: MissingOps.java    From beast-mcmc with GNU Lesser General Public License v2.1 5 votes vote down vote up
public static boolean allZeroDiagonals(DenseMatrix64F source) {
    final int length = source.getNumCols();

    for (int i = 0; i < length; ++i) {
        if (source.unsafe_get(i, i) != 0.0) {
            return false;
        }
    }
    return true;
}
 
Example 11
Source File: MissingOps.java    From beast-mcmc with GNU Lesser General Public License v2.1 5 votes vote down vote up
public static void getFiniteDiagonalIndices(final DenseMatrix64F source, final int[] indices) {
    final int length = source.getNumCols();

    int index = 0;
    for (int i = 0; i < length; ++i) {
        final double d = source.unsafe_get(i, i);
        if (!Double.isInfinite(d)) {
            indices[index] = i;
            ++index;
        }
    }
}
 
Example 12
Source File: MissingOps.java    From beast-mcmc with GNU Lesser General Public License v2.1 5 votes vote down vote up
public static int countFiniteNonZeroDiagonals(DenseMatrix64F source) {
    final int length = source.getNumCols();

    int count = 0;
    for (int i = 0; i < length; ++i) {
        final double d = source.unsafe_get(i, i);
        if (!Double.isInfinite(d) && d != 0.0) {
            ++count;
        }
    }
    return count;
}
 
Example 13
Source File: MissingOps.java    From beast-mcmc with GNU Lesser General Public License v2.1 5 votes vote down vote up
public static void getFiniteNonZeroDiagonalIndices(final DenseMatrix64F source, final int[] indices) {
    final int length = source.getNumCols();

    int index = 0;
    for (int i = 0; i < length; ++i) {
        final double d = source.unsafe_get(i, i);
        if (!Double.isInfinite(d) && d != 0.0) {
            indices[index] = i;
            ++index;
        }
    }
}
 
Example 14
Source File: ContinuousTraitGradientForBranch.java    From beast-mcmc with GNU Lesser General Public License v2.1 5 votes vote down vote up
private static void removeMissing(DenseMatrix64F M, int[] missing) {
    for (int m : missing) {
        for (int j = 0; j < M.getNumCols(); j++) {
            M.unsafe_set(m, j, 0.0);
            M.unsafe_set(j, m, 0.0);
        }
    }
}
 
Example 15
Source File: MissingOps.java    From beast-mcmc with GNU Lesser General Public License v2.1 4 votes vote down vote up
public static InversionResult safeDeterminant(DenseMatrix64F source, boolean invert) {
        final int finiteCount = countFiniteNonZeroDiagonals(source);

        InversionResult result;

        if (finiteCount == 0) {
            result = new InversionResult(NOT_OBSERVED, 0, Double.NEGATIVE_INFINITY, true);
        } else {
//            LinearSolver<DenseMatrix64F> solver = LinearSolverFactory.pseudoInverse(true);
//            solver.setA(source);
//
//            SingularValueDecomposition<DenseMatrix64F> svd = solver.getDecomposition();
//            double[] values = svd.getSingularValues();
//
//            if (values == null) {
//                throw new RuntimeException("Unable to perform SVD");
//            }

            SingularValueDecomposition<DenseMatrix64F> svd = DecompositionFactory.svd(source.getNumRows(), source.getNumCols(), false, false, false);
            if (!svd.decompose(source)) {
                if (SingularOps.rank(svd) == 0)
                    return new InversionResult(NOT_OBSERVED, 0, Double.NEGATIVE_INFINITY, true);
                throw new RuntimeException("SVD decomposition failed");
            }
            double[] values = svd.getSingularValues();
            double tol = SingularOps.singularThreshold(svd);
//            double tol = 0.0;

            int dim = 0;
            double logDet = 0;
            for (int i = 0; i < values.length; i++) {
                final double lambda = values[i];
                if (lambda > tol) {
                    logDet += Math.log(lambda);
                    ++dim;
                }
            }

            if (invert) {
                logDet = -logDet;
            }

            result = new InversionResult(dim == source.getNumCols() ? FULLY_OBSERVED : PARTIALLY_OBSERVED, dim, logDet, true);
        }

        return result;
    }