1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
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  
30  
31  
32  
33  
34  
35  
36  
37  public final class LaguerreSolverTest {
38      
39  
40  
41      @Test
42      public void testLinearFunction() {
43          double min;
44          double max;
45          double expected;
46          double result;
47          double tolerance;
48  
49          
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  
63  
64      @Test
65      public void testQuadraticFunction() {
66          double min;
67          double max;
68          double expected;
69          double result;
70          double tolerance;
71  
72          
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  
92  
93      @Test
94      public void testQuinticFunction() {
95          double min;
96          double max;
97          double expected;
98          double result;
99          double tolerance;
100 
101         
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 
127 
128 
129     @Test
130     public void testQuinticFunction2() {
131         
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 
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             
158             solver.solve(100, f, 1, -1);
159             Assert.fail("Expecting NumberIsTooLargeException - bad interval");
160         } catch (NumberIsTooLargeException ex) {
161             
162         }
163         try {
164             
165             solver.solve(100, f, 2, 3);
166             Assert.fail("Expecting NoBracketingException - no bracketing");
167         } catch (NoBracketingException ex) {
168             
169         }
170     }
171 }