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