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  package org.hipparchus.analysis.solvers;
23  
24  import org.hipparchus.analysis.QuinticFunction;
25  import org.hipparchus.analysis.UnivariateFunction;
26  import org.hipparchus.analysis.function.Expm1;
27  import org.hipparchus.analysis.function.Sin;
28  import org.hipparchus.exception.MathIllegalArgumentException;
29  import org.hipparchus.util.FastMath;
30  import org.junit.jupiter.api.Test;
31  
32  import static org.junit.jupiter.api.Assertions.assertEquals;
33  import static org.junit.jupiter.api.Assertions.fail;
34  
35  /**
36   * Test case for {@link MullerSolver2 Muller} solver.
37   * <p>
38   * Muller's method converges almost quadratically near roots, but it can
39   * be very slow in regions far away from zeros. Test runs show that for
40   * reasonably good initial values, for a default absolute accuracy of 1E-6,
41   * it generally takes 5 to 10 iterations for the solver to converge.
42   * <p>
43   * Tests for the exponential function illustrate the situations where
44   * Muller solver performs poorly.
45   *
46   */
47  final class MullerSolver2Test {
48      /**
49       * Test of solver for the sine function.
50       */
51      @Test
52      void testSinFunction() {
53          UnivariateFunction f = new Sin();
54          UnivariateSolver solver = new MullerSolver2();
55          double min, max, expected, result, tolerance;
56  
57          min = 3.0; max = 4.0; expected = FastMath.PI;
58          tolerance = FastMath.max(solver.getAbsoluteAccuracy(),
59                      FastMath.abs(expected * solver.getRelativeAccuracy()));
60          result = solver.solve(100, f, min, max);
61          assertEquals(expected, result, tolerance);
62  
63          min = -1.0; max = 1.5; expected = 0.0;
64          tolerance = FastMath.max(solver.getAbsoluteAccuracy(),
65                      FastMath.abs(expected * solver.getRelativeAccuracy()));
66          result = solver.solve(100, f, min, max);
67          assertEquals(expected, result, tolerance);
68      }
69  
70      /**
71       * Test of solver for the quintic function.
72       */
73      @Test
74      void testQuinticFunction() {
75          UnivariateFunction f = new QuinticFunction();
76          UnivariateSolver solver = new MullerSolver2();
77          double min, max, expected, result, tolerance;
78  
79          min = -0.4; max = 0.2; expected = 0.0;
80          tolerance = FastMath.max(solver.getAbsoluteAccuracy(),
81                      FastMath.abs(expected * solver.getRelativeAccuracy()));
82          result = solver.solve(100, f, min, max);
83          assertEquals(expected, result, tolerance);
84  
85          min = 0.75; max = 1.5; expected = 1.0;
86          tolerance = FastMath.max(solver.getAbsoluteAccuracy(),
87                      FastMath.abs(expected * solver.getRelativeAccuracy()));
88          result = solver.solve(100, f, min, max);
89          assertEquals(expected, result, tolerance);
90  
91          min = -0.9; max = -0.2; expected = -0.5;
92          tolerance = FastMath.max(solver.getAbsoluteAccuracy(),
93                      FastMath.abs(expected * solver.getRelativeAccuracy()));
94          result = solver.solve(100, f, min, max);
95          assertEquals(expected, result, tolerance);
96      }
97  
98      /**
99       * Test of solver for the exponential function.
100      * <p>
101      * It takes 25 to 50 iterations for the last two tests to converge.
102      */
103     @Test
104     void testExpm1Function() {
105         UnivariateFunction f = new Expm1();
106         UnivariateSolver solver = new MullerSolver2();
107         double min, max, expected, result, tolerance;
108 
109         min = -1.0; max = 2.0; expected = 0.0;
110         tolerance = FastMath.max(solver.getAbsoluteAccuracy(),
111                     FastMath.abs(expected * solver.getRelativeAccuracy()));
112         result = solver.solve(100, f, min, max);
113         assertEquals(expected, result, tolerance);
114 
115         min = -20.0; max = 10.0; expected = 0.0;
116         tolerance = FastMath.max(solver.getAbsoluteAccuracy(),
117                     FastMath.abs(expected * solver.getRelativeAccuracy()));
118         result = solver.solve(100, f, min, max);
119         assertEquals(expected, result, tolerance);
120 
121         min = -50.0; max = 100.0; expected = 0.0;
122         tolerance = FastMath.max(solver.getAbsoluteAccuracy(),
123                     FastMath.abs(expected * solver.getRelativeAccuracy()));
124         result = solver.solve(100, f, min, max);
125         assertEquals(expected, result, tolerance);
126     }
127 
128     /**
129      * Test of parameters for the solver.
130      */
131     @Test
132     void testParameters() {
133         UnivariateFunction f = new Sin();
134         UnivariateSolver solver = new MullerSolver2();
135 
136         try {
137             // bad interval
138             solver.solve(100, f, 1, -1);
139             fail("Expecting MathIllegalArgumentException - bad interval");
140         } catch (MathIllegalArgumentException ex) {
141             // expected
142         }
143         try {
144             // no bracketing
145             solver.solve(100, f, 2, 3);
146             fail("Expecting MathIllegalArgumentException - no bracketing");
147         } catch (MathIllegalArgumentException ex) {
148             // expected
149         }
150     }
151 }