 # & operation for Nd4J

Is there a way to implement the `&` or `&=` for nd4j arrays?

for example I want to find relative extrema so I wrote the following function:

``````    private static INDArray boolrelextrema(
INDArray data, BiFunction<INDArray, INDArray, INDArray> comp, int order) {

if (order < 1) {
throw new IllegalArgumentException("Order must be >= 1");
}
if (data.isMatrix()) {
throw new IllegalArgumentException("Argrelextrema currently only supports 1-D arrays");
}

long length = data.length();
INDArray locs = Nd4j.createFromArray(IntStream.range(0, (int) length).toArray());
int[] shifts = IntStream.range(1, order + 1).toArray();

INDArray results = Nd4j.ones(DataType.BOOL, length);
for (int shift : shifts) {
// same as np.take(locs + shift, axis=0, mode='clip')
INDArray plus = data.get(NDArrayIndex.interval(shift, length));
plus = Nd4j.concat(0, plus, data.get(NDArrayIndex.point(length - 1)));

// same as np.take(locs - shift, axis=0, mode='clip')
INDArray minus = data.get(NDArrayIndex.interval(0, length - shift));
minus = Nd4j.concat(0, data.get(NDArrayIndex.point(0)), minus);

// find relative peaks
results = results
.eq(comp.apply(data, plus))
.eq(comp.apply(data, minus));
if (!results.any()) {
return results;
}
}
return results;
``````

but instead of `.eq()`, what I need in the `results` calculation is

``````results &= comp.apply(data, plus)
results &= comp.apply(data, minus)
``````

or perhaps

``````   results = results
.and(comp.apply(data, plus))
.and(comp.apply(data, minus))
``````

I can think of some workarounds, but I was hoping to be missing some native nd4j operation that would allow this. Thanks in advance!

I guess you are looking for something like this?

You can access it from `Nd4j.math.and`.

It is using the And Op, so in principle you can create your own “in-place and” by passing the target (`z`) when instantiating the op.

Thank you for the quick response. That’s what I was looking for.

“and” was just too common a keyword to find it via a google search, and I’m not yet fluent at where to look in the javadoc for certain categories of operations. Thank you again.

A pretty good place to start looking for this kinds of things is here:

Not all ops are mapped there, but most are, and they have a bit of documentation attached to them.

If you can’t find something there, you can press the `t` button to bring up Github’s file search and search for class names. In this case searching for “and.java” this way brings up a few things 1 Like

Hey, that is a very neat trick! Sure enough I see a bunch of stuff. Thanks!