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