1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.math.analysis.solvers;
18
19 import org.apache.commons.math.MathException;
20 import org.apache.commons.math.TestUtils;
21 import org.apache.commons.math.analysis.SinFunction;
22 import org.apache.commons.math.analysis.polynomials.PolynomialFunction;
23 import org.apache.commons.math.complex.Complex;
24 import junit.framework.TestCase;
25
26
27
28
29
30
31
32
33
34
35
36 public final class LaguerreSolverTest extends TestCase {
37
38
39
40
41 @Deprecated
42 public void testDeprecated() throws MathException {
43 double min, max, expected, result, tolerance;
44
45
46 double coefficients[] = { -1.0, 4.0 };
47 PolynomialFunction f = new PolynomialFunction(coefficients);
48 UnivariateRealSolver solver = new LaguerreSolver(f);
49
50 min = 0.0; max = 1.0; expected = 0.25;
51 tolerance = Math.max(solver.getAbsoluteAccuracy(),
52 Math.abs(expected * solver.getRelativeAccuracy()));
53 result = solver.solve(min, max);
54 assertEquals(expected, result, tolerance);
55 }
56
57
58
59
60 public void testLinearFunction() throws MathException {
61 double min, max, expected, result, tolerance;
62
63
64 double coefficients[] = { -1.0, 4.0 };
65 PolynomialFunction f = new PolynomialFunction(coefficients);
66 UnivariateRealSolver solver = new LaguerreSolver();
67
68 min = 0.0; max = 1.0; expected = 0.25;
69 tolerance = Math.max(solver.getAbsoluteAccuracy(),
70 Math.abs(expected * solver.getRelativeAccuracy()));
71 result = solver.solve(f, min, max);
72 assertEquals(expected, result, tolerance);
73 }
74
75
76
77
78 public void testQuadraticFunction() throws MathException {
79 double min, max, expected, result, tolerance;
80
81
82 double coefficients[] = { -3.0, 5.0, 2.0 };
83 PolynomialFunction f = new PolynomialFunction(coefficients);
84 UnivariateRealSolver solver = new LaguerreSolver();
85
86 min = 0.0; max = 2.0; expected = 0.5;
87 tolerance = Math.max(solver.getAbsoluteAccuracy(),
88 Math.abs(expected * solver.getRelativeAccuracy()));
89 result = solver.solve(f, min, max);
90 assertEquals(expected, result, tolerance);
91
92 min = -4.0; max = -1.0; expected = -3.0;
93 tolerance = Math.max(solver.getAbsoluteAccuracy(),
94 Math.abs(expected * solver.getRelativeAccuracy()));
95 result = solver.solve(f, min, max);
96 assertEquals(expected, result, tolerance);
97 }
98
99
100
101
102 public void testQuinticFunction() throws MathException {
103 double min, max, expected, result, tolerance;
104
105
106 double coefficients[] = { -12.0, -1.0, 1.0, -12.0, -1.0, 1.0 };
107 PolynomialFunction f = new PolynomialFunction(coefficients);
108 UnivariateRealSolver solver = new LaguerreSolver();
109
110 min = -2.0; max = 2.0; expected = -1.0;
111 tolerance = Math.max(solver.getAbsoluteAccuracy(),
112 Math.abs(expected * solver.getRelativeAccuracy()));
113 result = solver.solve(f, min, max);
114 assertEquals(expected, result, tolerance);
115
116 min = -5.0; max = -2.5; expected = -3.0;
117 tolerance = Math.max(solver.getAbsoluteAccuracy(),
118 Math.abs(expected * solver.getRelativeAccuracy()));
119 result = solver.solve(f, min, max);
120 assertEquals(expected, result, tolerance);
121
122 min = 3.0; max = 6.0; expected = 4.0;
123 tolerance = Math.max(solver.getAbsoluteAccuracy(),
124 Math.abs(expected * solver.getRelativeAccuracy()));
125 result = solver.solve(f, min, max);
126 assertEquals(expected, result, tolerance);
127 }
128
129
130
131
132 public void testQuinticFunction2() throws MathException {
133 double initial = 0.0, tolerance;
134 Complex expected, result[];
135
136
137 double coefficients[] = { 4.0, 0.0, 1.0, 4.0, 0.0, 1.0 };
138 LaguerreSolver solver = new LaguerreSolver();
139 result = solver.solveAll(coefficients, initial);
140
141 expected = new Complex(0.0, -2.0);
142 tolerance = Math.max(solver.getAbsoluteAccuracy(),
143 Math.abs(expected.abs() * solver.getRelativeAccuracy()));
144 TestUtils.assertContains(result, expected, tolerance);
145
146 expected = new Complex(0.0, 2.0);
147 tolerance = Math.max(solver.getAbsoluteAccuracy(),
148 Math.abs(expected.abs() * solver.getRelativeAccuracy()));
149 TestUtils.assertContains(result, expected, tolerance);
150
151 expected = new Complex(0.5, 0.5 * Math.sqrt(3.0));
152 tolerance = Math.max(solver.getAbsoluteAccuracy(),
153 Math.abs(expected.abs() * solver.getRelativeAccuracy()));
154 TestUtils.assertContains(result, expected, tolerance);
155
156 expected = new Complex(-1.0, 0.0);
157 tolerance = Math.max(solver.getAbsoluteAccuracy(),
158 Math.abs(expected.abs() * solver.getRelativeAccuracy()));
159 TestUtils.assertContains(result, expected, tolerance);
160
161 expected = new Complex(0.5, -0.5 * Math.sqrt(3.0));
162 tolerance = Math.max(solver.getAbsoluteAccuracy(),
163 Math.abs(expected.abs() * solver.getRelativeAccuracy()));
164 TestUtils.assertContains(result, expected, tolerance);
165 }
166
167
168
169
170 public void testParameters() throws Exception {
171 double coefficients[] = { -3.0, 5.0, 2.0 };
172 PolynomialFunction f = new PolynomialFunction(coefficients);
173 UnivariateRealSolver solver = new LaguerreSolver();
174
175 try {
176
177 solver.solve(f, 1, -1);
178 fail("Expecting IllegalArgumentException - bad interval");
179 } catch (IllegalArgumentException ex) {
180
181 }
182 try {
183
184 solver.solve(f, 2, 3);
185 fail("Expecting IllegalArgumentException - no bracketing");
186 } catch (IllegalArgumentException ex) {
187
188 }
189 try {
190
191 solver.solve(new SinFunction(), -1, 1);
192 fail("Expecting IllegalArgumentException - bad function");
193 } catch (IllegalArgumentException ex) {
194
195 }
196 }
197 }