Skip to content

Commit

Permalink
Add max and min to expressions (#1249)
Browse files Browse the repository at this point in the history
Signed-off-by: Christopher White <[email protected]>
  • Loading branch information
cswhite2000 authored Oct 9, 2023
1 parent b550217 commit 3641ab0
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 3641ab0

Please sign in to comment.