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.math4.legacy.analysis;
018
019import java.util.function.DoubleUnaryOperator;
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(double) 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 double x;
036 *
037 *     public LocalException(double x) {
038 *         this.x = x;
039 *     }
040 *
041 *     public double getX() {
042 *         return x;
043 *     }
044 * }
045 *
046 * private static class MyFunction implements UnivariateFunction {
047 *     public double value(double x) {
048 *         double 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@FunctionalInterface
069public interface UnivariateFunction extends DoubleUnaryOperator {
070    /**
071     * Compute the value of the function.
072     *
073     * @param x Point at which the function value should be computed.
074     * @return the value of the function.
075     * @throws IllegalArgumentException when the activated method itself can
076     * ascertain that a precondition, specified in the API expressed at the
077     * level of the activated method, has been violated.
078     * When Commons Math throws an {@code IllegalArgumentException}, it is
079     * usually the consequence of checking the actual parameters passed to
080     * the method.
081     */
082    double value(double x);
083
084    /** {@inheritDoc} */
085    @Override
086    default double applyAsDouble(double x) {
087        return value(x);
088    }
089}