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.LocalizedCoreFormats;
25 import org.hipparchus.exception.MathIllegalArgumentException;
26 import org.hipparchus.exception.MathIllegalStateException;
27 import org.hipparchus.util.Binary64;
28 import org.junit.jupiter.api.Test;
29
30 import static org.junit.jupiter.api.Assertions.assertArrayEquals;
31 import static org.junit.jupiter.api.Assertions.assertEquals;
32 import static org.junit.jupiter.api.Assertions.assertFalse;
33 import static org.junit.jupiter.api.Assertions.assertTrue;
34 import static org.junit.jupiter.api.Assertions.fail;
35
36
37
38
39
40 class PolynomialSplineFunctionTest {
41
42
43 protected double tolerance = 1.0e-12;
44
45
46
47
48
49
50
51
52
53
54
55 protected PolynomialFunction[] polynomials = {
56 new PolynomialFunction(new double[] {0d, 1d, 1d}),
57 new PolynomialFunction(new double[] {2d, 1d, 1d}),
58 new PolynomialFunction(new double[] {4d, 1d, 1d})
59 };
60
61
62 protected double[] knots = {-1, 0, 1, 2};
63
64
65 protected PolynomialFunction dp =
66 new PolynomialFunction(new double[] {1d, 2d});
67
68
69 @Test
70 void testConstructor() {
71 PolynomialSplineFunction spline =
72 new PolynomialSplineFunction(knots, polynomials);
73 assertArrayEquals(knots, spline.getKnots());
74 assertEquals(1d, spline.getPolynomials()[0].getCoefficients()[2], 0);
75 assertEquals(3, spline.getN());
76
77 try {
78 new PolynomialSplineFunction(new double[] {0}, polynomials);
79 fail("Expecting MathIllegalArgumentException");
80 } catch (MathIllegalArgumentException ex) {
81
82 }
83
84 try {
85 new PolynomialSplineFunction(new double[] {0,1,2,3,4}, polynomials);
86 fail("Expecting MathIllegalArgumentException");
87 } catch (MathIllegalArgumentException ex) {
88
89 }
90
91 try {
92 new PolynomialSplineFunction(new double[] {0,1, 3, 2}, polynomials);
93 fail("Expecting MathIllegalArgumentException");
94 } catch (MathIllegalArgumentException ex) {
95
96 }
97 }
98
99 @Test
100 void testValues() {
101 PolynomialSplineFunction spline =
102 new PolynomialSplineFunction(knots, polynomials);
103 PolynomialSplineFunction dSpline = spline.polynomialSplineDerivative();
104
105
106
107
108
109
110 double x = -1;
111 int index = 0;
112 for (int i = 0; i < 10; i++) {
113 x+=0.25;
114 index = findKnot(knots, x);
115 assertEquals(polynomials[index].value(x - knots[index]), spline.value(x), tolerance, "spline function evaluation failed for x=" + x);
116 assertEquals(dp.value(x - knots[index]), dSpline.value(x), tolerance, "spline derivative evaluation failed for x=" + x);
117 }
118
119
120 for (int i = 0; i < 3; i++) {
121 assertEquals(polynomials[i].value(0), spline.value(knots[i]), tolerance, "spline function evaluation failed for knot=" + knots[i]);
122 assertEquals(dp.value(0), dSpline.value(new Binary64(knots[i])).getReal(), tolerance, "spline function evaluation failed for knot=" + knots[i]);
123 }
124
125 try {
126 x = spline.value(-1.5);
127 fail("Expecting MathIllegalArgumentException");
128 } catch (MathIllegalArgumentException ex) {
129
130 }
131
132 try {
133 x = spline.value(2.5);
134 fail("Expecting MathIllegalArgumentException");
135 } catch (MathIllegalArgumentException ex) {
136
137 }
138 }
139
140 @Test
141 void testIsValidPoint() {
142 final PolynomialSplineFunction spline =
143 new PolynomialSplineFunction(knots, polynomials);
144 final double xMin = knots[0];
145 final double xMax = knots[knots.length - 1];
146
147 double x;
148
149 x = xMin;
150 assertTrue(spline.isValidPoint(x));
151
152 spline.value(x);
153
154 x = xMax;
155 assertTrue(spline.isValidPoint(x));
156
157 spline.value(x);
158
159 final double xRange = xMax - xMin;
160 x = xMin + xRange / 3.4;
161 assertTrue(spline.isValidPoint(x));
162
163 spline.value(x);
164
165 final double small = 1e-8;
166 x = xMin - small;
167 assertFalse(spline.isValidPoint(x));
168
169 try {
170 spline.value(x);
171 fail("MathIllegalArgumentException expected");
172 } catch (MathIllegalArgumentException expected) {}
173 }
174
175
176
177
178
179 protected int findKnot(double[] knots, double x) {
180 if (x < knots[0] || x >= knots[knots.length -1]) {
181 throw new MathIllegalArgumentException(LocalizedCoreFormats.OUT_OF_RANGE_SIMPLE,
182 x, knots[0], knots[knots.length -1]);
183 }
184 for (int i = 0; i < knots.length; i++) {
185 if (knots[i] > x) {
186 return i - 1;
187 }
188 }
189 throw new MathIllegalStateException(LocalizedCoreFormats.ILLEGAL_STATE);
190 }
191 }
192