Skip to content

Commit 0e8a740

Browse files
gselzerctrueden
authored andcommitted
Rework sqrt into an OpCollection
1 parent c2da31b commit 0e8a740

1 file changed

Lines changed: 19 additions & 31 deletions

File tree

Lines changed: 19 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,43 @@
11
package org.scijava.ops.math;
22

33
import java.util.function.Function;
4-
import java.util.stream.IntStream;
54

6-
import org.scijava.ops.core.Op;
5+
import org.scijava.ops.OpField;
6+
import org.scijava.ops.core.OpCollection;
77
import org.scijava.ops.core.computer.Computer;
88
import org.scijava.ops.core.inplace.Inplace;
99
import org.scijava.param.Parameter;
1010
import org.scijava.plugin.Plugin;
1111
import org.scijava.struct.ItemIO;
1212

13+
@Plugin(type = OpCollection.class)
1314
public class Sqrt {
1415

1516
public static final String NAMES = MathOps.SQRT;
16-
17+
1718
// --------- Functions ---------
1819

19-
@Plugin(type = Op.class, name = NAMES)
20+
@OpField(names = NAMES)
2021
@Parameter(key = "number1")
2122
@Parameter(key = "result", type = ItemIO.OUTPUT)
22-
public static class MathSqrtDoubleFunction implements Function<Double, Double> {
23-
@Override
24-
public Double apply(Double t) {
25-
return Math.sqrt(t);
26-
}
27-
}
28-
23+
public static final Function<Double, Double> MathSqrtDoubleFunction = (t) -> Math.sqrt(t);
24+
2925
// --------- Computers ---------
3026

31-
@Plugin(type = Op.class, name = NAMES)
27+
@OpField(names = NAMES)
3228
@Parameter(key = "array1")
3329
@Parameter(key = "resultArray", type = ItemIO.BOTH)
34-
public static class MathPointwiseSqrtDoubleArrayComputer implements Computer<double[], double[]> {
35-
@Override
36-
public void compute(double[] in1, double[] out) {
37-
for (int i = 0; i < out.length; i++) {
38-
out[i] = Math.sqrt(in1[i]);
39-
}
40-
}
41-
}
42-
30+
public static final Computer<double[], double[]> MathPointwiseSqrtDoubleArrayComputer = (arr1, arr2) -> {
31+
for (int i = 0; i < arr1.length; i++)
32+
arr2[i] = Math.sqrt(arr1[i]);
33+
};
34+
4335
// --------- Inplaces ---------
44-
45-
@Plugin(type = Op.class, name = NAMES)
36+
37+
@OpField(names = NAMES)
4638
@Parameter(key = "arrayIO", type = ItemIO.BOTH)
47-
public static class MathPointwiseSqrtDoubleArrayInplace implements Inplace<double[]> {
48-
@Override
49-
public void mutate(double[] in1) {
50-
IntStream.range(0, in1.length).forEach(index -> {
51-
in1[index] = Math.sqrt(in1[index]);
52-
});
53-
}
54-
}
39+
public static final Inplace<double[]> MathPointwiseSqrtDoubleArrayInplace = (arr) -> {
40+
for(int i = 0; i < arr.length; i++) arr[i] = Math.sqrt(arr[i]);
41+
};
42+
5543
}

0 commit comments

Comments
 (0)