use of org.nd4j.linalg.api.shape.loop.coordinatefunction.CoordinateFunction in project nd4j by deeplearning4j.
the class BooleanIndexing method applyWhere.
/**
* This method sets provided number to all elements which match specified condition
*
* @param to
* @param condition
* @param number
*/
public static void applyWhere(final INDArray to, final Condition condition, final Number number) {
if (condition instanceof BaseCondition) {
// for all static conditions we go native
Nd4j.getExecutioner().exec(new CompareAndSet(to, number.doubleValue(), condition));
} else {
final double value = number.doubleValue();
final Function<Number, Number> dynamic = new Function<Number, Number>() {
@Override
public Number apply(Number number) {
return value;
}
};
Shape.iterate(to, new CoordinateFunction() {
@Override
public void process(int[]... coord) {
if (condition.apply(to.getDouble(coord[0])))
to.putScalar(coord[0], dynamic.apply(to.getDouble(coord[0])).doubleValue());
}
});
}
}
use of org.nd4j.linalg.api.shape.loop.coordinatefunction.CoordinateFunction in project nd4j by deeplearning4j.
the class BooleanIndexing method and.
/**
* And over the whole ndarray given some condition
*
* @param n the ndarray to test
* @param cond the condition to test against
* @return true if all of the elements meet the specified
* condition false otherwise
*/
public static boolean and(final INDArray n, final Condition cond) {
if (cond instanceof BaseCondition) {
long val = (long) Nd4j.getExecutioner().exec(new MatchCondition(n, cond), Integer.MAX_VALUE).getDouble(0);
if (val == n.lengthLong())
return true;
else
return false;
} else {
boolean ret = true;
final AtomicBoolean a = new AtomicBoolean(ret);
Shape.iterate(n, new CoordinateFunction() {
@Override
public void process(int[]... coord) {
if (a.get())
a.compareAndSet(true, a.get() && cond.apply(n.getDouble(coord[0])));
}
});
return a.get();
}
}
use of org.nd4j.linalg.api.shape.loop.coordinatefunction.CoordinateFunction in project nd4j by deeplearning4j.
the class BooleanIndexing method or.
/**
* Or over the whole ndarray given some condition
*
* @param n
* @param cond
* @return
*/
public static boolean or(final INDArray n, final Condition cond) {
if (cond instanceof BaseCondition) {
long val = (long) Nd4j.getExecutioner().exec(new MatchCondition(n, cond), Integer.MAX_VALUE).getDouble(0);
if (val > 0)
return true;
else
return false;
} else {
boolean ret = false;
final AtomicBoolean a = new AtomicBoolean(ret);
Shape.iterate(n, new CoordinateFunction() {
@Override
public void process(int[]... coord) {
if (!a.get())
a.compareAndSet(false, a.get() || cond.apply(n.getDouble(coord[0])));
}
});
return a.get();
}
}
Aggregations