001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.math3.analysis;
018
019import org.apache.commons.math3.RealFieldElement;
020
021/**
022 * An interface representing a univariate real function.
023 * <p>
024 * When a <em>user-defined</em> function encounters an error during
025 * evaluation, the {@link #value(RealFieldElement) value} method should throw a
026 * <em>user-defined</em> unchecked exception.</p>
027 * <p>
028 * The following code excerpt shows the recommended way to do that using
029 * a root solver as an example, but the same construct is applicable to
030 * ODE integrators or optimizers.</p>
031 *
032 * <pre>
033 * private static class LocalException extends RuntimeException {
034 *     // The x value that caused the problem.
035 *     private final SomeFieldType x;
036 *
037 *     public LocalException(SomeFieldType x) {
038 *         this.x = x;
039 *     }
040 *
041 *     public double getX() {
042 *         return x;
043 *     }
044 * }
045 *
046 * private static class MyFunction implements FieldUnivariateFunction&lt;SomeFieldType&gt; {
047 *     public SomeFieldType value(SomeFieldType x) {
048 *         SomeFieldType y = hugeFormula(x);
049 *         if (somethingBadHappens) {
050 *           throw new LocalException(x);
051 *         }
052 *         return y;
053 *     }
054 * }
055 *
056 * public void compute() {
057 *     try {
058 *         solver.solve(maxEval, new MyFunction(a, b, c), min, max);
059 *     } catch (LocalException le) {
060 *         // Retrieve the x value.
061 *     }
062 * }
063 * </pre>
064 *
065 * As shown, the exception is local to the user's code and it is guaranteed
066 * that Apache Commons Math will not catch it.
067 *
068 * @param <T> the type of the field elements
069 * @since 3.6
070 * @see UnivariateFunction
071 */
072public interface RealFieldUnivariateFunction<T extends RealFieldElement<T>> {
073    /**
074     * Compute the value of the function.
075     *
076     * @param x Point at which the function value should be computed.
077     * @return the value of the function.
078     * @throws IllegalArgumentException when the activated method itself can
079     * ascertain that a precondition, specified in the API expressed at the
080     * level of the activated method, has been violated.
081     * When Commons Math throws an {@code IllegalArgumentException}, it is
082     * usually the consequence of checking the actual parameters passed to
083     * the method.
084     */
085    T value(T x);
086}