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