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 import org.hipparchus.CalculusFieldElement; 25 26 /** 27 * An interface representing a univariate real function. 28 * <p> 29 * When a <em>user-defined</em> function encounters an error during 30 * evaluation, the {@link #value(CalculusFieldElement) value} method should throw a 31 * <em>user-defined</em> unchecked exception.</p> 32 * <p> 33 * The following code excerpt shows the recommended way to do that using 34 * a root solver as an example, but the same construct is applicable to 35 * ODE integrators or optimizers. 36 * 37 * <pre> 38 * private static class LocalException extends RuntimeException { 39 * // The x value that caused the problem. 40 * private final SomeFieldType x; 41 * 42 * public LocalException(SomeFieldType x) { 43 * this.x = x; 44 * } 45 * 46 * public double getX() { 47 * return x; 48 * } 49 * } 50 * 51 * private static class MyFunction implements FieldUnivariateFunction<SomeFieldType> { 52 * public SomeFieldType value(SomeFieldType x) { 53 * SomeFieldType y = hugeFormula(x); 54 * if (somethingBadHappens) { 55 * throw new LocalException(x); 56 * } 57 * return y; 58 * } 59 * } 60 * 61 * public void compute() { 62 * try { 63 * solver.solve(maxEval, new MyFunction(a, b, c), min, max); 64 * } catch (LocalException le) { 65 * // Retrieve the x value. 66 * } 67 * } 68 * </pre> 69 *<p> 70 * As shown, the exception is local to the user's code and it is guaranteed 71 * that Hipparchus will not catch it.</p> 72 * 73 * @param <T> the type of the field elements 74 * @see UnivariateFunction 75 * @see FieldUnivariateFunction 76 */ 77 public interface CalculusFieldUnivariateFunction<T extends CalculusFieldElement<T>> { 78 /** 79 * Compute the value of the function. 80 * 81 * @param x Point at which the function value should be computed. 82 * @return the value of the function. 83 * @throws IllegalArgumentException when the activated method itself can 84 * ascertain that a precondition, specified in the API expressed at the 85 * level of the activated method, has been violated. 86 * When Hipparchus throws an {@code IllegalArgumentException}, it is 87 * usually the consequence of checking the actual parameters passed to 88 * the method. 89 */ 90 T value(T x); 91 }