1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.math4.legacy.analysis.solvers;
18
19 import org.apache.commons.numbers.complex.Complex;
20 import org.apache.commons.math4.legacy.analysis.polynomials.PolynomialFunction;
21 import org.apache.commons.math4.legacy.exception.NoBracketingException;
22 import org.apache.commons.math4.legacy.exception.NoDataException;
23 import org.apache.commons.math4.legacy.exception.NullArgumentException;
24 import org.apache.commons.math4.legacy.exception.NumberIsTooLargeException;
25 import org.apache.commons.math4.legacy.exception.TooManyEvaluationsException;
26 import org.apache.commons.math4.legacy.exception.util.LocalizedFormats;
27 import org.apache.commons.math4.core.jdkmath.JdkMath;
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43 public class LaguerreSolver extends AbstractPolynomialSolver {
44
45 private static final double DEFAULT_ABSOLUTE_ACCURACY = 1e-6;
46
47 private final ComplexSolver complexSolver = new ComplexSolver();
48
49
50
51
52 public LaguerreSolver() {
53 this(DEFAULT_ABSOLUTE_ACCURACY);
54 }
55
56
57
58
59
60 public LaguerreSolver(double absoluteAccuracy) {
61 super(absoluteAccuracy);
62 }
63
64
65
66
67
68
69 public LaguerreSolver(double relativeAccuracy,
70 double absoluteAccuracy) {
71 super(relativeAccuracy, absoluteAccuracy);
72 }
73
74
75
76
77
78
79
80 public LaguerreSolver(double relativeAccuracy,
81 double absoluteAccuracy,
82 double functionValueAccuracy) {
83 super(relativeAccuracy, absoluteAccuracy, functionValueAccuracy);
84 }
85
86
87
88
89 @Override
90 public double doSolve()
91 throws TooManyEvaluationsException,
92 NumberIsTooLargeException,
93 NoBracketingException {
94 final double min = getMin();
95 final double max = getMax();
96 final double initial = getStartValue();
97 final double functionValueAccuracy = getFunctionValueAccuracy();
98
99 verifySequence(min, initial, max);
100
101
102 final double yInitial = computeObjectiveValue(initial);
103 if (JdkMath.abs(yInitial) <= functionValueAccuracy) {
104 return initial;
105 }
106
107
108 final double yMin = computeObjectiveValue(min);
109 if (JdkMath.abs(yMin) <= functionValueAccuracy) {
110 return min;
111 }
112
113
114 if (yInitial * yMin < 0) {
115 return laguerre(min, initial);
116 }
117
118
119 final double yMax = computeObjectiveValue(max);
120 if (JdkMath.abs(yMax) <= functionValueAccuracy) {
121 return max;
122 }
123
124
125 if (yInitial * yMax < 0) {
126 return laguerre(initial, max);
127 }
128
129 throw new NoBracketingException(min, max, yMin, yMax);
130 }
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148 private double laguerre(double lo, double hi) {
149 final Complex[] c = real2Complex(getCoefficients());
150
151 final Complex initial = Complex.ofCartesian(0.5 * (lo + hi), 0);
152 final Complex z = complexSolver.solve(c, initial);
153 if (complexSolver.isRoot(lo, hi, z)) {
154 return z.getReal();
155 } else {
156 double r = Double.NaN;
157
158 Complex[] root = complexSolver.solveAll(c, initial);
159 for (int i = 0; i < root.length; i++) {
160 if (complexSolver.isRoot(lo, hi, root[i])) {
161 r = root[i].getReal();
162 break;
163 }
164 }
165 return r;
166 }
167 }
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185 public Complex[] solveAllComplex(double[] coefficients,
186 double initial)
187 throws NullArgumentException,
188 NoDataException,
189 TooManyEvaluationsException {
190 setup(Integer.MAX_VALUE,
191 new PolynomialFunction(coefficients),
192 Double.NEGATIVE_INFINITY,
193 Double.POSITIVE_INFINITY,
194 initial);
195 return complexSolver.solveAll(real2Complex(coefficients),
196 Complex.ofCartesian(initial, 0d));
197 }
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215 public Complex solveComplex(double[] coefficients,
216 double initial)
217 throws NullArgumentException,
218 NoDataException,
219 TooManyEvaluationsException {
220 setup(Integer.MAX_VALUE,
221 new PolynomialFunction(coefficients),
222 Double.NEGATIVE_INFINITY,
223 Double.POSITIVE_INFINITY,
224 initial);
225 return complexSolver.solve(real2Complex(coefficients),
226 Complex.ofCartesian(initial, 0d));
227 }
228
229
230
231
232 private final class ComplexSolver {
233
234
235
236
237
238
239
240
241
242 public boolean isRoot(double min, double max, Complex z) {
243 if (isSequence(min, z.getReal(), max)) {
244 double tolerance = JdkMath.max(getRelativeAccuracy() * z.abs(), getAbsoluteAccuracy());
245 return JdkMath.abs(z.getImaginary()) <= tolerance ||
246 z.abs() <= getFunctionValueAccuracy();
247 }
248 return false;
249 }
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264 public Complex[] solveAll(Complex[] coefficients, Complex initial)
265 throws NullArgumentException,
266 NoDataException,
267 TooManyEvaluationsException {
268 if (coefficients == null) {
269 throw new NullArgumentException();
270 }
271 final int n = coefficients.length - 1;
272 if (n == 0) {
273 throw new NoDataException(LocalizedFormats.POLYNOMIAL);
274 }
275
276 final Complex[] c = coefficients.clone();
277
278
279 final Complex[] root = new Complex[n];
280 for (int i = 0; i < n; i++) {
281 final Complex[] subarray = new Complex[n - i + 1];
282 System.arraycopy(c, 0, subarray, 0, subarray.length);
283 root[i] = solve(subarray, initial);
284
285 Complex newc = c[n - i];
286 Complex oldc = null;
287 for (int j = n - i - 1; j >= 0; j--) {
288 oldc = c[j];
289 c[j] = newc;
290 newc = oldc.add(newc.multiply(root[i]));
291 }
292 }
293
294 return root;
295 }
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310 public Complex solve(Complex[] coefficients, Complex initial)
311 throws NullArgumentException,
312 NoDataException,
313 TooManyEvaluationsException {
314 if (coefficients == null) {
315 throw new NullArgumentException();
316 }
317
318 final int n = coefficients.length - 1;
319 if (n == 0) {
320 throw new NoDataException(LocalizedFormats.POLYNOMIAL);
321 }
322
323 final double absoluteAccuracy = getAbsoluteAccuracy();
324 final double relativeAccuracy = getRelativeAccuracy();
325 final double functionValueAccuracy = getFunctionValueAccuracy();
326
327 final Complex nC = Complex.ofCartesian(n, 0);
328 final Complex n1C = Complex.ofCartesian(n - 1, 0);
329
330 Complex z = initial;
331 Complex oldz = Complex.ofCartesian(Double.POSITIVE_INFINITY,
332 Double.POSITIVE_INFINITY);
333 while (true) {
334
335
336 Complex pv = coefficients[n];
337 Complex dv = Complex.ZERO;
338 Complex d2v = Complex.ZERO;
339 for (int j = n-1; j >= 0; j--) {
340 d2v = dv.add(z.multiply(d2v));
341 dv = pv.add(z.multiply(dv));
342 pv = coefficients[j].add(z.multiply(pv));
343 }
344 d2v = d2v.multiply(2);
345
346
347 final double tolerance = JdkMath.max(relativeAccuracy * z.abs(),
348 absoluteAccuracy);
349 if ((z.subtract(oldz)).abs() <= tolerance) {
350 return z;
351 }
352 if (pv.abs() <= functionValueAccuracy) {
353 return z;
354 }
355
356
357 final Complex g = dv.divide(pv);
358 final Complex g2 = g.multiply(g);
359 final Complex h = g2.subtract(d2v.divide(pv));
360 final Complex delta = n1C.multiply((nC.multiply(h)).subtract(g2));
361
362 final Complex deltaSqrt = delta.sqrt();
363 final Complex dplus = g.add(deltaSqrt);
364 final Complex dminus = g.subtract(deltaSqrt);
365 final Complex denominator = dplus.abs() > dminus.abs() ? dplus : dminus;
366
367
368
369 if (denominator.equals(Complex.ZERO)) {
370 z = z.add(Complex.ofCartesian(absoluteAccuracy, absoluteAccuracy));
371 oldz = Complex.ofCartesian(Double.POSITIVE_INFINITY,
372 Double.POSITIVE_INFINITY);
373 } else {
374 oldz = z;
375 z = z.subtract(nC.divide(denominator));
376 }
377 incrementEvaluationCount();
378 }
379 }
380 }
381
382
383
384
385
386
387
388 private static Complex[] real2Complex(double[] real) {
389 int index = 0;
390 final Complex[] c = new Complex[real.length];
391 for (final double d : real) {
392 c[index] = Complex.ofCartesian(d, 0);
393 index++;
394 }
395 return c;
396 }
397 }