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.UnitTestUtils;
25  import org.hipparchus.analysis.polynomials.PolynomialFunction;
26  import org.hipparchus.complex.Complex;
27  import org.hipparchus.exception.MathIllegalArgumentException;
28  import org.hipparchus.util.FastMath;
29  import org.hipparchus.util.MathUtils;
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 Laguerre solver.
37   * <p>
38   * Laguerre's method is very efficient in solving polynomials. Test runs
39   * show that for a default absolute accuracy of 1E-6, it generally takes
40   * less than 5 iterations to find one root, provided solveAll() is not
41   * invoked, and 15 to 20 iterations to find all roots for quintic function.
42   *
43   */
44  final class LaguerreSolverTest {
45      /**
46       * Test of solver for the linear function.
47       */
48      @Test
49      void testLinearFunction() {
50          double min, max, expected, result, tolerance;
51  
52          // p(x) = 4x - 1
53          double[] coefficients = { -1.0, 4.0 };
54          PolynomialFunction f = new PolynomialFunction(coefficients);
55          LaguerreSolver solver = new LaguerreSolver();
56  
57          min = 0.0; max = 1.0; expected = 0.25;
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  
64      /**
65       * Test of solver for the quadratic function.
66       */
67      @Test
68      void testQuadraticFunction() {
69          double min, max, expected, result, tolerance;
70  
71          // p(x) = 2x^2 + 5x - 3 = (x+3)(2x-1)
72          double[] coefficients = { -3.0, 5.0, 2.0 };
73          PolynomialFunction f = new PolynomialFunction(coefficients);
74          LaguerreSolver solver = new LaguerreSolver();
75  
76          min = 0.0; max = 2.0; expected = 0.5;
77          tolerance = FastMath.max(solver.getAbsoluteAccuracy(),
78                      FastMath.abs(expected * solver.getRelativeAccuracy()));
79          result = solver.solve(100, f, min, max);
80          assertEquals(expected, result, tolerance);
81  
82          min = -4.0; max = -1.0; expected = -3.0;
83          tolerance = FastMath.max(solver.getAbsoluteAccuracy(),
84                      FastMath.abs(expected * solver.getRelativeAccuracy()));
85          result = solver.solve(100, f, min, max);
86          assertEquals(expected, result, tolerance);
87      }
88  
89      /**
90       * Test of solver for the quintic function.
91       */
92      @Test
93      void testQuinticFunction() {
94          double min, max, expected, result, tolerance;
95  
96          // p(x) = x^5 - x^4 - 12x^3 + x^2 - x - 12 = (x+1)(x+3)(x-4)(x^2-x+1)
97          double[] coefficients = { -12.0, -1.0, 1.0, -12.0, -1.0, 1.0 };
98          PolynomialFunction f = new PolynomialFunction(coefficients);
99          LaguerreSolver solver = new LaguerreSolver();
100 
101         min = -2.0; max = 2.0; expected = -1.0;
102         tolerance = FastMath.max(solver.getAbsoluteAccuracy(),
103                     FastMath.abs(expected * solver.getRelativeAccuracy()));
104         result = solver.solve(100, f, min, max);
105         assertEquals(expected, result, tolerance);
106 
107         min = -5.0; max = -2.5; expected = -3.0;
108         tolerance = FastMath.max(solver.getAbsoluteAccuracy(),
109                     FastMath.abs(expected * solver.getRelativeAccuracy()));
110         result = solver.solve(100, f, min, max);
111         assertEquals(expected, result, tolerance);
112 
113         min = 3.0; max = 6.0; expected = 4.0;
114         tolerance = FastMath.max(solver.getAbsoluteAccuracy(),
115                     FastMath.abs(expected * solver.getRelativeAccuracy()));
116         result = solver.solve(100, f, min, max);
117         assertEquals(expected, result, tolerance);
118     }
119 
120     /**
121      * Test of solver for the quintic function using
122      * {@link LaguerreSolver#solveAllComplex(double[], double) solveAllComplex}.
123      */
124     @Test
125     void testQuinticFunction2() {
126         // p(x) = x^5 + 4x^3 + x^2 + 4 = (x+1)(x^2-x+1)(x^2+4)
127         final double[] coefficients = { 4.0, 0.0, 1.0, 4.0, 0.0, 1.0 };
128         final LaguerreSolver solver = new LaguerreSolver();
129         final Complex[] result = solver.solveAllComplex(coefficients, 0);
130 
131         for (Complex expected : new Complex[] { new Complex(0, -2),
132                                                 new Complex(0, 2),
133                                                 new Complex(0.5, 0.5 * FastMath.sqrt(3)),
134                                                 new Complex(-1, 0),
135                                                 new Complex(0.5, -0.5 * FastMath.sqrt(3.0)) }) {
136             final double tolerance = FastMath.max(solver.getAbsoluteAccuracy(),
137                                                   FastMath.abs(expected.norm() * solver.getRelativeAccuracy()));
138             UnitTestUtils.customAssertContains(result, expected, tolerance);
139         }
140     }
141 
142     /**
143      * Test of parameters for the solver.
144      */
145     @Test
146     void testParameters() {
147         double[] coefficients = { -3.0, 5.0, 2.0 };
148         PolynomialFunction f = new PolynomialFunction(coefficients);
149         LaguerreSolver solver = new LaguerreSolver();
150 
151         try {
152             // bad interval
153             solver.solve(100, f, 1, -1);
154             fail("Expecting MathIllegalArgumentException - bad interval");
155         } catch (MathIllegalArgumentException ex) {
156             // expected
157         }
158         try {
159             // no bracketing
160             solver.solve(100, f, 2, 3);
161             fail("Expecting MathIllegalArgumentException - no bracketing");
162         } catch (MathIllegalArgumentException ex) {
163             // expected
164         }
165     }
166 
167     @Test
168     void testIssue177() {
169         doTestIssue177(new double[] {-100.0,    0.0, 0.0, 0.0, 1.0}, FastMath.sqrt(10.0));
170         doTestIssue177(new double[] {        -100.0, 0.0, 0.0, 1.0}, FastMath.cbrt(100.0));
171         doTestIssue177(new double[] { -16.0,    0.0, 0.0, 0.0, 1.0}, 2.0);
172     }
173 
174     private void doTestIssue177(final double[] coefficients, final double expected) {
175         Complex[] roots = new LaguerreSolver(1.0e-5).solveAllComplex(coefficients, 0);
176         assertEquals(coefficients.length - 1, roots.length);
177         for (final Complex root : roots) {
178             assertEquals(expected, root.norm(), 1.0e-15);
179             assertEquals(0.0, MathUtils.normalizeAngle(roots.length * root.getArgument(), 0.0), 1.0e-15);
180         }
181     }
182 
183 }