1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
35
36
37
38
39
40
41
42 public final class LaguerreSolverTest {
43
44
45
46 @Test
47 public void testLinearFunction() {
48 double min, max, expected, result, tolerance;
49
50
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
64
65 @Test
66 public void testQuadraticFunction() {
67 double min, max, expected, result, tolerance;
68
69
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
89
90 @Test
91 public void testQuinticFunction() {
92 double min, max, expected, result, tolerance;
93
94
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
120
121
122 @Test
123 public void testQuinticFunction2() {
124
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
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
151 solver.solve(100, f, 1, -1);
152 Assert.fail("Expecting MathIllegalArgumentException - bad interval");
153 } catch (MathIllegalArgumentException ex) {
154
155 }
156 try {
157
158 solver.solve(100, f, 2, 3);
159 Assert.fail("Expecting MathIllegalArgumentException - no bracketing");
160 } catch (MathIllegalArgumentException ex) {
161
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 }