Search in sources :

Example 1 with CacheMatrix

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

Ignite (org.apache.ignite.Ignite)1 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)1 IdentityValueMapper (org.apache.ignite.ml.math.IdentityValueMapper)1 MatrixKeyMapper (org.apache.ignite.ml.math.distributed.MatrixKeyMapper)1 ValueMapper (org.apache.ignite.ml.math.distributed.ValueMapper)1 CacheMatrix (org.apache.ignite.ml.math.impls.matrix.CacheMatrix)1