Search in sources :

Example 21 with Vector

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

the class VectorCustomStorageExample method main.

/**
     * Executes example.
     *
     * @param args Command line arguments, none required.
     */
public static void main(String[] args) {
    System.out.println();
    System.out.println(">>> Vector custom storage API usage example started.");
    System.out.println("\n>>> Creating perpendicular vectors.");
    double[] data1 = new double[] { 1, 0, 3, 0, 5, 0 };
    double[] data2 = new double[] { 0, 2, 0, 4, 0, 6 };
    Vector v1 = new VectorCustomStorage(data1);
    Vector v2 = new VectorCustomStorage(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>>> Vector custom storage API usage example completed.");
}
Also used : AbstractVector(org.apache.ignite.ml.math.impls.vector.AbstractVector) Vector(org.apache.ignite.ml.math.Vector)

Example 22 with Vector

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

the class MatrixImplementationsTest method testFoldCol.

/** */
@Test
public void testFoldCol() {
    consumeSampleMatrix((m, desc) -> {
        if (ignore(m.getClass()))
            return;
        fillMatrix(m);
        Vector foldCols = m.foldColumns(Vector::sum);
        for (int j = 0; j < m.columnSize(); j++) {
            Double locSum = 0d;
            for (int i = 0; i < m.rowSize(); i++) locSum += m.get(i, j);
            assertEquals("Unexpected value for " + desc + " at " + j, foldCols.get(j), locSum, 0d);
        }
    });
}
Also used : RandomVector(org.apache.ignite.ml.math.impls.vector.RandomVector) DenseLocalOffHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOffHeapVector) Vector(org.apache.ignite.ml.math.Vector) SparseLocalVector(org.apache.ignite.ml.math.impls.vector.SparseLocalVector) DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) Test(org.junit.Test) ExternalizeTest(org.apache.ignite.ml.math.ExternalizeTest)

Example 23 with Vector

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

the class MatrixImplementationsTest method testFoldRow.

/** */
@Test
public void testFoldRow() {
    consumeSampleMatrix((m, desc) -> {
        if (ignore(m.getClass()))
            return;
        fillMatrix(m);
        Vector foldRows = m.foldRows(Vector::sum);
        for (int i = 0; i < m.rowSize(); i++) {
            Double locSum = 0d;
            for (int j = 0; j < m.columnSize(); j++) locSum += m.get(i, j);
            assertEquals("Unexpected value for " + desc + " at " + i, foldRows.get(i), locSum, 0d);
        }
    });
}
Also used : RandomVector(org.apache.ignite.ml.math.impls.vector.RandomVector) DenseLocalOffHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOffHeapVector) Vector(org.apache.ignite.ml.math.Vector) SparseLocalVector(org.apache.ignite.ml.math.impls.vector.SparseLocalVector) DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) Test(org.junit.Test) ExternalizeTest(org.apache.ignite.ml.math.ExternalizeTest)

Example 24 with Vector

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

the class LUDecompositionTest method solveVec.

/** */
@Test
public void solveVec() throws Exception {
    Vector sol = new LUDecomposition(new PivotedMatrixView(testMatrix)).solve(new DenseLocalOnHeapVector(testMatrix.rowSize()));
    assertEquals("Wrong solution vector size.", testMatrix.rowSize(), sol.size());
    for (int i = 0; i < sol.size(); i++) assertEquals("Unexpected value at index " + i, 0d, sol.getX(i), 0.0000001d);
}
Also used : PivotedMatrixView(org.apache.ignite.ml.math.impls.matrix.PivotedMatrixView) DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) Vector(org.apache.ignite.ml.math.Vector) DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) Test(org.junit.Test)

Example 25 with Vector

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

the class EigenDecompositionTest method assertDiagonalConsistsOfEigenvalues.

/** */
private void assertDiagonalConsistsOfEigenvalues(DenseLocalOnHeapMatrix m, Matrix d, Matrix v) {
    int n = m.columnSize();
    for (int i = 0; i < n; i++) {
        Vector eigenVector = v.viewColumn(i);
        double eigenVal = d.getX(i, i);
        assertVectorsAreEqual(m.times(eigenVector), eigenVector.times(eigenVal));
    }
}
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