Search in sources :

Example 16 with IdentityValueMapper

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

the class CacheVectorTest method testMax.

/**
 */
public void testMax() {
    IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName());
    IdentityValueMapper valMapper = new IdentityValueMapper();
    CacheVector<Integer, Double> cacheVector = new CacheVector<>(size, getCache(), keyMapper, valMapper);
    Vector testVec = new DenseLocalOnHeapVector(IntStream.range(0, size).asDoubleStream().toArray());
    cacheVector.assign(testVec);
    assertEquals("Unexpected value.", cacheVector.maxValue(), testVec.get(size - 1), 0d);
}
Also used : IdentityValueMapper(org.apache.ignite.ml.math.IdentityValueMapper) Vector(org.apache.ignite.ml.math.Vector)

Example 17 with IdentityValueMapper

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

the class CacheVectorTest method testAssignRange.

/**
 */
public void testAssignRange() {
    IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName());
    IdentityValueMapper valMapper = new IdentityValueMapper();
    CacheVector<Integer, Double> cacheVector = new CacheVector<>(size, getCache(), keyMapper, valMapper);
    cacheVector.assign(IntStream.range(0, size).asDoubleStream().toArray());
    for (int i = 0; i < size; i++) assertEquals("Unexpected value.", cacheVector.get(i), i, 0d);
}
Also used : IdentityValueMapper(org.apache.ignite.ml.math.IdentityValueMapper)

Example 18 with IdentityValueMapper

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

the class CacheVectorExample method main.

/**
 * Executes example.
 *
 * @param args Command line arguments, none required.
 */
@SuppressWarnings("unchecked")
public static void main(String[] args) {
    try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
        System.out.println();
        System.out.println(">>> CacheVector example started.");
        CacheConfiguration<Integer, Double> cfg = new CacheConfiguration<>();
        cfg.setName(CACHE_NAME);
        try (IgniteCache<Integer, Double> cache = ignite.getOrCreateCache(cfg)) {
            double[] testValues1 = { 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 };
            double[] testValues2 = { 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 };
            ValueMapper valMapper = new IdentityValueMapper();
            // Map vector element index to cache keys.
            VectorKeyMapper<Integer> keyMapper1 = new VectorKeyMapper<Integer>() {

                @Override
                public Integer apply(int i) {
                    return i;
                }

                @Override
                public boolean isValid(Integer integer) {
                    return integer >= 0 && CARDINALITY > integer;
                }
            };
            // Map vector element index to cache keys with shift.
            VectorKeyMapper<Integer> keyMapper2 = new VectorKeyMapper<Integer>() {

                @Override
                public Integer apply(int i) {
                    return i + CARDINALITY;
                }

                @Override
                public boolean isValid(Integer integer) {
                    return integer >= 0 && CARDINALITY > integer;
                }
            };
            // Create two cache vectors over one cache.
            CacheVector cacheVector1 = new CacheVector(CARDINALITY, cache, keyMapper1, valMapper);
            System.out.println(">>> First cache vector created.");
            CacheVector cacheVector2 = new CacheVector(CARDINALITY, cache, keyMapper2, valMapper);
            System.out.println(">>> Second cache vector created.");
            cacheVector1.assign(testValues1);
            cacheVector2.assign(testValues2);
            // Dot product for orthogonal vectors is 0.0.
            assert cacheVector1.dot(cacheVector2) == 0.0;
            System.out.println(">>>");
            System.out.println(">>> Finished executing Ignite \"CacheVector\" example.");
            System.out.println(">>> Dot product is 0.0 for orthogonal vectors.");
            System.out.println(">>>");
        }
    }
}
Also used : IdentityValueMapper(org.apache.ignite.ml.math.IdentityValueMapper) ValueMapper(org.apache.ignite.ml.math.distributed.ValueMapper) IdentityValueMapper(org.apache.ignite.ml.math.IdentityValueMapper) CacheVector(org.apache.ignite.ml.math.impls.vector.CacheVector) VectorKeyMapper(org.apache.ignite.ml.math.distributed.VectorKeyMapper) Ignite(org.apache.ignite.Ignite) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Example 19 with IdentityValueMapper

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

the class CacheVectorTest method testSumNegative.

/** */
public void testSumNegative() {
    IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName());
    IdentityValueMapper valMapper = new IdentityValueMapper();
    CacheVector<Integer, Double> cacheVector = new CacheVector<>(size, getCache(), keyMapper, valMapper);
    try {
        double d = cacheVector.sum();
        fail();
    } catch (NullPointerException e) {
    // No-op.
    }
}
Also used : IdentityValueMapper(org.apache.ignite.ml.math.IdentityValueMapper)

Example 20 with IdentityValueMapper

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

the class CacheMatrixExample method main.

/**
 * Executes example.
 *
 * @param args Command line arguments, none required.
 */
public static void main(String[] args) {
    try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
        System.out.println();
        System.out.println(">>> CacheMatrix example started.");
        CacheConfiguration<Integer, Double> cfg = new CacheConfiguration<>();
        cfg.setName(CACHE_NAME);
        try (IgniteCache<Integer, Double> cache = ignite.getOrCreateCache(cfg)) {
            double[][] testValues = { { 1.0, 0.0, 0.0 }, { 1.0, 1.0, 0.0 }, { 1.0, 1.0, 1.0 } };
            ValueMapper valMapper = new IdentityValueMapper();
            // Map matrix element indices to cache keys.
            MatrixKeyMapper<Integer> keyMapper = new MatrixKeyMapper<Integer>() {

                @Override
                public Integer apply(int x, int y) {
                    return x * COLS + y;
                }

                @Override
                public boolean isValid(Integer integer) {
                    return integer >= 0 && integer < COLS * ROWS;
                }
            };
            // Create cache matrix.
            CacheMatrix<Integer, Double> cacheMatrix = new CacheMatrix<>(ROWS, COLS, cache, keyMapper, valMapper);
            cacheMatrix.assign(testValues);
            Tracer.showAscii(cacheMatrix);
            // Find all positive elements.
            Integer nonZeroes = cacheMatrix.foldMap((o, aDouble) -> {
                if (aDouble > 0)
                    return o + 1;
                return o;
            }, Functions.IDENTITY, 0);
            System.out.println("Quantity of non zeroes elements is " + nonZeroes.intValue());
            System.out.println(">>>");
            System.out.println(">>> Finished executing Ignite \"CacheMatrix\" example.");
            System.out.println(">>> Lower triangular matrix 3x3 have only 6 positive elements.");
            System.out.println(">>>");
        }
    }
}
Also used : IdentityValueMapper(org.apache.ignite.ml.math.IdentityValueMapper) ValueMapper(org.apache.ignite.ml.math.distributed.ValueMapper) IdentityValueMapper(org.apache.ignite.ml.math.IdentityValueMapper) CacheMatrix(org.apache.ignite.ml.math.impls.matrix.CacheMatrix) Ignite(org.apache.ignite.Ignite) MatrixKeyMapper(org.apache.ignite.ml.math.distributed.MatrixKeyMapper) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Aggregations

IdentityValueMapper (org.apache.ignite.ml.math.IdentityValueMapper)37 UnsupportedOperationException (org.apache.ignite.ml.math.exceptions.UnsupportedOperationException)8 Vector (org.apache.ignite.ml.math.Vector)5 Ignite (org.apache.ignite.Ignite)2 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)2 ValueMapper (org.apache.ignite.ml.math.distributed.ValueMapper)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 ObjectInputStream (java.io.ObjectInputStream)1 ObjectOutputStream (java.io.ObjectOutputStream)1 ExternalizeTest (org.apache.ignite.ml.math.ExternalizeTest)1 MatrixKeyMapper (org.apache.ignite.ml.math.distributed.MatrixKeyMapper)1 VectorKeyMapper (org.apache.ignite.ml.math.distributed.VectorKeyMapper)1 CacheMatrix (org.apache.ignite.ml.math.impls.matrix.CacheMatrix)1 CacheVector (org.apache.ignite.ml.math.impls.vector.CacheVector)1