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 MullerSolver 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 MullerSolverTest {
48      /**
49       * Test of solver for the sine function.
50       */
51      @Test
52      void testSinFunction() {
53          UnivariateFunction f = new Sin();
54          UnivariateSolver solver = new MullerSolver();
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 MullerSolver();
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 10 to 15 iterations for the last two tests to converge.
102      * In fact, if not for the bisection alternative, the solver would
103      * exceed the default maximal iteration of 100.
104      */
105     @Test
106     void testExpm1Function() {
107         UnivariateFunction f = new Expm1();
108         UnivariateSolver solver = new MullerSolver();
109         double min, max, expected, result, tolerance;
110 
111         min = -1.0; max = 2.0; expected = 0.0;
112         tolerance = FastMath.max(solver.getAbsoluteAccuracy(),
113                     FastMath.abs(expected * solver.getRelativeAccuracy()));
114         result = solver.solve(100, f, min, max);
115         assertEquals(expected, result, tolerance);
116 
117         min = -20.0; max = 10.0; expected = 0.0;
118         tolerance = FastMath.max(solver.getAbsoluteAccuracy(),
119                     FastMath.abs(expected * solver.getRelativeAccuracy()));
120         result = solver.solve(100, f, min, max);
121         assertEquals(expected, result, tolerance);
122 
123         min = -50.0; max = 100.0; expected = 0.0;
124         tolerance = FastMath.max(solver.getAbsoluteAccuracy(),
125                     FastMath.abs(expected * solver.getRelativeAccuracy()));
126         result = solver.solve(100, f, min, max);
127         assertEquals(expected, result, tolerance);
128     }
129 
130     /**
131      * Test of parameters for the solver.
132      */
133     @Test
134     void testParameters() {
135         UnivariateFunction f = new Sin();
136         UnivariateSolver solver = new MullerSolver();
137 
138         try {
139             // bad interval
140             double root = solver.solve(100, f, 1, -1);
141             System.out.println("root=" + root);
142             fail("Expecting MathIllegalArgumentException - bad interval");
143         } catch (MathIllegalArgumentException ex) {
144             // expected
145         }
146         try {
147             // no bracketing
148             solver.solve(100, f, 2, 3);
149             fail("Expecting MathIllegalArgumentException - no bracketing");
150         } catch (MathIllegalArgumentException ex) {
151             // expected
152         }
153     }
154 }