When a user-defined function encounters an error during
evaluation, the value method should throw a
user-defined unchecked exception.
The following code excerpt shows the recommended way to do that using a root solver as an example, but the same construct is applicable to ODE integrators or optimizers.
private static class LocalException extends RuntimeException {
// The x value that caused the problem.
private final SomeFieldType x;
public LocalException(SomeFieldType x) {
this.x = x;
}
public double getX() {
return x;
}
}
private static class MyFunction implements FieldUnivariateFunction<SomeFieldType> {
public SomeFieldType value(SomeFieldType x) {
SomeFieldType y = hugeFormula(x);
if (somethingBadHappens) {
throw new LocalException(x);
}
return y;
}
}
public void compute() {
try {
solver.solve(maxEval, new MyFunction(a, b, c), min, max);
} catch (LocalException le) {
// Retrieve the x value.
}
}
As shown, the exception is local to the user's code and it is guaranteed
that Apache Commons Math will not catch it.
UnivariateFunction
| Modifier and Type | Method and Description |
|---|---|
| public T | Returns: the value of the function.Point at which the function value should be computed. x)Compute the value of the function. |
| value | back to summary |
|---|---|
| public T value(T x) Compute the value of the function.
| |