Search in sources :

Example 1 with CardinalityException

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

Example 2 with CardinalityException

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

the class Blas method syr.

/**
 * A := alpha * x * x^T + A.
 *
 * @param alpha a real scalar that will be multiplied to x * x^T^.
 * @param x the vector x that contains the n elements.
 * @param a the symmetric matrix A. Size of n x n.
 */
void syr(Double alpha, Vector x, DenseMatrix a) {
    int mA = a.rowSize();
    int nA = a.columnSize();
    if (mA != nA)
        throw new NonSquareMatrixException(mA, nA);
    if (mA != x.size())
        throw new CardinalityException(x.size(), mA);
    // TODO: IGNITE-5535, Process DenseLocalOffHeapVector
    if (x instanceof DenseVector)
        syr(alpha, x, a);
    else if (x instanceof SparseVector)
        syr(alpha, x, a);
    else
        throw new IllegalArgumentException("Operation 'syr' does not support vector [class=" + x.getClass().getName() + "].");
}
Also used : NonSquareMatrixException(org.apache.ignite.ml.math.exceptions.math.NonSquareMatrixException) CardinalityException(org.apache.ignite.ml.math.exceptions.math.CardinalityException) SparseVector(org.apache.ignite.ml.math.primitives.vector.impl.SparseVector) DenseVector(org.apache.ignite.ml.math.primitives.vector.impl.DenseVector) MathIllegalArgumentException(org.apache.ignite.ml.math.exceptions.math.MathIllegalArgumentException)

Example 3 with CardinalityException

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

the class AbstractMatrix method setColumn.

/**
 * {@inheritDoc}
 */
@Override
public Matrix setColumn(int col, double[] data) {
    checkColumnIndex(col);
    int rows = rowSize();
    if (rows != data.length)
        throw new CardinalityException(rows, data.length);
    for (int x = 0; x < rows; x++) setX(x, col, data[x]);
    return this;
}
Also used : CardinalityException(org.apache.ignite.ml.math.exceptions.math.CardinalityException)

Example 4 with CardinalityException

use of org.apache.ignite.ml.math.exceptions.math.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());
    Matrix res = like(rowSize(), mtx.columnSize());
    Blas.gemm(1, this, mtx, 0, res);
    return res;
}
Also used : VectorizedViewMatrix(org.apache.ignite.ml.math.primitives.vector.impl.VectorizedViewMatrix) CardinalityException(org.apache.ignite.ml.math.exceptions.math.CardinalityException)

Example 5 with CardinalityException

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

the class LabeledDatasetLoader method parseFeatures.

/**
 */
@NotNull
private static Vector parseFeatures(Path pathToFile, boolean isFallOnBadData, int colSize, int rowIdx, String[] rowData) {
    final Vector vec = LabeledVectorSet.emptyVector(colSize);
    if (isFallOnBadData && rowData.length != colSize + 1)
        throw new CardinalityException(colSize + 1, rowData.length);
    double missedData = fillMissedData();
    for (int j = 0; j < colSize; j++) {
        try {
            double feature = Double.parseDouble(rowData[j + 1]);
            vec.set(j, feature);
        } catch (NumberFormatException e) {
            if (isFallOnBadData)
                throw new FileParsingException(rowData[j + 1], rowIdx, pathToFile);
            else
                vec.set(j, missedData);
        } catch (ArrayIndexOutOfBoundsException e) {
            vec.set(j, missedData);
        }
    }
    return vec;
}
Also used : FileParsingException(org.apache.ignite.ml.math.exceptions.datastructures.FileParsingException) CardinalityException(org.apache.ignite.ml.math.exceptions.math.CardinalityException) Vector(org.apache.ignite.ml.math.primitives.vector.Vector) LabeledVector(org.apache.ignite.ml.structures.LabeledVector) NotNull(org.jetbrains.annotations.NotNull)

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