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.Assert;
31  import org.junit.Test;
32  
33  /**
34   * Test case for {@link MullerSolver Muller} solver.
35   * <p>
36   * Muller's method converges almost quadratically near roots, but it can
37   * be very slow in regions far away from zeros. Test runs show that for
38   * reasonably good initial values, for a default absolute accuracy of 1E-6,
39   * it generally takes 5 to 10 iterations for the solver to converge.
40   * <p>
41   * Tests for the exponential function illustrate the situations where
42   * Muller solver performs poorly.
43   *
44   */
45  public final class MullerSolverTest {
46      /**
47       * Test of solver for the sine function.
48       */
49      @Test
50      public void testSinFunction() {
51          UnivariateFunction f = new Sin();
52          UnivariateSolver solver = new MullerSolver();
53          double min, max, expected, result, tolerance;
54  
55          min = 3.0; max = 4.0; expected = FastMath.PI;
56          tolerance = FastMath.max(solver.getAbsoluteAccuracy(),
57                      FastMath.abs(expected * solver.getRelativeAccuracy()));
58          result = solver.solve(100, f, min, max);
59          Assert.assertEquals(expected, result, tolerance);
60  
61          min = -1.0; max = 1.5; expected = 0.0;
62          tolerance = FastMath.max(solver.getAbsoluteAccuracy(),
63                      FastMath.abs(expected * solver.getRelativeAccuracy()));
64          result = solver.solve(100, f, min, max);
65          Assert.assertEquals(expected, result, tolerance);
66      }
67  
68      /**
69       * Test of solver for the quintic function.
70       */
71      @Test
72      public void testQuinticFunction() {
73          UnivariateFunction f = new QuinticFunction();
74          UnivariateSolver solver = new MullerSolver();
75          double min, max, expected, result, tolerance;
76  
77          min = -0.4; max = 0.2; expected = 0.0;
78          tolerance = FastMath.max(solver.getAbsoluteAccuracy(),
79                      FastMath.abs(expected * solver.getRelativeAccuracy()));
80          result = solver.solve(100, f, min, max);
81          Assert.assertEquals(expected, result, tolerance);
82  
83          min = 0.75; max = 1.5; expected = 1.0;
84          tolerance = FastMath.max(solver.getAbsoluteAccuracy(),
85                      FastMath.abs(expected * solver.getRelativeAccuracy()));
86          result = solver.solve(100, f, min, max);
87          Assert.assertEquals(expected, result, tolerance);
88  
89          min = -0.9; max = -0.2; expected = -0.5;
90          tolerance = FastMath.max(solver.getAbsoluteAccuracy(),
91                      FastMath.abs(expected * solver.getRelativeAccuracy()));
92          result = solver.solve(100, f, min, max);
93          Assert.assertEquals(expected, result, tolerance);
94      }
95  
96      /**
97       * Test of solver for the exponential function.
98       * <p>
99       * It takes 10 to 15 iterations for the last two tests to converge.
100      * In fact, if not for the bisection alternative, the solver would
101      * exceed the default maximal iteration of 100.
102      */
103     @Test
104     public void testExpm1Function() {
105         UnivariateFunction f = new Expm1();
106         UnivariateSolver solver = new MullerSolver();
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         Assert.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         Assert.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         Assert.assertEquals(expected, result, tolerance);
126     }
127 
128     /**
129      * Test of parameters for the solver.
130      */
131     @Test
132     public void testParameters() {
133         UnivariateFunction f = new Sin();
134         UnivariateSolver solver = new MullerSolver();
135 
136         try {
137             // bad interval
138             double root = solver.solve(100, f, 1, -1);
139             System.out.println("root=" + root);
140             Assert.fail("Expecting MathIllegalArgumentException - bad interval");
141         } catch (MathIllegalArgumentException ex) {
142             // expected
143         }
144         try {
145             // no bracketing
146             solver.solve(100, f, 2, 3);
147             Assert.fail("Expecting MathIllegalArgumentException - no bracketing");
148         } catch (MathIllegalArgumentException ex) {
149             // expected
150         }
151     }
152 }