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 import org.hipparchus.exception.LocalizedCoreFormats;
25 import org.hipparchus.exception.MathIllegalArgumentException;
26 import org.hipparchus.exception.MathIllegalStateException;
27 import org.hipparchus.util.FastMath;
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 public class MullerSolver2 extends AbstractUnivariateSolver {
54
55
56 private static final double DEFAULT_ABSOLUTE_ACCURACY = 1e-6;
57
58
59
60
61 public MullerSolver2() {
62 this(DEFAULT_ABSOLUTE_ACCURACY);
63 }
64
65
66
67
68
69 public MullerSolver2(double absoluteAccuracy) {
70 super(absoluteAccuracy);
71 }
72
73
74
75
76
77
78 public MullerSolver2(double relativeAccuracy,
79 double absoluteAccuracy) {
80 super(relativeAccuracy, absoluteAccuracy);
81 }
82
83
84
85
86 @Override
87 protected double doSolve()
88 throws MathIllegalArgumentException, MathIllegalStateException {
89 final double min = getMin();
90 final double max = getMax();
91
92 verifyInterval(min, max);
93
94 final double relativeAccuracy = getRelativeAccuracy();
95 final double absoluteAccuracy = getAbsoluteAccuracy();
96 final double functionValueAccuracy = getFunctionValueAccuracy();
97
98
99
100
101
102 double x0 = min;
103 double y0 = computeObjectiveValue(x0);
104 if (FastMath.abs(y0) < functionValueAccuracy) {
105 return x0;
106 }
107 double x1 = max;
108 double y1 = computeObjectiveValue(x1);
109 if (FastMath.abs(y1) < functionValueAccuracy) {
110 return x1;
111 }
112
113 if(y0 * y1 > 0) {
114 throw new MathIllegalArgumentException(LocalizedCoreFormats.NOT_BRACKETING_INTERVAL,
115 x0, x1, y0, y1);
116 }
117
118 double x2 = 0.5 * (x0 + x1);
119 double y2 = computeObjectiveValue(x2);
120
121 double oldx = Double.POSITIVE_INFINITY;
122 while (true) {
123
124 final double q = (x2 - x1) / (x1 - x0);
125 final double a = q * (y2 - (1 + q) * y1 + q * y0);
126 final double b = (2 * q + 1) * y2 - (1 + q) * (1 + q) * y1 + q * q * y0;
127 final double c = (1 + q) * y2;
128 final double delta = b * b - 4 * a * c;
129 double x;
130 final double denominator;
131 if (delta >= 0.0) {
132
133 double dplus = b + FastMath.sqrt(delta);
134 double dminus = b - FastMath.sqrt(delta);
135 denominator = FastMath.abs(dplus) > FastMath.abs(dminus) ? dplus : dminus;
136 } else {
137
138 denominator = FastMath.sqrt(b * b - delta);
139 }
140 if (denominator != 0) {
141 x = x2 - 2.0 * c * (x2 - x1) / denominator;
142
143
144 while (x == x1 || x == x2) {
145 x += absoluteAccuracy;
146 }
147 } else {
148
149 x = min + FastMath.random() * (max - min);
150 oldx = Double.POSITIVE_INFINITY;
151 }
152 final double y = computeObjectiveValue(x);
153
154
155 final double tolerance = FastMath.max(relativeAccuracy * FastMath.abs(x), absoluteAccuracy);
156 if (FastMath.abs(x - oldx) <= tolerance ||
157 FastMath.abs(y) <= functionValueAccuracy) {
158 return x;
159 }
160
161
162 x0 = x1;
163 y0 = y1;
164 x1 = x2;
165 y1 = y2;
166 x2 = x;
167 y2 = y;
168 oldx = x;
169 }
170 }
171 }