Skip to content

Commit

Permalink
Add max and min to expressions
Browse files Browse the repository at this point in the history
Signed-off-by: Christopher White <[email protected]>
  • Loading branch information
cswhite2000 committed Sep 27, 2023
1 parent f9da16d commit 7ed8fc7
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion util/src/main/java/tc/oc/pgm/util/math/Formula.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,22 @@ public double apply(double... doubles) {
}
};

Function MAX =
new Function("max", 2) {
@Override
public double apply(double... doubles) {
return Math.max(doubles[0], doubles[1]);
}
};

Function MIN =
new Function("min", 2) {
@Override
public double apply(double... doubles) {
return Math.min(doubles[0], doubles[1]);
}
};

static <T extends Supplier<Map<String, Double>>> Formula<T> of(
String expression, Set<String> variables, Formula<T> fallback)
throws IllegalArgumentException {
Expand All @@ -41,7 +57,10 @@ static <T> Formula<T> of(String expression, Set<String> variables, ContextBuilde
throws IllegalArgumentException {

Expression exp =
new ExpressionBuilder(expression).variables(variables).functions(BOUND, RANDOM).build();
new ExpressionBuilder(expression)
.variables(variables)
.functions(BOUND, RANDOM, MAX, MIN)
.build();

return new ExpFormula<>(exp, context);
}
Expand Down

0 comments on commit 7ed8fc7

Please sign in to comment.