Search in sources :

Example 16 with MatchCondition

use of org.nd4j.linalg.api.ops.impl.accum.MatchCondition 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();
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BaseCondition(org.nd4j.linalg.indexing.conditions.BaseCondition) CoordinateFunction(org.nd4j.linalg.api.shape.loop.coordinatefunction.CoordinateFunction) MatchCondition(org.nd4j.linalg.api.ops.impl.accum.MatchCondition)

Example 17 with MatchCondition

use of org.nd4j.linalg.api.ops.impl.accum.MatchCondition in project nd4j by deeplearning4j.

the class BooleanIndexing method or.

/**
 * Or over the whole ndarray given some condition, with respect to dimensions
 *
 * @param n    the ndarray to test
 * @param condition the condition to test against
 * @return true if all of the elements meet the specified
 * condition false otherwise
 */
public static boolean[] or(final INDArray n, final Condition condition, int... dimension) {
    if (!(condition instanceof BaseCondition))
        throw new UnsupportedOperationException("Only static Conditions are supported");
    MatchCondition op = new MatchCondition(n, condition);
    INDArray arr = Nd4j.getExecutioner().exec(op, dimension);
    boolean[] result = new boolean[arr.length()];
    for (int i = 0; i < arr.length(); i++) {
        if (arr.getDouble(i) > 0)
            result[i] = true;
        else
            result[i] = false;
    }
    return result;
}
Also used : BaseCondition(org.nd4j.linalg.indexing.conditions.BaseCondition) INDArray(org.nd4j.linalg.api.ndarray.INDArray) MatchCondition(org.nd4j.linalg.api.ops.impl.accum.MatchCondition)

Aggregations

MatchCondition (org.nd4j.linalg.api.ops.impl.accum.MatchCondition)17 INDArray (org.nd4j.linalg.api.ndarray.INDArray)12 Test (org.junit.Test)8 BaseNd4jTest (org.nd4j.linalg.BaseNd4jTest)6 BaseCondition (org.nd4j.linalg.indexing.conditions.BaseCondition)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 CoordinateFunction (org.nd4j.linalg.api.shape.loop.coordinatefunction.CoordinateFunction)2 CompressionDescriptor (org.nd4j.linalg.compression.CompressionDescriptor)2 ND4JIllegalStateException (org.nd4j.linalg.exception.ND4JIllegalStateException)2 IntPointer (org.bytedeco.javacpp.IntPointer)1 IActivation (org.nd4j.linalg.activations.IActivation)1 ActivationSigmoid (org.nd4j.linalg.activations.impl.ActivationSigmoid)1 DataBuffer (org.nd4j.linalg.api.buffer.DataBuffer)1 BernoulliDistribution (org.nd4j.linalg.api.ops.random.impl.BernoulliDistribution)1 DefaultRandom (org.nd4j.linalg.api.rng.DefaultRandom)1 Random (org.nd4j.linalg.api.rng.Random)1 Distribution (org.nd4j.linalg.api.rng.distribution.Distribution)1 CompressedDataBuffer (org.nd4j.linalg.compression.CompressedDataBuffer)1 NativeRandom (org.nd4j.rng.NativeRandom)1