Search in sources :

Example 1 with SparseVector

use of org.apache.ignite.ml.math.primitives.vector.impl.SparseVector 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 2 with SparseVector

use of org.apache.ignite.ml.math.primitives.vector.impl.SparseVector in project ignite by apache.

the class BlasTest method testGemvDenseSparseDense.

/**
 * Tests 'gemv' operation for dense matrix A, sparse vector x and dense vector y.
 */
@Test
public void testGemvDenseSparseDense() {
    // y := alpha * A * x + beta * y
    double alpha = 3.0;
    SparseMatrix a = (SparseMatrix) new SparseMatrix(2, 2).assign(new double[][] { { 10.0, 11.0 }, { 0.0, 1.0 } });
    SparseVector x = sparseFromArray(new double[] { 1.0, 2.0 });
    double beta = 2.0;
    DenseVector y = new DenseVector(new double[] { 3.0, 4.0 });
    DenseVector exp = (DenseVector) y.times(beta).plus(a.times(x).times(alpha));
    Blas.gemv(alpha, a, x, beta, y);
    Assert.assertEquals(exp, y);
}
Also used : SparseMatrix(org.apache.ignite.ml.math.primitives.matrix.impl.SparseMatrix) SparseVector(org.apache.ignite.ml.math.primitives.vector.impl.SparseVector) DenseVector(org.apache.ignite.ml.math.primitives.vector.impl.DenseVector) Test(org.junit.Test)

Example 3 with SparseVector

use of org.apache.ignite.ml.math.primitives.vector.impl.SparseVector in project ignite by apache.

the class BlasTest method testGemvSparseSparseDense.

/**
 * Tests 'gemv' operation for sparse matrix A, sparse vector x and dense vector y.
 */
@Test
public void testGemvSparseSparseDense() {
    // y := alpha * A * x + beta * y
    double alpha = 3.0;
    DenseMatrix a = new DenseMatrix(new double[][] { { 10.0, 11.0 }, { 0.0, 1.0 } }, 2);
    SparseVector x = sparseFromArray(new double[] { 1.0, 2.0 });
    double beta = 2.0;
    DenseVector y = new DenseVector(new double[] { 3.0, 4.0 });
    DenseVector exp = (DenseVector) y.times(beta).plus(a.times(x).times(alpha));
    Blas.gemv(alpha, a, x, beta, y);
    Assert.assertEquals(exp, y);
}
Also used : SparseVector(org.apache.ignite.ml.math.primitives.vector.impl.SparseVector) DenseVector(org.apache.ignite.ml.math.primitives.vector.impl.DenseVector) DenseMatrix(org.apache.ignite.ml.math.primitives.matrix.impl.DenseMatrix) Test(org.junit.Test)

Example 4 with SparseVector

use of org.apache.ignite.ml.math.primitives.vector.impl.SparseVector in project ignite by apache.

the class BlasTest method testScalSparse.

/**
 * Test 'scal' operation for sparse matrix.
 */
@Test
public void testScalSparse() {
    double[] data = new double[] { 1.0, 1.0 };
    double alpha = 2.0;
    SparseVector v = sparseFromArray(data);
    Vector exp = sparseFromArray(data).times(alpha);
    Blas.scal(alpha, v);
    Assert.assertEquals(v, exp);
}
Also used : SparseVector(org.apache.ignite.ml.math.primitives.vector.impl.SparseVector) SparseVector(org.apache.ignite.ml.math.primitives.vector.impl.SparseVector) Vector(org.apache.ignite.ml.math.primitives.vector.Vector) DenseVector(org.apache.ignite.ml.math.primitives.vector.impl.DenseVector) Test(org.junit.Test)

Example 5 with SparseVector

use of org.apache.ignite.ml.math.primitives.vector.impl.SparseVector in project ignite by apache.

the class Deltas method getStateVector.

/**
 * @param mdl Model.
 * @return vector of model weights with intercept.
 */
private Vector getStateVector(SVMLinearClassificationModel mdl) {
    double intercept = mdl.intercept();
    Vector weights = mdl.weights();
    int stateVectorSize = weights.size() + 1;
    Vector res = weights.isDense() ? new DenseVector(stateVectorSize) : new SparseVector(stateVectorSize);
    res.set(0, intercept);
    weights.nonZeroes().forEach(ith -> res.set(ith.index(), ith.get()));
    return res;
}
Also used : SparseVector(org.apache.ignite.ml.math.primitives.vector.impl.SparseVector) Vector(org.apache.ignite.ml.math.primitives.vector.Vector) SparseVector(org.apache.ignite.ml.math.primitives.vector.impl.SparseVector) LabeledVector(org.apache.ignite.ml.structures.LabeledVector) DenseVector(org.apache.ignite.ml.math.primitives.vector.impl.DenseVector) DenseVector(org.apache.ignite.ml.math.primitives.vector.impl.DenseVector)

Aggregations

SparseVector (org.apache.ignite.ml.math.primitives.vector.impl.SparseVector)11 DenseVector (org.apache.ignite.ml.math.primitives.vector.impl.DenseVector)9 Test (org.junit.Test)6 DenseMatrix (org.apache.ignite.ml.math.primitives.matrix.impl.DenseMatrix)3 CardinalityException (org.apache.ignite.ml.math.exceptions.math.CardinalityException)2 Vector (org.apache.ignite.ml.math.primitives.vector.Vector)2 DelegatingNamedVector (org.apache.ignite.ml.math.primitives.vector.impl.DelegatingNamedVector)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Objects (java.util.Objects)1 MathIllegalArgumentException (org.apache.ignite.ml.math.exceptions.math.MathIllegalArgumentException)1 NonSquareMatrixException (org.apache.ignite.ml.math.exceptions.math.NonSquareMatrixException)1 SparseMatrix (org.apache.ignite.ml.math.primitives.matrix.impl.SparseMatrix)1 LabeledVector (org.apache.ignite.ml.structures.LabeledVector)1