Search in sources :

Example 1 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.ValueMapper) IdentityValueMapper(org.apache.ignite.ml.math.IdentityValueMapper) CacheVector(org.apache.ignite.ml.math.impls.vector.CacheVector) VectorKeyMapper(org.apache.ignite.ml.math.VectorKeyMapper) Ignite(org.apache.ignite.Ignite) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Example 2 with IdentityValueMapper

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

the class CacheVectorTest method testSum.

/** */
public void testSum() {
    IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName());
    IdentityValueMapper valMapper = new IdentityValueMapper();
    CacheVector<Integer, Double> cacheVector = new CacheVector<>(size, getCache(), keyMapper, valMapper);
    initVector(cacheVector);
    assertEquals("Unexpected value.", cacheVector.sum(), 0d, 0d);
    cacheVector.assign(1d);
    assertEquals("Unexpected value.", cacheVector.sum(), size, 0d);
}
Also used : IdentityValueMapper(org.apache.ignite.ml.math.IdentityValueMapper)

Example 3 with IdentityValueMapper

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

the class CacheVectorTest method testDivide.

/** */
public void testDivide() {
    IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName());
    final int size = MathTestConstants.STORAGE_SIZE;
    IdentityValueMapper valMapper = new IdentityValueMapper();
    CacheVector<Integer, Double> cacheVector = new CacheVector<>(size, getCache(), keyMapper, valMapper);
    initVector(cacheVector);
    cacheVector.assign(1d);
    cacheVector.divide(2d);
    for (int i = 0; i < size; i++) assertEquals("Unexpected value.", cacheVector.get(i), 1d / 2d, 0d);
}
Also used : IdentityValueMapper(org.apache.ignite.ml.math.IdentityValueMapper)

Example 4 with IdentityValueMapper

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

the class CacheVectorTest method testLikeMatrix.

/** */
public void testLikeMatrix() {
    IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName());
    IdentityValueMapper valMapper = new IdentityValueMapper();
    CacheVector<Integer, Double> cacheVector = new CacheVector<>(size, getCache(), keyMapper, valMapper);
    try {
        cacheVector.likeMatrix(size, size);
        TestCase.fail("Unsupported case");
    } catch (UnsupportedOperationException ignored) {
    }
}
Also used : IdentityValueMapper(org.apache.ignite.ml.math.IdentityValueMapper) UnsupportedOperationException(org.apache.ignite.ml.math.exceptions.UnsupportedOperationException)

Example 5 with IdentityValueMapper

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

the class CacheVectorTest method testAssignFunc.

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

Aggregations

IdentityValueMapper (org.apache.ignite.ml.math.IdentityValueMapper)36 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.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.MatrixKeyMapper)1 VectorKeyMapper (org.apache.ignite.ml.math.VectorKeyMapper)1 CacheMatrix (org.apache.ignite.ml.math.impls.matrix.CacheMatrix)1 CacheVector (org.apache.ignite.ml.math.impls.vector.CacheVector)1