Search in sources :

Example 26 with Vector

use of org.apache.ignite.ml.math.Vector in project ignite by apache.

the class AbstractMultipleLinearRegression method estimateResiduals.

/**
     * {@inheritDoc}
     */
@Override
public double[] estimateResiduals() {
    Vector b = calculateBeta();
    Vector e = yVector.minus(xMatrix.times(b));
    return e.getStorage().data();
}
Also used : Vector(org.apache.ignite.ml.math.Vector)

Example 27 with Vector

use of org.apache.ignite.ml.math.Vector in project ignite by apache.

the class VectorBenchmarkTest method basicMix.

/** */
private void basicMix(int size, Function<Integer, Vector> constructor) {
    final Vector v1 = constructor.apply(size), v2 = constructor.apply(size);
    for (int idx = 0; idx < size; idx++) {
        v1.set(idx, idx);
        v2.set(idx, size - idx);
    }
    assertNotNull(v1.sum());
    assertNotNull(v1.copy());
    assertFalse(v1.getLengthSquared() < 0);
    assertNotNull(v1.normalize());
    assertNotNull(v1.logNormalize());
    assertFalse(v1.getDistanceSquared(v2) < 0);
    assertNotNull(v1.divide(2));
    assertNotNull(v1.minus(v2));
    assertNotNull(v1.plus(v2));
    assertNotNull(v1.dot(v2));
    assertNotNull(v1.assign(v2));
    // IMPL NOTE this would better be last test for it sets all values the same
    assertNotNull(v1.assign(1));
}
Also used : Vector(org.apache.ignite.ml.math.Vector) DenseLocalOffHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOffHeapVector) DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector)

Example 28 with Vector

use of org.apache.ignite.ml.math.Vector in project ignite by apache.

the class VectorBenchmarkTest method foldMapMix.

/** */
private void foldMapMix(int size, Function<Integer, Vector> constructor) {
    final Vector v1 = constructor.apply(size), v2 = constructor.apply(size);
    for (int idx = 0; idx < size; idx++) {
        v1.set(idx, idx);
        v2.set(idx, size - idx);
    }
    assertNotNull(v1.map((val) -> (val + 1)));
    assertNotNull(v1.map(v2, (one, other) -> one + other / 2.0));
    assertNotNull(v1.map((val, val1) -> (val + val1), 2.0));
    assertNotNull(v1.foldMap((sum, val) -> (val + sum), (val) -> val, 0.0));
    assertNotNull(v1.foldMap(v2, (sum, val) -> (val + sum), (val1, val2) -> val1 + val2, 0.0));
}
Also used : Vector(org.apache.ignite.ml.math.Vector) Ignore(org.junit.Ignore) Assert.assertFalse(org.junit.Assert.assertFalse) Assert.assertNotNull(org.junit.Assert.assertNotNull) BiConsumer(java.util.function.BiConsumer) DenseLocalOffHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOffHeapVector) Test(org.junit.Test) Function(java.util.function.Function) DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) Vector(org.apache.ignite.ml.math.Vector) DenseLocalOffHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOffHeapVector) DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector)

Example 29 with Vector

use of org.apache.ignite.ml.math.Vector in project ignite by apache.

the class CholeskyDecompositionTest method basicTest.

/** */
private void basicTest(Matrix m) {
    // This decomposition is useful when dealing with systems of linear equations of the form
    // m x = b where m is a Hermitian matrix.
    // For such systems Cholesky decomposition provides
    // more effective method of solving compared to LU decomposition.
    // Suppose we want to solve system
    // m x = b for various bs. Then after we computed Cholesky decomposition, we can feed various bs
    // as a matrix of the form
    // (b1, b2, ..., bm)
    // to the method Cholesky::solve which returns solutions in the form
    // (sol1, sol2, ..., solm)
    CholeskyDecomposition dec = new CholeskyDecomposition(m);
    assertEquals("Unexpected value for decomposition determinant.", 4d, dec.getDeterminant(), 0d);
    Matrix l = dec.getL();
    Matrix lt = dec.getLT();
    assertNotNull("Matrix l is expected to be not null.", l);
    assertNotNull("Matrix lt is expected to be not null.", lt);
    for (int row = 0; row < l.rowSize(); row++) for (int col = 0; col < l.columnSize(); col++) assertEquals("Unexpected value transposed matrix at (" + row + "," + col + ").", l.get(row, col), lt.get(col, row), 0d);
    Matrix bs = new DenseLocalOnHeapMatrix(new double[][] { { 4.0, -6.0, 7.0 }, { 1.0, 1.0, 1.0 } }).transpose();
    Matrix sol = dec.solve(bs);
    assertNotNull("Solution matrix is expected to be not null.", sol);
    assertEquals("Solution rows are not as expected.", bs.rowSize(), sol.rowSize());
    assertEquals("Solution columns are not as expected.", bs.columnSize(), sol.columnSize());
    for (int i = 0; i < sol.columnSize(); i++) assertNotNull("Solution matrix column is expected to be not null at index " + i, sol.viewColumn(i));
    Vector b = new DenseLocalOnHeapVector(new double[] { 4.0, -6.0, 7.0 });
    Vector solVec = dec.solve(b);
    for (int idx = 0; idx < b.size(); idx++) assertEquals("Unexpected value solution vector at " + idx, b.get(idx), solVec.get(idx), 0d);
    dec.destroy();
}
Also used : Matrix(org.apache.ignite.ml.math.Matrix) DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix) DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix) Vector(org.apache.ignite.ml.math.Vector) DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector)

Example 30 with Vector

use of org.apache.ignite.ml.math.Vector in project ignite by apache.

the class AbstractVector method plus.

/** {@inheritDoc} */
@Override
public Vector plus(Vector vec) {
    checkCardinality(vec);
    Vector cp = copy();
    return cp.map(vec, Functions.PLUS);
}
Also used : Vector(org.apache.ignite.ml.math.Vector)

Aggregations

Vector (org.apache.ignite.ml.math.Vector)44 Test (org.junit.Test)17 DenseLocalOnHeapVector (org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector)13 DenseLocalOffHeapVector (org.apache.ignite.ml.math.impls.vector.DenseLocalOffHeapVector)7 IdentityValueMapper (org.apache.ignite.ml.math.IdentityValueMapper)5 Matrix (org.apache.ignite.ml.math.Matrix)5 UnsupportedOperationException (org.apache.ignite.ml.math.exceptions.UnsupportedOperationException)5 SparseLocalVector (org.apache.ignite.ml.math.impls.vector.SparseLocalVector)5 ExternalizeTest (org.apache.ignite.ml.math.ExternalizeTest)4 RandomVector (org.apache.ignite.ml.math.impls.vector.RandomVector)4 DenseLocalOnHeapMatrix (org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix)3 BiConsumer (java.util.function.BiConsumer)1 Function (java.util.function.Function)1 CardinalityException (org.apache.ignite.ml.math.exceptions.CardinalityException)1 PivotedMatrixView (org.apache.ignite.ml.math.impls.matrix.PivotedMatrixView)1 SparseDistributedMatrix (org.apache.ignite.ml.math.impls.matrix.SparseDistributedMatrix)1 AbstractVector (org.apache.ignite.ml.math.impls.vector.AbstractVector)1 MatrixUtil.likeVector (org.apache.ignite.ml.math.util.MatrixUtil.likeVector)1 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)1 Assert.assertFalse (org.junit.Assert.assertFalse)1