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