Search in sources :

Example 16 with DenseVector

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

the class BlasTest method testDot.

/**
 * Test 'dot' operation.
 */
@Test
public void testDot() {
    DenseVector v1 = new DenseVector(new double[] { 1.0, 1.0 });
    DenseVector v2 = new DenseVector(new double[] { 2.0, 2.0 });
    Assert.assertEquals(Blas.dot(v1, v2), v1.dot(v2), 0.0);
}
Also used : DenseVector(org.apache.ignite.ml.math.primitives.vector.impl.DenseVector) Test(org.junit.Test)

Example 17 with DenseVector

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

the class BlasTest method testGemvSparseDenseDense.

/**
 * Tests 'gemv' operation for dense matrix A, dense vector x and dense vector y.
 */
@Test
public void testGemvSparseDenseDense() {
    // 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 } });
    DenseVector x = new DenseVector(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) DenseVector(org.apache.ignite.ml.math.primitives.vector.impl.DenseVector) Test(org.junit.Test)

Example 18 with DenseVector

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

the class BlasTest method testSyrDenseDense.

/**
 * Tests 'syr' operation for dense vector x and dense matrix A.
 */
@Test
public void testSyrDenseDense() {
    double alpha = 2.0;
    DenseVector x = new DenseVector(new double[] { 1.0, 2.0 });
    DenseMatrix a = new DenseMatrix(new double[][] { { 10.0, 20.0 }, { 20.0, 10.0 } });
    // alpha * x * x^T + A
    DenseMatrix exp = (DenseMatrix) new DenseMatrix(new double[][] { { 1.0, 2.0 }, { 2.0, 4.0 } }).times(alpha).plus(a);
    Blas.syr(alpha, x, a);
    Assert.assertEquals(exp, a);
}
Also used : 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 19 with DenseVector

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

the class BlasTest method testSprDenseDense.

/**
 * Test 'spr' operation for dense vector v and dense matrix A.
 */
@Test
public void testSprDenseDense() {
    double alpha = 3.0;
    DenseVector v = new DenseVector(new double[] { 1.0, 2.0 });
    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 }, { 2.0, 4.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 : 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 20 with DenseVector

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

Aggregations

DenseVector (org.apache.ignite.ml.math.primitives.vector.impl.DenseVector)101 Vector (org.apache.ignite.ml.math.primitives.vector.Vector)59 Test (org.junit.Test)59 Serializable (java.io.Serializable)16 SparseVector (org.apache.ignite.ml.math.primitives.vector.impl.SparseVector)14 HashMap (java.util.HashMap)13 DenseMatrix (org.apache.ignite.ml.math.primitives.matrix.impl.DenseMatrix)13 DummyVectorizer (org.apache.ignite.ml.dataset.feature.extractor.impl.DummyVectorizer)10 LabeledVector (org.apache.ignite.ml.structures.LabeledVector)10 RendezvousAffinityFunction (org.apache.ignite.cache.affinity.rendezvous.RendezvousAffinityFunction)9 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)9 HashSet (java.util.HashSet)7 TrainerTest (org.apache.ignite.ml.common.TrainerTest)7 KMeansModel (org.apache.ignite.ml.clustering.kmeans.KMeansModel)5 LocalDatasetBuilder (org.apache.ignite.ml.dataset.impl.local.LocalDatasetBuilder)5 EuclideanDistance (org.apache.ignite.ml.math.distances.EuclideanDistance)5 IgniteDifferentiableVectorToDoubleFunction (org.apache.ignite.ml.math.functions.IgniteDifferentiableVectorToDoubleFunction)5 MLPArchitecture (org.apache.ignite.ml.nn.architecture.MLPArchitecture)5 OneHotEncoderPreprocessor (org.apache.ignite.ml.preprocessing.encoding.onehotencoder.OneHotEncoderPreprocessor)4 Random (java.util.Random)3