Search in sources :

Example 36 with DenseMatrix

use of org.apache.ignite.ml.math.primitives.matrix.impl.DenseMatrix in project ignite by apache.

the class BlasTest method testGemmSparseDenseDense.

/**
 * Tests 'gemm' operation for sparse matrix A, dense matrix B and dense matrix C.
 */
@Test
public void testGemmSparseDenseDense() {
    // C := alpha * A * B + beta * C
    double alpha = 1.0;
    SparseMatrix a = (SparseMatrix) new SparseMatrix(2, 2).assign(new double[][] { { 10.0, 11.0 }, { 0.0, 1.0 } });
    DenseMatrix b = new DenseMatrix(new double[][] { { 1.0, 0.3 }, { 0.0, 1.0 } });
    double beta = 0.0;
    DenseMatrix c = new DenseMatrix(new double[][] { { 1.0, 2.0 }, { 2.0, 3.0 } });
    // .times(alpha).plus(c.times(beta));
    Matrix exp = a.times(b);
    Blas.gemm(alpha, a, b, beta, c);
    Assert.assertTrue(Arrays.equals(exp.getStorage().data(), c.getStorage().data()));
}
Also used : SparseMatrix(org.apache.ignite.ml.math.primitives.matrix.impl.SparseMatrix) Matrix(org.apache.ignite.ml.math.primitives.matrix.Matrix) DenseMatrix(org.apache.ignite.ml.math.primitives.matrix.impl.DenseMatrix) SparseMatrix(org.apache.ignite.ml.math.primitives.matrix.impl.SparseMatrix) DenseMatrix(org.apache.ignite.ml.math.primitives.matrix.impl.DenseMatrix) Test(org.junit.Test)

Example 37 with DenseMatrix

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

Example 38 with DenseMatrix

use of org.apache.ignite.ml.math.primitives.matrix.impl.DenseMatrix in project ignite by apache.

the class BlasTest method testSprSparseDense1.

/**
 * Test 'spr' operation for sparse vector v (sparse in representation, dense in fact) and dense matrix A.
 */
@Test
public void testSprSparseDense1() {
    double alpha = 3.0;
    SparseVector v = sparseFromArray(new double[] { 1.0, 2.0 });
    DenseVector u = new DenseVector(new double[] { 3.0, 13.0, 20.0, 0.0 });
    DenseMatrix a = new DenseMatrix(new double[][] { { 3.0, 0.0 }, { 13.0, 20.0 } }, StorageConstants.COLUMN_STORAGE_MODE);
    DenseMatrix exp = (DenseMatrix) new DenseMatrix(new double[][] { { 1.0, 0.0 }, { 2.0, 4.0 } }, StorageConstants.COLUMN_STORAGE_MODE).times(alpha).plus(a);
    // 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(exp, 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)

Example 39 with DenseMatrix

use of org.apache.ignite.ml.math.primitives.matrix.impl.DenseMatrix in project ignite by apache.

the class LUDecompositionTest method setUp.

/**
 */
@Before
public void setUp() {
    double[][] rawMatrix = new double[][] { { 2.0d, 1.0d, 1.0d, 0.0d }, { 4.0d, 3.0d, 3.0d, 1.0d }, { 8.0d, 7.0d, 9.0d, 5.0d }, { 6.0d, 7.0d, 9.0d, 8.0d } };
    double[][] rawL = { { 1.0d, 0.0d, 0.0d, 0.0d }, { 3.0d / 4.0d, 1.0d, 0.0d, 0.0d }, { 1.0d / 2.0d, -2.0d / 7.0d, 1.0d, 0.0d }, { 1.0d / 4.0d, -3.0d / 7.0d, 1.0d / 3.0d, 1.0d } };
    double[][] rawU = { { 8.0d, 7.0d, 9.0d, 5.0d }, { 0.0d, 7.0d / 4.0d, 9.0d / 4.0d, 17.0d / 4.0d }, { 0.0d, 0.0d, -6.0d / 7.0d, -2.0d / 7.0d }, { 0.0d, 0.0d, 0.0d, 2.0d / 3.0d } };
    double[][] rawP = new double[][] { { 0, 0, 1.0d, 0 }, { 0, 0, 0, 1.0d }, { 0, 1.0d, 0, 0 }, { 1.0d, 0, 0, 0 } };
    rawPivot = new int[] { 3, 4, 2, 1 };
    testMatrix = new DenseMatrix(rawMatrix);
    testL = new DenseMatrix(rawL);
    testU = new DenseMatrix(rawU);
    testP = new DenseMatrix(rawP);
}
Also used : DenseMatrix(org.apache.ignite.ml.math.primitives.matrix.impl.DenseMatrix) Before(org.junit.Before)

Aggregations

DenseMatrix (org.apache.ignite.ml.math.primitives.matrix.impl.DenseMatrix)39 Test (org.junit.Test)28 Matrix (org.apache.ignite.ml.math.primitives.matrix.Matrix)14 DenseVector (org.apache.ignite.ml.math.primitives.vector.impl.DenseVector)14 MLPArchitecture (org.apache.ignite.ml.nn.architecture.MLPArchitecture)9 Vector (org.apache.ignite.ml.math.primitives.vector.Vector)8 SparseVector (org.apache.ignite.ml.math.primitives.vector.impl.SparseVector)4 MultivariateGaussianDistribution (org.apache.ignite.ml.math.stat.MultivariateGaussianDistribution)3 LabeledVector (org.apache.ignite.ml.structures.LabeledVector)3 RendezvousAffinityFunction (org.apache.ignite.cache.affinity.rendezvous.RendezvousAffinityFunction)2 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)2 EuclideanDistance (org.apache.ignite.ml.math.distances.EuclideanDistance)2 HammingDistance (org.apache.ignite.ml.math.distances.HammingDistance)2 ManhattanDistance (org.apache.ignite.ml.math.distances.ManhattanDistance)2 ViewMatrix (org.apache.ignite.ml.math.primitives.matrix.impl.ViewMatrix)2 VectorizedViewMatrix (org.apache.ignite.ml.math.primitives.vector.impl.VectorizedViewMatrix)2 MLPTrainer (org.apache.ignite.ml.nn.MLPTrainer)2 SimpleGDParameterUpdate (org.apache.ignite.ml.optimization.updatecalculators.SimpleGDParameterUpdate)2 SimpleGDUpdateCalculator (org.apache.ignite.ml.optimization.updatecalculators.SimpleGDUpdateCalculator)2 Path (java.nio.file.Path)1