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.jupiter.api.Test;
31
32 import static org.junit.jupiter.api.Assertions.assertEquals;
33 import static org.junit.jupiter.api.Assertions.fail;
34
35
36
37
38
39
40
41
42
43
44 final class LaguerreSolverTest {
45
46
47
48 @Test
49 void testLinearFunction() {
50 double min, max, expected, result, tolerance;
51
52
53 double[] coefficients = { -1.0, 4.0 };
54 PolynomialFunction f = new PolynomialFunction(coefficients);
55 LaguerreSolver solver = new LaguerreSolver();
56
57 min = 0.0; max = 1.0; expected = 0.25;
58 tolerance = FastMath.max(solver.getAbsoluteAccuracy(),
59 FastMath.abs(expected * solver.getRelativeAccuracy()));
60 result = solver.solve(100, f, min, max);
61 assertEquals(expected, result, tolerance);
62 }
63
64
65
66
67 @Test
68 void testQuadraticFunction() {
69 double min, max, expected, result, tolerance;
70
71
72 double[] coefficients = { -3.0, 5.0, 2.0 };
73 PolynomialFunction f = new PolynomialFunction(coefficients);
74 LaguerreSolver solver = new LaguerreSolver();
75
76 min = 0.0; max = 2.0; expected = 0.5;
77 tolerance = FastMath.max(solver.getAbsoluteAccuracy(),
78 FastMath.abs(expected * solver.getRelativeAccuracy()));
79 result = solver.solve(100, f, min, max);
80 assertEquals(expected, result, tolerance);
81
82 min = -4.0; max = -1.0; expected = -3.0;
83 tolerance = FastMath.max(solver.getAbsoluteAccuracy(),
84 FastMath.abs(expected * solver.getRelativeAccuracy()));
85 result = solver.solve(100, f, min, max);
86 assertEquals(expected, result, tolerance);
87 }
88
89
90
91
92 @Test
93 void testQuinticFunction() {
94 double min, max, expected, result, tolerance;
95
96
97 double[] coefficients = { -12.0, -1.0, 1.0, -12.0, -1.0, 1.0 };
98 PolynomialFunction f = new PolynomialFunction(coefficients);
99 LaguerreSolver solver = new LaguerreSolver();
100
101 min = -2.0; max = 2.0; expected = -1.0;
102 tolerance = FastMath.max(solver.getAbsoluteAccuracy(),
103 FastMath.abs(expected * solver.getRelativeAccuracy()));
104 result = solver.solve(100, f, min, max);
105 assertEquals(expected, result, tolerance);
106
107 min = -5.0; max = -2.5; expected = -3.0;
108 tolerance = FastMath.max(solver.getAbsoluteAccuracy(),
109 FastMath.abs(expected * solver.getRelativeAccuracy()));
110 result = solver.solve(100, f, min, max);
111 assertEquals(expected, result, tolerance);
112
113 min = 3.0; max = 6.0; expected = 4.0;
114 tolerance = FastMath.max(solver.getAbsoluteAccuracy(),
115 FastMath.abs(expected * solver.getRelativeAccuracy()));
116 result = solver.solve(100, f, min, max);
117 assertEquals(expected, result, tolerance);
118 }
119
120
121
122
123
124 @Test
125 void testQuinticFunction2() {
126
127 final double[] coefficients = { 4.0, 0.0, 1.0, 4.0, 0.0, 1.0 };
128 final LaguerreSolver solver = new LaguerreSolver();
129 final Complex[] result = solver.solveAllComplex(coefficients, 0);
130
131 for (Complex expected : new Complex[] { new Complex(0, -2),
132 new Complex(0, 2),
133 new Complex(0.5, 0.5 * FastMath.sqrt(3)),
134 new Complex(-1, 0),
135 new Complex(0.5, -0.5 * FastMath.sqrt(3.0)) }) {
136 final double tolerance = FastMath.max(solver.getAbsoluteAccuracy(),
137 FastMath.abs(expected.norm() * solver.getRelativeAccuracy()));
138 UnitTestUtils.customAssertContains(result, expected, tolerance);
139 }
140 }
141
142
143
144
145 @Test
146 void testParameters() {
147 double[] coefficients = { -3.0, 5.0, 2.0 };
148 PolynomialFunction f = new PolynomialFunction(coefficients);
149 LaguerreSolver solver = new LaguerreSolver();
150
151 try {
152
153 solver.solve(100, f, 1, -1);
154 fail("Expecting MathIllegalArgumentException - bad interval");
155 } catch (MathIllegalArgumentException ex) {
156
157 }
158 try {
159
160 solver.solve(100, f, 2, 3);
161 fail("Expecting MathIllegalArgumentException - no bracketing");
162 } catch (MathIllegalArgumentException ex) {
163
164 }
165 }
166
167 @Test
168 void testIssue177() {
169 doTestIssue177(new double[] {-100.0, 0.0, 0.0, 0.0, 1.0}, FastMath.sqrt(10.0));
170 doTestIssue177(new double[] { -100.0, 0.0, 0.0, 1.0}, FastMath.cbrt(100.0));
171 doTestIssue177(new double[] { -16.0, 0.0, 0.0, 0.0, 1.0}, 2.0);
172 }
173
174 private void doTestIssue177(final double[] coefficients, final double expected) {
175 Complex[] roots = new LaguerreSolver(1.0e-5).solveAllComplex(coefficients, 0);
176 assertEquals(coefficients.length - 1, roots.length);
177 for (final Complex root : roots) {
178 assertEquals(expected, root.norm(), 1.0e-15);
179 assertEquals(0.0, MathUtils.normalizeAngle(roots.length * root.getArgument(), 0.0), 1.0e-15);
180 }
181 }
182
183 }