Search in sources :

Example 1 with SparseLocalVector

use of org.apache.ignite.ml.math.impls.vector.SparseLocalVector 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;
    DenseLocalOnHeapMatrix a = new DenseLocalOnHeapMatrix(new double[][] { { 10.0, 11.0 }, { 0.0, 1.0 } }, 2);
    SparseLocalVector x = sparseFromArray(new double[] { 1.0, 2.0 });
    double beta = 2.0;
    DenseLocalOnHeapVector y = new DenseLocalOnHeapVector(new double[] { 3.0, 4.0 });
    DenseLocalOnHeapVector exp = (DenseLocalOnHeapVector) y.times(beta).plus(a.times(x).times(alpha));
    Blas.gemv(alpha, a, x, beta, y);
    Assert.assertEquals(exp, y);
}
Also used : DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix) SparseLocalVector(org.apache.ignite.ml.math.impls.vector.SparseLocalVector) Test(org.junit.Test)

Example 2 with SparseLocalVector

use of org.apache.ignite.ml.math.impls.vector.SparseLocalVector 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;
    SparseLocalVector v = sparseFromArray(data);
    Vector exp = sparseFromArray(data).times(alpha);
    Blas.scal(alpha, v);
    Assert.assertEquals(v, exp);
}
Also used : SparseLocalVector(org.apache.ignite.ml.math.impls.vector.SparseLocalVector) SparseLocalVector(org.apache.ignite.ml.math.impls.vector.SparseLocalVector) DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) Test(org.junit.Test)

Example 3 with SparseLocalVector

use of org.apache.ignite.ml.math.impls.vector.SparseLocalVector 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;
    SparseLocalOnHeapMatrix a = (SparseLocalOnHeapMatrix) new SparseLocalOnHeapMatrix(2, 2).assign(new double[][] { { 10.0, 11.0 }, { 0.0, 1.0 } });
    SparseLocalVector x = sparseFromArray(new double[] { 1.0, 2.0 });
    double beta = 2.0;
    DenseLocalOnHeapVector y = new DenseLocalOnHeapVector(new double[] { 3.0, 4.0 });
    DenseLocalOnHeapVector exp = (DenseLocalOnHeapVector) y.times(beta).plus(a.times(x).times(alpha));
    Blas.gemv(alpha, a, x, beta, y);
    Assert.assertEquals(exp, y);
}
Also used : DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) SparseLocalVector(org.apache.ignite.ml.math.impls.vector.SparseLocalVector) SparseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.SparseLocalOnHeapMatrix) Test(org.junit.Test)

Example 4 with SparseLocalVector

use of org.apache.ignite.ml.math.impls.vector.SparseLocalVector in project ignite by apache.

the class SparseVectorExample method main.

/**
 * Executes example.
 *
 * @param args Command line arguments, none required.
 */
public static void main(String[] args) {
    System.out.println();
    System.out.println(">>> Sparse vector API usage example started.");
    System.out.println("\n>>> Creating perpendicular sparse vectors.");
    double[] data1 = new double[] { 1, 0, 3, 0, 5, 0 };
    double[] data2 = new double[] { 0, 2, 0, 4, 0, 6 };
    Vector v1 = new SparseLocalVector(data1.length, RANDOM_ACCESS_MODE);
    Vector v2 = new SparseLocalVector(data2.length, RANDOM_ACCESS_MODE);
    v1.assign(data1);
    v2.assign(data2);
    System.out.println(">>> First vector: " + Arrays.toString(data1));
    System.out.println(">>> Second vector: " + Arrays.toString(data2));
    double dotProduct = v1.dot(v2);
    boolean dotProductIsAsExp = dotProduct == 0;
    System.out.println("\n>>> Dot product of vectors: [" + dotProduct + "], it is 0 as expected: [" + dotProductIsAsExp + "].");
    Vector hypotenuse = v1.plus(v2);
    System.out.println("\n>>> Hypotenuse (sum of vectors): " + Arrays.toString(hypotenuse.getStorage().data()));
    double lenSquared1 = v1.getLengthSquared();
    double lenSquared2 = v2.getLengthSquared();
    double lenSquaredHypotenuse = hypotenuse.getLengthSquared();
    boolean lenSquaredHypotenuseIsAsExp = lenSquaredHypotenuse == lenSquared1 + lenSquared2;
    System.out.println(">>> Squared length of first vector: [" + lenSquared1 + "].");
    System.out.println(">>> Squared length of second vector: [" + lenSquared2 + "].");
    System.out.println(">>> Squared length of hypotenuse: [" + lenSquaredHypotenuse + "], equals sum of squared lengths of two original vectors as expected: [" + lenSquaredHypotenuseIsAsExp + "].");
    System.out.println("\n>>> Sparse vector API usage example completed.");
}
Also used : Vector(org.apache.ignite.ml.math.Vector) SparseLocalVector(org.apache.ignite.ml.math.impls.vector.SparseLocalVector) SparseLocalVector(org.apache.ignite.ml.math.impls.vector.SparseLocalVector)

Example 5 with SparseLocalVector

use of org.apache.ignite.ml.math.impls.vector.SparseLocalVector in project ignite by apache.

the class BlasTest method testAxpySparseArray.

/**
 * Test 'axpy' operation for sparse vector and array-based vector.
 */
@Test
public void testAxpySparseArray() {
    DenseLocalOnHeapVector y = new DenseLocalOnHeapVector(new double[] { 1.0, 2.0 });
    double a = 2.0;
    SparseLocalVector x = sparseFromArray(new double[] { 1.0, 2.0 });
    SparseLocalVector exp = (SparseLocalVector) x.times(a).plus(y);
    Blas.axpy(a, x, y);
    Assert.assertTrue(elementsEqual(exp, y));
}
Also used : DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) SparseLocalVector(org.apache.ignite.ml.math.impls.vector.SparseLocalVector) Test(org.junit.Test)

Aggregations

SparseLocalVector (org.apache.ignite.ml.math.impls.vector.SparseLocalVector)9 DenseLocalOnHeapVector (org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector)7 Test (org.junit.Test)6 DenseLocalOnHeapMatrix (org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix)3 CardinalityException (org.apache.ignite.ml.math.exceptions.CardinalityException)2 Vector (org.apache.ignite.ml.math.Vector)1 MathIllegalArgumentException (org.apache.ignite.ml.math.exceptions.MathIllegalArgumentException)1 NonSquareMatrixException (org.apache.ignite.ml.math.exceptions.NonSquareMatrixException)1 SparseLocalOnHeapMatrix (org.apache.ignite.ml.math.impls.matrix.SparseLocalOnHeapMatrix)1