Shorten less than, greater than and equals operations.
@@ -164,46 +164,30 @@ public class INDArrayShim {
}
- private static INDArray performOperationWithScalarTensorPreservingShape(INDArray left, INDArray right, BiFunction<INDArray, INDArray, INDArray> operation) {
- if (left.length() == 1 || right.length() == 1) {
- long[] resultShape = Shape.broadcastOutputShape(left.shape(), right.shape());
- INDArray result;
- if (left.length() == 1) {
- result = operation.apply(Nd4j.valueArrayOf(right.shape(), left.getDouble(0)), right);
- } else {
- result = operation.apply(left, Nd4j.valueArrayOf(left.shape(), right.getDouble(0)));
- }
- return result.reshape(resultShape);
- }
- return operation.apply(left, right);
- }
-
public static INDArray lt(INDArray left, INDArray right) {
- if (left.length() == 1 || right.length() == 1) {
- long[] resultShape = Shape.broadcastOutputShape(left.shape(), right.shape());
- INDArray result;
- if (left.length() == 1) {
- result = Nd4j.valueArrayOf(right.shape(), left.getDouble(0)).lt(right);
- } else {
- result = left.lt(right.getDouble(0));
+ return performOperationWithScalarTensorPreservingShape(left, right, INDArray::lt);
}
- return result.reshape(resultShape);
+
+ public static INDArray gt(INDArray left, INDArray right) {
+ return performOperationWithScalarTensorPreservingShape(left, right, INDArray::gt);
}
- return left.lt(right);
+
+ public static INDArray eq(INDArray left, INDArray right) {
+ return performOperationWithScalarTensorPreservingShape(left, right, INDArray::eq);
}
- public static INDArray gt(INDArray left, INDArray right) {
+ private static INDArray performOperationWithScalarTensorPreservingShape(INDArray left, INDArray right, BiFunction<INDArray, INDArray, INDArray> operation) {
if (left.length() == 1 || right.length() == 1) {
long[] resultShape = Shape.broadcastOutputShape(left.shape(), right.shape());
INDArray result;
if (left.length() == 1) {
- result = Nd4j.valueArrayOf(right.shape(), left.getDouble(0)).gt(right);
+ result = operation.apply(Nd4j.valueArrayOf(right.shape(), left.getDouble(0)), right);
} else {
- result = left.gt(right.getDouble(0));
+ result = operation.apply(left, Nd4j.valueArrayOf(left.shape(), right.getDouble(0)));
}
return result.reshape(resultShape);
}
- return left.gt(right);
+ return operation.apply(left, right);
}
public static INDArray lessThanOrEqual(INDArray left, INDArray right) {
@@ -238,20 +222,6 @@ public class INDArrayShim {
return left;
}
- public static INDArray eq(INDArray left, INDArray right) {
- if (left.length() == 1 || right.length() == 1) {
- long[] resultShape = Shape.broadcastOutputShape(left.shape(), right.shape());
- INDArray result;
- if (left.length() == 1) {
- result = Nd4j.valueArrayOf(right.shape(), left.getDouble(0)).eq(right);
- } else {
- result = left.eq(right.getDouble(0));
- }
- return result.reshape(resultShape);
- }
- return left.eq(right);
- }
-
public static INDArray getGreaterThanMask(INDArray mask, INDArray right, DataBuffer.Type bufferType) {
if (mask.length() == 1 || right.length() == 1) {
long[] resultShape = Shape.broadcastOutputShape(mask.shape(), right.shape());
