Skip to content

Commit

Permalink
feat: move to java, use InferredExpression, and test java for RS_Modulo
Browse files Browse the repository at this point in the history
  • Loading branch information
furqaankhan committed Sep 19, 2023
1 parent 6743f85 commit 3af7aa8
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,20 @@ public static double[] multiplyFactor(double[] band, double factor) {
return result;
}

/**
* @param band band values
* @param dividend dividend for modulo
* @return an array with modular remainders calculated from the given dividend
*/
public static double[] modulo(double[] band, double dividend) {
double[] result = new double[band.length];
for (int i = 0; i < band.length; i++) {
result[i] = band[i] % dividend;
}

return result;
}

/**
* Throws an IllegalArgumentException if the lengths of the bands are not the same.
* @param band1 length of band values
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,20 @@ public void testDivide() {
assertArrayEquals(expected, actual, 0.01d);
}

@Test
public void testModulo() {
double[] band = new double[] {100.0, 260.0, 189.0, 106.0, 230.0, 169.0, 196.0};
double dividend = 90;
double[] actual = MapAlgebra.modulo(band, dividend);
double[] expected = new double[] {10.0, 80.0, 9.0, 16.0, 50.0, 79.0, 16.0};
assertArrayEquals(expected, actual, 0.1d);

band = new double[] {230.0, 345.0, 136.0, 106.0, 134.0, 105.0};
actual = MapAlgebra.modulo(band, dividend);
expected = new double[] {50.0, 75.0, 46.0, 16.0, 44.0, 15.0};
assertArrayEquals(expected, actual, 0.1d);
}

@Test
public void testMapAlgebra() throws FactoryException {
Random random = new Random();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -384,33 +384,7 @@ case class RS_Divide(inputExpressions: Seq[Expression]) extends InferredExpressi
}

// Modulo of a band
case class RS_Modulo(inputExpressions: Seq[Expression])
extends Expression with CodegenFallback with UserDataGeneratator {
assert(inputExpressions.length == 2)

override def nullable: Boolean = false

override def eval(inputRow: InternalRow): Any = {
val band = inputExpressions(0).eval(inputRow).asInstanceOf[ArrayData].toDoubleArray()
val dividend = inputExpressions(1).eval(inputRow).asInstanceOf[Decimal].toDouble

new GenericArrayData(modulo(band, dividend))
}

private def modulo(band: Array[Double], dividend:Double):Array[Double] = {

val result = new Array[Double](band.length)
for(i<-0 until band.length) {
result(i) = band(i) % dividend
}
result

}

override def dataType: DataType = ArrayType(DoubleType)

override def children: Seq[Expression] = inputExpressions

case class RS_Modulo(inputExpressions: Seq[Expression]) extends InferredExpression(MapAlgebra.modulo _) {
protected def withNewChildrenInternal(newChildren: IndexedSeq[Expression]) = {
copy(inputExpressions = newChildren)
}
Expand Down

0 comments on commit 3af7aa8

Please sign in to comment.