View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  /*
19   * This is not the original file distributed by the Apache Software Foundation
20   * It has been modified by the Hipparchus project
21   */
22  package org.hipparchus.analysis;
23  
24  /**
25   * An interface representing a univariate real function.
26   * <p>
27   * When a <em>user-defined</em> function encounters an error during
28   * evaluation, the {@link #value(double) value} method should throw a
29   * <em>user-defined</em> unchecked exception.</p>
30   * <p>
31   * The following code excerpt shows the recommended way to do that using
32   * a root solver as an example, but the same construct is applicable to
33   * ODE integrators or optimizers.</p>
34   *
35   * <pre>
36   * private static class LocalException extends RuntimeException {
37   *     // The x value that caused the problem.
38   *     private final double x;
39   *
40   *     public LocalException(double x) {
41   *         this.x = x;
42   *     }
43   *
44   *     public double getX() {
45   *         return x;
46   *     }
47   * }
48   *
49   * private static class MyFunction implements UnivariateFunction {
50   *     public double value(double x) {
51   *         double y = hugeFormula(x);
52   *         if (somethingBadHappens) {
53   *           throw new LocalException(x);
54   *         }
55   *         return y;
56   *     }
57   * }
58   *
59   * public void compute() {
60   *     try {
61   *         solver.solve(maxEval, new MyFunction(a, b, c), min, max);
62   *     } catch (LocalException le) {
63   *         // Retrieve the x value.
64   *     }
65   * }
66   * </pre>
67   *
68   * As shown, the exception is local to the user's code and it is guaranteed
69   * that Hipparchus will not catch it.
70   *
71   */
72  public interface UnivariateFunction {
73      /**
74       * Compute the value of the function.
75       *
76       * @param x Point at which the function value should be computed.
77       * @return the value of the function.
78       * @throws IllegalArgumentException when the activated method itself can
79       * ascertain that a precondition, specified in the API expressed at the
80       * level of the activated method, has been violated.
81       * When Hipparchus throws an {@code IllegalArgumentException}, it is
82       * usually the consequence of checking the actual parameters passed to
83       * the method.
84       */
85      double value(double x);
86  }