1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.hipparchus.analysis.solvers;
23
24
25 import org.hipparchus.exception.LocalizedCoreFormats;
26 import org.hipparchus.exception.MathIllegalArgumentException;
27 import org.hipparchus.exception.MathIllegalStateException;
28 import org.hipparchus.util.FastMath;
29 import org.hipparchus.util.Precision;
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50 public class BrentSolver extends AbstractUnivariateSolver {
51
52
53 private static final double DEFAULT_ABSOLUTE_ACCURACY = 1e-6;
54
55
56
57
58 public BrentSolver() {
59 this(DEFAULT_ABSOLUTE_ACCURACY);
60 }
61
62
63
64
65
66 public BrentSolver(double absoluteAccuracy) {
67 super(absoluteAccuracy);
68 }
69
70
71
72
73
74
75 public BrentSolver(double relativeAccuracy,
76 double absoluteAccuracy) {
77 super(relativeAccuracy, absoluteAccuracy);
78 }
79
80
81
82
83
84
85
86
87
88 public BrentSolver(double relativeAccuracy,
89 double absoluteAccuracy,
90 double functionValueAccuracy) {
91 super(relativeAccuracy, absoluteAccuracy, functionValueAccuracy);
92 }
93
94
95
96
97 @Override
98 protected double doSolve()
99 throws MathIllegalArgumentException, MathIllegalStateException {
100 double min = getMin();
101 double max = getMax();
102 final double initial = getStartValue();
103 final double functionValueAccuracy = getFunctionValueAccuracy();
104
105 verifySequence(min, initial, max);
106
107
108 double yInitial = computeObjectiveValue(initial);
109 if (FastMath.abs(yInitial) <= functionValueAccuracy) {
110 return initial;
111 }
112
113
114 double yMin = computeObjectiveValue(min);
115 if (FastMath.abs(yMin) <= functionValueAccuracy) {
116 return min;
117 }
118
119
120 if (yInitial * yMin < 0) {
121 return brent(min, initial, yMin, yInitial);
122 }
123
124
125 double yMax = computeObjectiveValue(max);
126 if (FastMath.abs(yMax) <= functionValueAccuracy) {
127 return max;
128 }
129
130
131 if (yInitial * yMax < 0) {
132 return brent(initial, max, yInitial, yMax);
133 }
134
135 throw new MathIllegalArgumentException(LocalizedCoreFormats.NOT_BRACKETING_INTERVAL,
136 min, max, yMin, yMax);
137 }
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155 private double brent(double lo, double hi,
156 double fLo, double fHi) {
157 double a = lo;
158 double fa = fLo;
159 double b = hi;
160 double fb = fHi;
161 double c = a;
162 double fc = fa;
163 double d = b - a;
164 double e = d;
165
166 final double t = getAbsoluteAccuracy();
167 final double eps = getRelativeAccuracy();
168
169 while (true) {
170 if (FastMath.abs(fc) < FastMath.abs(fb)) {
171 a = b;
172 b = c;
173 c = a;
174 fa = fb;
175 fb = fc;
176 fc = fa;
177 }
178
179 final double tol = 2 * eps * FastMath.abs(b) + t;
180 final double m = 0.5 * (c - b);
181
182 if (FastMath.abs(m) <= tol ||
183 Precision.equals(fb, 0)) {
184 return b;
185 }
186 if (FastMath.abs(e) < tol ||
187 FastMath.abs(fa) <= FastMath.abs(fb)) {
188
189 d = m;
190 e = d;
191 } else {
192 double s = fb / fa;
193 double p;
194 double q;
195
196
197
198 if (a == c) {
199
200 p = 2 * m * s;
201 q = 1 - s;
202 } else {
203
204 q = fa / fc;
205 final double r = fb / fc;
206 p = s * (2 * m * q * (q - r) - (b - a) * (r - 1));
207 q = (q - 1) * (r - 1) * (s - 1);
208 }
209 if (p > 0) {
210 q = -q;
211 } else {
212 p = -p;
213 }
214 s = e;
215 e = d;
216 if (p >= 1.5 * m * q - FastMath.abs(tol * q) ||
217 p >= FastMath.abs(0.5 * s * q)) {
218
219
220
221 d = m;
222 e = d;
223 } else {
224 d = p / q;
225 }
226 }
227 a = b;
228 fa = fb;
229
230 if (FastMath.abs(d) > tol) {
231 b += d;
232 } else if (m > 0) {
233 b += tol;
234 } else {
235 b -= tol;
236 }
237 fb = computeObjectiveValue(b);
238 if ((fb > 0 && fc > 0) ||
239 (fb <= 0 && fc <= 0)) {
240 c = a;
241 fc = fa;
242 d = b - a;
243 e = d;
244 }
245 }
246 }
247 }