summaryrefslogtreecommitdiff
path: root/src/main/java/org/apache/commons/math3/dfp/BracketingNthOrderBrentSolverDFP.java
blob: 23d4861dcef15de9b00454194dd0832d24a3b8a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.commons.math3.dfp;

import org.apache.commons.math3.analysis.RealFieldUnivariateFunction;
import org.apache.commons.math3.analysis.solvers.AllowedSolution;
import org.apache.commons.math3.analysis.solvers.FieldBracketingNthOrderBrentSolver;
import org.apache.commons.math3.exception.NoBracketingException;
import org.apache.commons.math3.exception.NullArgumentException;
import org.apache.commons.math3.exception.NumberIsTooSmallException;
import org.apache.commons.math3.util.MathUtils;

/**
 * This class implements a modification of the <a
 * href="http://mathworld.wolfram.com/BrentsMethod.html">Brent algorithm</a>.
 *
 * <p>The changes with respect to the original Brent algorithm are:
 *
 * <ul>
 *   <li>the returned value is chosen in the current interval according to user specified {@link
 *       AllowedSolution},
 *   <li>the maximal order for the invert polynomial root search is user-specified instead of being
 *       invert quadratic only
 * </ul>
 *
 * The given interval must bracket the root.
 *
 * @deprecated as of 3.6 replaced with {@link FieldBracketingNthOrderBrentSolver}
 */
@Deprecated
public class BracketingNthOrderBrentSolverDFP extends FieldBracketingNthOrderBrentSolver<Dfp> {

    /**
     * Construct a solver.
     *
     * @param relativeAccuracy Relative accuracy.
     * @param absoluteAccuracy Absolute accuracy.
     * @param functionValueAccuracy Function value accuracy.
     * @param maximalOrder maximal order.
     * @exception NumberIsTooSmallException if maximal order is lower than 2
     */
    public BracketingNthOrderBrentSolverDFP(
            final Dfp relativeAccuracy,
            final Dfp absoluteAccuracy,
            final Dfp functionValueAccuracy,
            final int maximalOrder)
            throws NumberIsTooSmallException {
        super(relativeAccuracy, absoluteAccuracy, functionValueAccuracy, maximalOrder);
    }

    /**
     * Get the absolute accuracy.
     *
     * @return absolute accuracy
     */
    @Override
    public Dfp getAbsoluteAccuracy() {
        return super.getAbsoluteAccuracy();
    }

    /**
     * Get the relative accuracy.
     *
     * @return relative accuracy
     */
    @Override
    public Dfp getRelativeAccuracy() {
        return super.getRelativeAccuracy();
    }

    /**
     * Get the function accuracy.
     *
     * @return function accuracy
     */
    @Override
    public Dfp getFunctionValueAccuracy() {
        return super.getFunctionValueAccuracy();
    }

    /**
     * Solve for a zero in the given interval. A solver may require that the interval brackets a
     * single zero root. Solvers that do require bracketing should be able to handle the case where
     * one of the endpoints is itself a root.
     *
     * @param maxEval Maximum number of evaluations.
     * @param f Function to solve.
     * @param min Lower bound for the interval.
     * @param max Upper bound for the interval.
     * @param allowedSolution The kind of solutions that the root-finding algorithm may accept as
     *     solutions.
     * @return a value where the function is zero.
     * @exception NullArgumentException if f is null.
     * @exception NoBracketingException if root cannot be bracketed
     */
    public Dfp solve(
            final int maxEval,
            final UnivariateDfpFunction f,
            final Dfp min,
            final Dfp max,
            final AllowedSolution allowedSolution)
            throws NullArgumentException, NoBracketingException {
        return solve(maxEval, f, min, max, min.add(max).divide(2), allowedSolution);
    }

    /**
     * Solve for a zero in the given interval, start at {@code startValue}. A solver may require
     * that the interval brackets a single zero root. Solvers that do require bracketing should be
     * able to handle the case where one of the endpoints is itself a root.
     *
     * @param maxEval Maximum number of evaluations.
     * @param f Function to solve.
     * @param min Lower bound for the interval.
     * @param max Upper bound for the interval.
     * @param startValue Start value to use.
     * @param allowedSolution The kind of solutions that the root-finding algorithm may accept as
     *     solutions.
     * @return a value where the function is zero.
     * @exception NullArgumentException if f is null.
     * @exception NoBracketingException if root cannot be bracketed
     */
    public Dfp solve(
            final int maxEval,
            final UnivariateDfpFunction f,
            final Dfp min,
            final Dfp max,
            final Dfp startValue,
            final AllowedSolution allowedSolution)
            throws NullArgumentException, NoBracketingException {

        // checks
        MathUtils.checkNotNull(f);

        // wrap the function
        RealFieldUnivariateFunction<Dfp> fieldF =
                new RealFieldUnivariateFunction<Dfp>() {

                    /** {@inheritDoc} */
                    public Dfp value(final Dfp x) {
                        return f.value(x);
                    }
                };

        // delegate to general field solver
        return solve(maxEval, fieldF, min, max, startValue, allowedSolution);
    }
}