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