Search in sources :

Example 1 with ValueMapper

use of org.apache.ignite.ml.math.ValueMapper 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 ValueMapper

use of org.apache.ignite.ml.math.ValueMapper 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.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.MatrixKeyMapper) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Aggregations

Ignite (org.apache.ignite.Ignite)2 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)2 IdentityValueMapper (org.apache.ignite.ml.math.IdentityValueMapper)2 ValueMapper (org.apache.ignite.ml.math.ValueMapper)2 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