Search in sources :

Example 1 with CardinalityException

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

the class CholeskyDecomposition method solve.

/**
     * Solve the linear equation A × X = B for matrices A.
     *
     * @param b right-hand side of the equation A × X = B
     * @return a vector X that minimizes the two norm of A × X - B
     * @throws CardinalityException if the vectors dimensions do not match
     */
public Vector solve(final Vector b) {
    final int m = lTData.length;
    if (b.size() != m)
        throw new CardinalityException(b.size(), m);
    final double[] x = b.getStorage().data();
    // Solve LY = b
    for (int j = 0; j < m; j++) {
        final double[] lJ = lTData[j];
        x[j] /= lJ[j];
        final double xJ = x[j];
        for (int i = j + 1; i < m; i++) x[i] -= xJ * lJ[i];
    }
    // Solve LTX = Y
    for (int j = m - 1; j >= 0; j--) {
        x[j] /= lTData[j][j];
        final double xJ = x[j];
        for (int i = 0; i < j; i++) x[i] -= xJ * lTData[i][j];
    }
    return likeVector(origin, m).assign(x);
}
Also used : CardinalityException(org.apache.ignite.ml.math.exceptions.CardinalityException)

Example 2 with CardinalityException

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

the class CholeskyDecomposition method solve.

/**
     * Solve the linear equation A &times; X = B for matrices A.
     *
     * @param b right-hand side of the equation A &times; X = B
     * @return a matrix X that minimizes the two norm of A &times; X - B
     * @throws CardinalityException if the matrices dimensions do not match
     */
public Matrix solve(final Matrix b) {
    final int m = lTData.length;
    if (b.rowSize() != m)
        throw new CardinalityException(b.rowSize(), m);
    final int nColB = b.columnSize();
    final double[][] x = b.getStorage().data();
    // Solve LY = b
    for (int j = 0; j < m; j++) {
        final double[] lJ = lTData[j];
        final double lJJ = lJ[j];
        final double[] xJ = x[j];
        for (int k = 0; k < nColB; ++k) xJ[k] /= lJJ;
        for (int i = j + 1; i < m; i++) {
            final double[] xI = x[i];
            final double lJI = lJ[i];
            for (int k = 0; k < nColB; ++k) xI[k] -= xJ[k] * lJI;
        }
    }
    // Solve LTX = Y
    for (int j = m - 1; j >= 0; j--) {
        final double lJJ = lTData[j][j];
        final double[] xJ = x[j];
        for (int k = 0; k < nColB; ++k) xJ[k] /= lJJ;
        for (int i = 0; i < j; i++) {
            final double[] xI = x[i];
            final double lIJ = lTData[i][j];
            for (int k = 0; k < nColB; ++k) xI[k] -= xJ[k] * lIJ;
        }
    }
    return like(origin, m, b.columnSize()).assign(x);
}
Also used : CardinalityException(org.apache.ignite.ml.math.exceptions.CardinalityException)

Example 3 with CardinalityException

use of org.apache.ignite.ml.math.exceptions.CardinalityException 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 4 with CardinalityException

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

the class AbstractMatrix method assignRow.

/** {@inheritDoc} */
@Override
public Matrix assignRow(int row, Vector vec) {
    checkRowIndex(row);
    int cols = columnSize();
    if (cols != vec.size())
        throw new CardinalityException(cols, vec.size());
    if (sto.isArrayBased() && vec.getStorage().isArrayBased())
        System.arraycopy(vec.getStorage().data(), 0, sto.data()[row], 0, cols);
    else
        for (int y = 0; y < cols; y++) storageSet(row, y, vec.getX(y));
    return this;
}
Also used : CardinalityException(org.apache.ignite.ml.math.exceptions.CardinalityException)

Example 5 with CardinalityException

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

the class AbstractMatrix method times.

/** {@inheritDoc} */
@Override
public Matrix times(Matrix mtx) {
    int cols = columnSize();
    if (cols != mtx.rowSize())
        throw new CardinalityException(cols, mtx.rowSize());
    int rows = rowSize();
    int mtxCols = mtx.columnSize();
    Matrix res = like(rows, mtxCols);
    for (int x = 0; x < rows; x++) for (int y = 0; y < mtxCols; y++) {
        double sum = 0.0;
        for (int k = 0; k < cols; k++) sum += getX(x, k) * mtx.getX(k, y);
        res.setX(x, y, sum);
    }
    return res;
}
Also used : Matrix(org.apache.ignite.ml.math.Matrix) CardinalityException(org.apache.ignite.ml.math.exceptions.CardinalityException)

Aggregations

CardinalityException (org.apache.ignite.ml.math.exceptions.CardinalityException)11 Matrix (org.apache.ignite.ml.math.Matrix)2 SingularMatrixException (org.apache.ignite.ml.math.exceptions.SingularMatrixException)2 Vector (org.apache.ignite.ml.math.Vector)1 LUDecomposition (org.apache.ignite.ml.math.decompositions.LUDecomposition)1 InsufficientDataException (org.apache.ignite.ml.math.exceptions.InsufficientDataException)1 NullArgumentException (org.apache.ignite.ml.math.exceptions.NullArgumentException)1