View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  /*
19   * This is not the original file distributed by the Apache Software Foundation
20   * It has been modified by the Hipparchus project
21   */
22  
23  package org.hipparchus.analysis.solvers;
24  
25  import org.hipparchus.analysis.CalculusFieldUnivariateFunction;
26  import org.hipparchus.analysis.QuinticFunction;
27  import org.hipparchus.analysis.UnivariateFunction;
28  import org.hipparchus.analysis.function.Sin;
29  import org.hipparchus.exception.MathIllegalArgumentException;
30  import org.hipparchus.exception.NullArgumentException;
31  import org.hipparchus.util.Binary64;
32  import org.hipparchus.util.FastMath;
33  import org.junit.jupiter.api.Test;
34  
35  import static org.junit.jupiter.api.Assertions.assertEquals;
36  import static org.junit.jupiter.api.Assertions.assertThrows;
37  import static org.junit.jupiter.api.Assertions.assertTrue;
38  
39  /**
40   */
41  class UnivariateSolverUtilsTest {
42  
43      private UnivariateFunction sin = new Sin();
44      private CalculusFieldUnivariateFunction<Binary64> fieldSin = Binary64::sin;
45  
46      @Test
47      void testSolveNull() {
48          assertThrows(NullArgumentException.class, () -> UnivariateSolverUtils.solve(null, 0.0, 4.0));
49      }
50  
51      @Test
52      void testSolveBadEndpoints() {
53          assertThrows(MathIllegalArgumentException.class, () -> {
54              double root = UnivariateSolverUtils.solve(sin, 4.0, -0.1, 1e-6);
55              System.out.println("root=" + root);
56          });
57      }
58  
59      @Test
60      void testSolveBadAccuracy() {
61          try { // bad accuracy
62              UnivariateSolverUtils.solve(sin, 0.0, 4.0, 0.0);
63  //             Assertions.fail("Expecting MathIllegalArgumentException"); // TODO needs rework since convergence behaviour was changed
64          } catch (MathIllegalArgumentException ex) {
65              // expected
66          }
67      }
68  
69      @Test
70      void testSolveSin() {
71          double x = UnivariateSolverUtils.solve(sin, 1.0, 4.0);
72          assertEquals(FastMath.PI, x, 1.0e-4);
73      }
74  
75      @Test
76      void testSolveAccuracyNull()  {
77          assertThrows(NullArgumentException.class, () -> {
78              double accuracy = 1.0e-6;
79              UnivariateSolverUtils.solve(null, 0.0, 4.0, accuracy);
80          });
81      }
82  
83      @Test
84      void testSolveAccuracySin() {
85          double accuracy = 1.0e-6;
86          double x = UnivariateSolverUtils.solve(sin, 1.0,
87                  4.0, accuracy);
88          assertEquals(FastMath.PI, x, accuracy);
89      }
90  
91      @Test
92      void testSolveNoRoot() {
93          assertThrows(MathIllegalArgumentException.class, () -> UnivariateSolverUtils.solve(sin, 1.0, 1.5));
94      }
95  
96      @Test
97      void testBracketSin() {
98          double[] result = UnivariateSolverUtils.bracket(sin,
99                  0.0, -2.0, 2.0);
100         assertTrue(sin.value(result[0]) < 0);
101         assertTrue(sin.value(result[1]) > 0);
102     }
103 
104     @Test
105     void testBracketCentered() {
106         double initial = 0.1;
107         double[] result = UnivariateSolverUtils.bracket(sin, initial, -2.0, 2.0, 0.2, 1.0, 100);
108         assertTrue(result[0] < initial);
109         assertTrue(result[1] > initial);
110         assertTrue(sin.value(result[0]) < 0);
111         assertTrue(sin.value(result[1]) > 0);
112     }
113 
114     @Test
115     void testBracketLow() {
116         double initial = 0.5;
117         double[] result = UnivariateSolverUtils.bracket(sin, initial, -2.0, 2.0, 0.2, 1.0, 100);
118         assertTrue(result[0] < initial);
119         assertTrue(result[1] < initial);
120         assertTrue(sin.value(result[0]) < 0);
121         assertTrue(sin.value(result[1]) > 0);
122     }
123 
124     @Test
125     void testBracketHigh(){
126         double initial = -0.5;
127         double[] result = UnivariateSolverUtils.bracket(sin, initial, -2.0, 2.0, 0.2, 1.0, 100);
128         assertTrue(result[0] > initial);
129         assertTrue(result[1] > initial);
130         assertTrue(sin.value(result[0]) < 0);
131         assertTrue(sin.value(result[1]) > 0);
132     }
133 
134     @Test
135     void testBracketLinear(){
136         assertThrows(MathIllegalArgumentException.class, () -> UnivariateSolverUtils.bracket(x -> 1 - x, 1000, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, 100));
137     }
138 
139     @Test
140     void testBracketExponential(){
141         double[] result = UnivariateSolverUtils.bracket(x -> 1 - x, 1000, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, 1.0, 2.0, 10);
142         assertTrue(result[0] <= 1);
143         assertTrue(result[1] >= 1);
144     }
145 
146     @Test
147     void testBracketEndpointRoot() {
148         double[] result = UnivariateSolverUtils.bracket(sin, 1.5, 0, 2.0, 100);
149         assertEquals(0.0, sin.value(result[0]), 1.0e-15);
150         assertTrue(sin.value(result[1]) > 0);
151     }
152 
153     @Test
154     void testNullFunction() {
155         assertThrows(NullArgumentException.class, () -> UnivariateSolverUtils.bracket(null, 1.5, 0, 2.0));
156     }
157 
158     @Test
159     void testBadInitial() {
160         assertThrows(MathIllegalArgumentException.class, () -> UnivariateSolverUtils.bracket(sin, 2.5, 0, 2.0));
161     }
162 
163     @Test
164     void testBadAdditive() {
165         assertThrows(MathIllegalArgumentException.class, () -> UnivariateSolverUtils.bracket(sin, 1.0, -2.0, 3.0, -1.0, 1.0, 100));
166     }
167 
168     @Test
169     void testIterationExceeded() {
170         assertThrows(MathIllegalArgumentException.class, () -> UnivariateSolverUtils.bracket(sin, 1.0, -2.0, 3.0, 1.0e-5, 1.0, 100));
171     }
172 
173     @Test
174     void testBadEndpoints() {
175         assertThrows(MathIllegalArgumentException.class, () -> {
176             // endpoints not valid
177             UnivariateSolverUtils.bracket(sin, 1.5, 2.0, 1.0);
178         });
179     }
180 
181     @Test
182     void testBadMaximumIterations() {
183         assertThrows(MathIllegalArgumentException.class, () -> {
184             // bad maximum iterations
185             UnivariateSolverUtils.bracket(sin, 1.5, 0, 2.0, 0);
186         });
187     }
188 
189     @Test
190     void testFieldBracketSin() {
191         Binary64[] result = UnivariateSolverUtils.bracket(fieldSin,new Binary64(0.0),
192                                                            new Binary64(-2.0),new Binary64(2.0));
193         assertTrue(fieldSin.value(result[0]).getReal() < 0);
194         assertTrue(fieldSin.value(result[1]).getReal() > 0);
195     }
196 
197     @Test
198     void testFieldBracketCentered() {
199         Binary64 initial = new Binary64(0.1);
200         Binary64[] result = UnivariateSolverUtils.bracket(fieldSin, initial,
201                                                            new Binary64(-2.0), new Binary64(2.0),
202                                                            new Binary64(0.2), new Binary64(1.0),
203                                                            100);
204         assertTrue(result[0].getReal() < initial.getReal());
205         assertTrue(result[1].getReal() > initial.getReal());
206         assertTrue(fieldSin.value(result[0]).getReal() < 0);
207         assertTrue(fieldSin.value(result[1]).getReal() > 0);
208     }
209 
210     @Test
211     void testFieldBracketLow() {
212         Binary64 initial = new Binary64(0.5);
213         Binary64[] result = UnivariateSolverUtils.bracket(fieldSin, initial,
214                                                            new Binary64(-2.0), new Binary64(2.0),
215                                                            new Binary64(0.2), new Binary64(1.0),
216                                                            100);
217         assertTrue(result[0].getReal() < initial.getReal());
218         assertTrue(result[1].getReal() < initial.getReal());
219         assertTrue(fieldSin.value(result[0]).getReal() < 0);
220         assertTrue(fieldSin.value(result[1]).getReal() > 0);
221     }
222 
223     @Test
224     void testFieldBracketHigh(){
225         Binary64 initial = new Binary64(-0.5);
226         Binary64[] result = UnivariateSolverUtils.bracket(fieldSin, initial,
227                                                            new Binary64(-2.0), new Binary64(2.0),
228                                                            new Binary64(0.2), new Binary64(1.0),
229                                                            100);
230         assertTrue(result[0].getReal() > initial.getReal());
231         assertTrue(result[1].getReal() > initial.getReal());
232         assertTrue(fieldSin.value(result[0]).getReal() < 0);
233         assertTrue(fieldSin.value(result[1]).getReal() > 0);
234     }
235 
236     @Test
237     void testFieldBracketLinear(){
238         assertThrows(MathIllegalArgumentException.class, () -> UnivariateSolverUtils.bracket(x -> x.negate().add(1),
239             new Binary64(1000),
240             new Binary64(Double.NEGATIVE_INFINITY), new Binary64(Double.POSITIVE_INFINITY),
241             new Binary64(1.0), new Binary64(1.0), 100));
242     }
243 
244     @Test
245     void testFieldBracketExponential(){
246         Binary64[] result = UnivariateSolverUtils.bracket(x -> x.negate().add(1),
247         new Binary64(1000),
248         new Binary64(Double.NEGATIVE_INFINITY), new Binary64(Double.POSITIVE_INFINITY),
249         new Binary64(1.0), new Binary64(2.0), 10);
250         assertTrue(result[0].getReal() <= 1);
251         assertTrue(result[1].getReal() >= 1);
252     }
253 
254     @Test
255     void testFieldBracketEndpointRoot() {
256         Binary64[] result = UnivariateSolverUtils.bracket(fieldSin,
257                                                            new Binary64(1.5), new Binary64(0),
258                                                            new Binary64(2.0), 100);
259         assertEquals(0.0, fieldSin.value(result[0]).getReal(), 1.0e-15);
260         assertTrue(fieldSin.value(result[1]).getReal() > 0);
261     }
262 
263     @Test
264     void testFieldNullFunction() {
265         assertThrows(NullArgumentException.class, () -> UnivariateSolverUtils.bracket(null, new Binary64(1.5), new Binary64(0), new Binary64(2.0)));
266     }
267 
268     @Test
269     void testFieldBadInitial() {
270         assertThrows(MathIllegalArgumentException.class, () -> UnivariateSolverUtils.bracket(fieldSin, new Binary64(2.5), new Binary64(0), new Binary64(2.0)));
271     }
272 
273     @Test
274     void testFieldBadAdditive() {
275         assertThrows(MathIllegalArgumentException.class, () -> UnivariateSolverUtils.bracket(fieldSin, new Binary64(1.0), new Binary64(-2.0), new Binary64(3.0),
276             new Binary64(-1.0), new Binary64(1.0), 100));
277     }
278 
279     @Test
280     void testFieldIterationExceeded() {
281         assertThrows(MathIllegalArgumentException.class, () -> UnivariateSolverUtils.bracket(fieldSin, new Binary64(1.0), new Binary64(-2.0), new Binary64(3.0),
282             new Binary64(1.0e-5), new Binary64(1.0), 100));
283     }
284 
285     @Test
286     void testFieldBadEndpoints() {
287         assertThrows(MathIllegalArgumentException.class, () -> {
288             // endpoints not valid
289             UnivariateSolverUtils.bracket(fieldSin, new Binary64(1.5), new Binary64(2.0), new Binary64(1.0));
290         });
291     }
292 
293     @Test
294     void testFieldBadMaximumIterations() {
295         assertThrows(MathIllegalArgumentException.class, () -> {
296             // bad maximum iterations
297             UnivariateSolverUtils.bracket(fieldSin, new Binary64(1.5), new Binary64(0), new Binary64(2.0), 0);
298         });
299     }
300 
301     /** check the search continues when a = lowerBound and b &lt; upperBound. */
302     @Test
303     void testBracketLoopConditionForB() {
304         double[] result = UnivariateSolverUtils.bracket(sin, -0.9, -1, 1, 0.1, 1, 100);
305         assertTrue(result[0] <= 0);
306         assertTrue(result[1] >= 0);
307     }
308 
309     @Test
310     void testMisc() {
311         UnivariateFunction f = new QuinticFunction();
312         double result;
313         // Static solve method
314         result = UnivariateSolverUtils.solve(f, -0.2, 0.2);
315         assertEquals(0, result, 1E-8);
316         result = UnivariateSolverUtils.solve(f, -0.1, 0.3);
317         assertEquals(0, result, 1E-8);
318         result = UnivariateSolverUtils.solve(f, -0.3, 0.45);
319         assertEquals(0, result, 1E-6);
320         result = UnivariateSolverUtils.solve(f, 0.3, 0.7);
321         assertEquals(0.5, result, 1E-6);
322         result = UnivariateSolverUtils.solve(f, 0.2, 0.6);
323         assertEquals(0.5, result, 1E-6);
324         result = UnivariateSolverUtils.solve(f, 0.05, 0.95);
325         assertEquals(0.5, result, 1E-6);
326         result = UnivariateSolverUtils.solve(f, 0.85, 1.25);
327         assertEquals(1.0, result, 1E-6);
328         result = UnivariateSolverUtils.solve(f, 0.8, 1.2);
329         assertEquals(1.0, result, 1E-6);
330         result = UnivariateSolverUtils.solve(f, 0.85, 1.75);
331         assertEquals(1.0, result, 1E-6);
332         result = UnivariateSolverUtils.solve(f, 0.55, 1.45);
333         assertEquals(1.0, result, 1E-6);
334         result = UnivariateSolverUtils.solve(f, 0.85, 5);
335         assertEquals(1.0, result, 1E-6);
336     }
337 }