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(">>>");
}
}
}
Aggregations