Search in sources :

Example 6 with SparseVector

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

Example 7 with SparseVector

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

the class VectorUtils method of.

/**
 * Creates vector based on array of Doubles. If array contains null-elements then
 * method returns sparse local on head vector. In other case method returns
 * dense local on heap vector.
 *
 * @param values Values.
 */
public static Vector of(Double[] values) {
    A.notNull(values, "values");
    Vector answer;
    if (Arrays.stream(values).anyMatch(Objects::isNull))
        answer = new SparseVector(values.length);
    else
        answer = new DenseVector(values.length);
    for (int i = 0; i < values.length; i++) if (values[i] != null)
        answer.set(i, values[i]);
    return answer;
}
Also used : Objects(java.util.Objects) SparseVector(org.apache.ignite.ml.math.primitives.vector.impl.SparseVector) SparseVector(org.apache.ignite.ml.math.primitives.vector.impl.SparseVector) DelegatingNamedVector(org.apache.ignite.ml.math.primitives.vector.impl.DelegatingNamedVector) DenseVector(org.apache.ignite.ml.math.primitives.vector.impl.DenseVector) DenseVector(org.apache.ignite.ml.math.primitives.vector.impl.DenseVector)

Example 8 with SparseVector

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

the class VectorUtils method of.

/**
 * Creates named vector based on map of keys and values.
 *
 * @param values Values.
 * @return Named vector.
 */
public static NamedVector of(Map<String, Double> values) {
    SparseVector vector = new SparseVector(values.size());
    for (int i = 0; i < values.size(); i++) vector.set(i, Double.NaN);
    Map<String, Integer> dict = new HashMap<>();
    int idx = 0;
    for (Map.Entry<String, Double> e : values.entrySet()) {
        dict.put(e.getKey(), idx);
        vector.set(idx, e.getValue());
        idx++;
    }
    return new DelegatingNamedVector(vector, dict);
}
Also used : HashMap(java.util.HashMap) DelegatingNamedVector(org.apache.ignite.ml.math.primitives.vector.impl.DelegatingNamedVector) SparseVector(org.apache.ignite.ml.math.primitives.vector.impl.SparseVector) Map(java.util.Map) HashMap(java.util.HashMap)

Example 9 with SparseVector

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

the class BlasTest method testAxpySparseArray.

/**
 * Test 'axpy' operation for sparse vector and array-based vector.
 */
@Test
public void testAxpySparseArray() {
    DenseVector y = new DenseVector(new double[] { 1.0, 2.0 });
    double a = 2.0;
    SparseVector x = sparseFromArray(new double[] { 1.0, 2.0 });
    SparseVector exp = (SparseVector) x.times(a).plus(y);
    Blas.axpy(a, x, y);
    Assert.assertTrue(elementsEqual(exp, y));
}
Also used : 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 10 with SparseVector

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

the class BlasTest method testSprSparseDense2.

/**
 * Test 'spr' operation for sparse vector v (sparse in representation, sparse in fact) and dense matrix A.
 */
@Test
public void testSprSparseDense2() {
    double alpha = 3.0;
    SparseVector v = new SparseVector(2);
    v.set(0, 1);
    DenseVector u = new DenseVector(new double[] { 3.0, 13.0, 20.0, 0.0 });
    // m is alpha * v * v^t
    DenseMatrix m = (DenseMatrix) new DenseMatrix(new double[][] { { 1.0, 0.0 }, { 0.0, 0.0 } }, StorageConstants.COLUMN_STORAGE_MODE).times(alpha);
    DenseMatrix a = new DenseMatrix(new double[][] { { 3.0, 0.0 }, { 13.0, 20.0 } }, StorageConstants.COLUMN_STORAGE_MODE);
    // m := alpha * v * v.t + A
    Blas.spr(alpha, v, u);
    DenseMatrix mu = fromVector(u, a.rowSize(), StorageConstants.COLUMN_STORAGE_MODE, (i, j) -> i >= j);
    Assert.assertEquals(m.plus(a), mu);
}
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)

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