Search in sources :

Example 6 with Vector

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

the class VectorNormTest method getDistanceSquaredTest.

/** */
@Test
public void getDistanceSquaredTest() {
    consumeSampleVectors((v, desc) -> {
        // IMPL NOTE this initialises vector
        new VectorImplementationsTest.ElementsChecker(v, desc);
        final int size = v.size();
        final Vector vOnHeap = new DenseLocalOnHeapVector(size);
        final Vector vOffHeap = new DenseLocalOffHeapVector(size);
        invertValues(v, vOnHeap);
        invertValues(v, vOffHeap);
        for (int idx = 0; idx < size; idx++) {
            final double exp = v.get(idx);
            final int idxMirror = size - 1 - idx;
            assertTrue("On heap vector difference at " + desc + ", idx " + idx, exp - vOnHeap.get(idxMirror) == 0);
            assertTrue("Off heap vector difference at " + desc + ", idx " + idx, exp - vOffHeap.get(idxMirror) == 0);
        }
        // IMPL NOTE this won't mutate vOnHeap
        final double exp = vOnHeap.minus(v).getLengthSquared();
        final VectorImplementationsTest.Metric metric = new VectorImplementationsTest.Metric(exp, v.getDistanceSquared(vOnHeap));
        assertTrue("On heap vector not close enough at " + desc + ", " + metric, metric.closeEnough());
        final VectorImplementationsTest.Metric metric1 = new VectorImplementationsTest.Metric(exp, v.getDistanceSquared(vOffHeap));
        assertTrue("Off heap vector not close enough at " + desc + ", " + metric1, metric1.closeEnough());
    });
}
Also used : Vector(org.apache.ignite.ml.math.Vector) Test(org.junit.Test)

Example 7 with Vector

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

the class DelegatingVectorConstructorTest method basicTest.

/** */
@Test
public void basicTest() {
    final Vector parent = new DenseLocalOnHeapVector(new double[] { 0, 1 });
    final Vector delegate = new DelegatingVector(parent);
    final int size = parent.size();
    assertEquals("Delegate size differs from expected.", size, delegate.size());
    for (int idx = 0; idx < size; idx++) assertDelegate(parent, delegate, idx);
}
Also used : Vector(org.apache.ignite.ml.math.Vector) Test(org.junit.Test)

Example 8 with Vector

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

the class RandomVectorConstructorTest method basicTest.

/** */
@Test
public void basicTest() {
    final int basicSize = 3;
    Vector v1 = new RandomVector(basicSize);
    //noinspection EqualsWithItself
    assertTrue("Expect vector to be equal to self", v1.equals(v1));
    //noinspection ObjectEqualsNull
    assertFalse("Expect vector to be not equal to null", v1.equals(null));
    assertEquals("Size differs from expected", basicSize, v1.size());
    verifyValues(v1);
    Vector v2 = new RandomVector(basicSize, true);
    assertEquals("Size differs from expected", basicSize, v2.size());
    verifyValues(v2);
    Vector v3 = new RandomVector(basicSize, false);
    assertEquals("Size differs from expected", basicSize, v3.size());
    verifyValues(v3);
}
Also used : Vector(org.apache.ignite.ml.math.Vector) Test(org.junit.Test)

Example 9 with Vector

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

the class SingleElementVectorConstructorTest method basicTest.

/** */
private void basicTest(int size, int idx) {
    final Double expVal = (double) (size - idx);
    Vector v = new SingleElementVector(size, idx, expVal);
    assertTrue("Expect value " + expVal + " at index " + idx + " for size " + size, expVal.equals(v.get(idx)));
    final double delta = 1.0;
    v.set(idx, expVal - delta);
    assertTrue("Expect value " + expVal + " at index " + idx + " for size " + size, expVal.equals(v.get(idx) + delta));
    final Double zero = 0.0;
    for (int i = 0; i < size; i++) {
        if (i == idx)
            continue;
        assertTrue("Expect zero at index " + i + " for size " + size, zero.equals(v.get(i)));
        boolean eCaught = false;
        try {
            v.set(i, 1.0);
        } catch (UnsupportedOperationException uoe) {
            eCaught = true;
        }
        assertTrue("Expect " + java.lang.UnsupportedOperationException.class.getSimpleName() + " at index " + i + " for size " + size, eCaught);
    }
}
Also used : Vector(org.apache.ignite.ml.math.Vector) UnsupportedOperationException(org.apache.ignite.ml.math.exceptions.UnsupportedOperationException)

Example 10 with Vector

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

the class EigenDecomposition method orthes.

/** */
private Matrix orthes(Matrix matrix) {
    // Working storage for nonsymmetric algorithm.
    Vector ort = likeVector(matrix);
    Matrix hessenBerg = like(matrix).assign(matrix);
    //  This is derived from the Algol procedures orthes and ortran,
    //  by Martin and Wilkinson, Handbook for Auto. Comp.,
    //  Vol.ii-Linear Algebra, and the corresponding
    //  Fortran subroutines in EISPACK.
    int low = 0;
    int high = n - 1;
    for (int m = low + 1; m <= high - 1; m++) {
        // Scale column.
        Vector hCol = hessenBerg.viewColumn(m - 1).viewPart(m, high - m + 1);
        double scale = hCol.kNorm(1);
        if (scale != 0.0) {
            // Compute Householder transformation.
            ort.viewPart(m, high - m + 1).map(hCol, Functions.plusMult(1 / scale));
            double h = ort.viewPart(m, high - m + 1).getLengthSquared();
            double g = Math.sqrt(h);
            if (ort.getX(m) > 0)
                g = -g;
            h -= ort.getX(m) * g;
            ort.setX(m, ort.getX(m) - g);
            // Apply Householder similarity transformation
            // H = (I-u*u'/h)*H*(I-u*u')/h)
            Vector ortPiece = ort.viewPart(m, high - m + 1);
            for (int j = m; j < n; j++) {
                double f = ortPiece.dot(hessenBerg.viewColumn(j).viewPart(m, high - m + 1)) / h;
                hessenBerg.viewColumn(j).viewPart(m, high - m + 1).map(ortPiece, Functions.plusMult(-f));
            }
            for (int i = 0; i <= high; i++) {
                double f = ortPiece.dot(hessenBerg.viewRow(i).viewPart(m, high - m + 1)) / h;
                hessenBerg.viewRow(i).viewPart(m, high - m + 1).map(ortPiece, Functions.plusMult(-f));
            }
            ort.setX(m, scale * ort.getX(m));
            hessenBerg.setX(m, m - 1, scale * g);
        }
    }
    // Accumulate transformations (Algol's ortran).
    v.assign(0);
    v.viewDiagonal().assign(1);
    for (int m = high - 1; m >= low + 1; m--) {
        if (hessenBerg.getX(m, m - 1) != 0.0) {
            ort.viewPart(m + 1, high - m).assign(hessenBerg.viewColumn(m - 1).viewPart(m + 1, high - m));
            for (int j = m; j <= high; j++) {
                double g = ort.viewPart(m, high - m + 1).dot(v.viewColumn(j).viewPart(m, high - m + 1));
                // Double division avoids possible underflow
                g = g / ort.getX(m) / hessenBerg.getX(m, m - 1);
                v.viewColumn(j).viewPart(m, high - m + 1).map(ort.viewPart(m, high - m + 1), Functions.plusMult(g));
            }
        }
    }
    return hessenBerg;
}
Also used : Matrix(org.apache.ignite.ml.math.Matrix) Vector(org.apache.ignite.ml.math.Vector) MatrixUtil.likeVector(org.apache.ignite.ml.math.util.MatrixUtil.likeVector)

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