Search in sources :

Example 11 with CardinalityException

use of org.apache.ignite.ml.math.exceptions.math.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.math.SingularMatrixException) CardinalityException(org.apache.ignite.ml.math.exceptions.math.CardinalityException)

Example 12 with CardinalityException

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

the class Blas method gemv.

/**
 * y := alpha * A * x + beta * y.
 *
 * @param alpha Alpha.
 * @param a Matrix a.
 * @param x Vector x.
 * @param beta Beta.
 * @param y Vector y.
 */
public static void gemv(double alpha, Matrix a, Vector x, double beta, Vector y) {
    checkCardinality(a, x);
    if (a.rowSize() != y.size())
        throw new CardinalityException(a.columnSize(), y.size());
    if (alpha == 0.0 && beta == 1.0)
        return;
    if (alpha == 0.0) {
        scal(y, beta);
        return;
    }
    double[] fA = a.getStorage().data();
    double[] fX = x.getStorage().data();
    double[] fY = y.getStorage().data();
    nativeBlas.dgemv("N", a.rowSize(), a.columnSize(), alpha, fA, a.rowSize(), fX, 1, beta, fY, 1);
    if (y instanceof SparseVector)
        y.assign(fY);
}
Also used : CardinalityException(org.apache.ignite.ml.math.exceptions.math.CardinalityException) SparseVector(org.apache.ignite.ml.math.primitives.vector.impl.SparseVector)

Aggregations

CardinalityException (org.apache.ignite.ml.math.exceptions.math.CardinalityException)12 DenseVector (org.apache.ignite.ml.math.primitives.vector.impl.DenseVector)4 SingularMatrixException (org.apache.ignite.ml.math.exceptions.math.SingularMatrixException)2 Vector (org.apache.ignite.ml.math.primitives.vector.Vector)2 SparseVector (org.apache.ignite.ml.math.primitives.vector.impl.SparseVector)2 FileParsingException (org.apache.ignite.ml.math.exceptions.datastructures.FileParsingException)1 MathIllegalArgumentException (org.apache.ignite.ml.math.exceptions.math.MathIllegalArgumentException)1 NonSquareMatrixException (org.apache.ignite.ml.math.exceptions.math.NonSquareMatrixException)1 VectorizedViewMatrix (org.apache.ignite.ml.math.primitives.vector.impl.VectorizedViewMatrix)1 LabeledVector (org.apache.ignite.ml.structures.LabeledVector)1 NotNull (org.jetbrains.annotations.NotNull)1