Search in sources :

Example 1 with SingularMatrixException

use of org.apache.ignite.ml.math.exceptions.SingularMatrixException in project ignite by apache.

the class LUDecomposition method solve.

/**
     * @param b Matrix to solve using this decomposition.
     * @return Solution matrix.
     */
public Matrix solve(Matrix b) {
    final int m = pivot.size();
    if (b.rowSize() != m)
        throw new CardinalityException(b.rowSize(), m);
    if (singular)
        throw new SingularMatrixException();
    final int nColB = b.columnSize();
    // Apply permutations to b
    final double[][] bp = new double[m][nColB];
    for (int row = 0; row < m; row++) {
        final double[] bpRow = bp[row];
        final int pRow = (int) pivot.get(row);
        for (int col = 0; col < nColB; col++) bpRow[col] = b.get(pRow, col);
    }
    // Solve LY = b
    for (int col = 0; col < m; col++) {
        final double[] bpCol = bp[col];
        for (int i = col + 1; i < m; i++) {
            final double[] bpI = bp[i];
            final double luICol = lu.get(i, col);
            for (int j = 0; j < nColB; j++) bpI[j] -= bpCol[j] * luICol;
        }
    }
    // Solve UX = Y
    for (int col = m - 1; col >= 0; col--) {
        final double[] bpCol = bp[col];
        final double luDiag = lu.getX(col, col);
        for (int j = 0; j < nColB; j++) bpCol[j] /= luDiag;
        for (int i = 0; i < col; i++) {
            final double[] bpI = bp[i];
            final double luICol = lu.get(i, col);
            for (int j = 0; j < nColB; j++) bpI[j] -= bpCol[j] * luICol;
        }
    }
    return b.like(b.rowSize(), b.columnSize()).assign(bp);
}
Also used : SingularMatrixException(org.apache.ignite.ml.math.exceptions.SingularMatrixException) CardinalityException(org.apache.ignite.ml.math.exceptions.CardinalityException)

Example 2 with SingularMatrixException

use of org.apache.ignite.ml.math.exceptions.SingularMatrixException in project ignite by apache.

the class LUDecomposition method solve.

/**
     * @param b Vector to solve using this decomposition.
     * @return Solution vector.
     */
public Vector solve(Vector b) {
    final int m = pivot.size();
    if (b.size() != m)
        throw new CardinalityException(b.size(), m);
    if (singular)
        throw new SingularMatrixException();
    final double[] bp = new double[m];
    // Apply permutations to b
    for (int row = 0; row < m; row++) bp[row] = b.get((int) pivot.get(row));
    // Solve LY = b
    for (int col = 0; col < m; col++) {
        final double bpCol = bp[col];
        for (int i = col + 1; i < m; i++) bp[i] -= bpCol * lu.get(i, col);
    }
    // Solve UX = Y
    for (int col = m - 1; col >= 0; col--) {
        bp[col] /= lu.get(col, col);
        final double bpCol = bp[col];
        for (int i = 0; i < col; i++) bp[i] -= bpCol * lu.get(i, col);
    }
    return b.like(m).assign(bp);
}
Also used : SingularMatrixException(org.apache.ignite.ml.math.exceptions.SingularMatrixException) CardinalityException(org.apache.ignite.ml.math.exceptions.CardinalityException)

Aggregations

CardinalityException (org.apache.ignite.ml.math.exceptions.CardinalityException)2 SingularMatrixException (org.apache.ignite.ml.math.exceptions.SingularMatrixException)2