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.polynomials;
23
24 import org.hipparchus.analysis.differentiation.DSFactory;
25 import org.hipparchus.analysis.differentiation.DerivativeStructure;
26 import org.hipparchus.exception.MathIllegalArgumentException;
27 import org.hipparchus.util.Binary64;
28 import org.junit.Assert;
29 import org.junit.Test;
30
31
32
33
34
35
36
37
38 public final class PolynomialFunctionNewtonFormTest {
39
40
41
42
43 @Test
44 public void testLinearFunction() {
45 PolynomialFunctionNewtonForm p;
46 double coefficients[], z, expected, result, tolerance = 1E-12;
47
48
49 double a[] = { 2.0, 1.5 };
50 double c[] = { 4.0 };
51 p = new PolynomialFunctionNewtonForm(a, c);
52
53 z = 2.0; expected = -1.0; result = p.value(z);
54 Assert.assertEquals(expected, result, tolerance);
55
56 z = 4.5; expected = 2.75; result = p.value(z);
57 Assert.assertEquals(expected, result, tolerance);
58
59 z = 6.0; expected = 5.0; result = p.value(new Binary64(z)).getReal();
60 Assert.assertEquals(expected, result, tolerance);
61
62 Assert.assertEquals(1, p.degree());
63
64 coefficients = p.getCoefficients();
65 Assert.assertEquals(2, coefficients.length);
66 Assert.assertEquals(-4.0, coefficients[0], tolerance);
67 Assert.assertEquals(1.5, coefficients[1], tolerance);
68 }
69
70
71
72
73 @Test
74 public void testQuadraticFunction() {
75 PolynomialFunctionNewtonForm p;
76 double coefficients[], z, expected, result, tolerance = 1E-12;
77
78
79 double a[] = { 4.0, 3.0, 2.0 };
80 double c[] = { 1.0, -2.0 };
81 p = new PolynomialFunctionNewtonForm(a, c);
82
83 z = 1.0; expected = 4.0; result = p.value(z);
84 Assert.assertEquals(expected, result, tolerance);
85
86 z = 2.5; expected = 22.0; result = p.value(z);
87 Assert.assertEquals(expected, result, tolerance);
88
89 z = -2.0; expected = -5.0; result = p.value(z);
90 Assert.assertEquals(expected, result, tolerance);
91
92 Assert.assertEquals(2, p.degree());
93
94 coefficients = p.getCoefficients();
95 Assert.assertEquals(3, coefficients.length);
96 Assert.assertEquals(-3.0, coefficients[0], tolerance);
97 Assert.assertEquals(5.0, coefficients[1], tolerance);
98 Assert.assertEquals(2.0, coefficients[2], tolerance);
99 }
100
101
102
103
104 @Test
105 public void testQuinticFunction() {
106 PolynomialFunctionNewtonForm p;
107 double coefficients[], z, expected, result, tolerance = 1E-12;
108
109
110
111 double a[] = { 0.0, 6.0, -6.0, -6.0, 1.0, 1.0 };
112 double c[] = { 0.0, 0.0, 1.0, -1.0, 2.0 };
113 p = new PolynomialFunctionNewtonForm(a, c);
114
115 z = 0.0; expected = 0.0; result = p.value(z);
116 Assert.assertEquals(expected, result, tolerance);
117
118 z = -2.0; expected = 0.0; result = p.value(z);
119 Assert.assertEquals(expected, result, tolerance);
120
121 z = 4.0; expected = 360.0; result = p.value(z);
122 Assert.assertEquals(expected, result, tolerance);
123
124 Assert.assertEquals(5, p.degree());
125
126 coefficients = p.getCoefficients();
127 Assert.assertEquals(6, coefficients.length);
128 Assert.assertEquals(0.0, coefficients[0], tolerance);
129 Assert.assertEquals(6.0, coefficients[1], tolerance);
130 Assert.assertEquals(1.0, coefficients[2], tolerance);
131 Assert.assertEquals(-7.0, coefficients[3], tolerance);
132 Assert.assertEquals(-1.0, coefficients[4], tolerance);
133 Assert.assertEquals(1.0, coefficients[5], tolerance);
134 }
135
136
137
138
139 @Test
140 public void testDerivative() {
141
142
143 PolynomialFunctionNewtonForm p =
144 new PolynomialFunctionNewtonForm(new double[] { 0, 1, 3, 1 },
145 new double[] { 0, 1, 2 });
146
147 double eps = 2.0e-14;
148 DSFactory factory = new DSFactory(1, 4);
149 for (double t = 0.0; t < 10.0; t += 0.1) {
150 DerivativeStructure x = factory.variable(0, t);
151 DerivativeStructure y = p.value(x);
152 Assert.assertEquals(t * t * t, y.getValue(), eps * t * t * t);
153 Assert.assertEquals(3.0 * t * t, y.getPartialDerivative(1), eps * 3.0 * t * t);
154 Assert.assertEquals(6.0 * t, y.getPartialDerivative(2), eps * 6.0 * t);
155 Assert.assertEquals(6.0, y.getPartialDerivative(3), eps * 6.0);
156 Assert.assertEquals(0.0, y.getPartialDerivative(4), eps);
157 }
158
159 }
160
161
162
163
164 @Test
165 public void testParameters() {
166
167 try {
168
169 double a[] = { 1.0 };
170 double c[] = { 2.0 };
171 new PolynomialFunctionNewtonForm(a, c);
172 Assert.fail("Expecting MathIllegalArgumentException - bad input array length");
173 } catch (MathIllegalArgumentException ex) {
174
175 }
176 try {
177
178 double a[] = { 1.0, 2.0, 3.0, 4.0 };
179 double c[] = { 4.0, 3.0, 2.0, 1.0 };
180 new PolynomialFunctionNewtonForm(a, c);
181 Assert.fail("Expecting MathIllegalArgumentException - mismatch input arrays");
182 } catch (MathIllegalArgumentException ex) {
183
184 }
185 }
186 }