1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.math.analysis;
18
19 import org.apache.commons.math.MathException;
20 import org.apache.commons.math.TestUtils;
21
22 import junit.framework.Test;
23 import junit.framework.TestCase;
24 import junit.framework.TestSuite;
25
26
27
28
29
30
31 public class SplineInterpolatorTest extends TestCase {
32
33
34 protected double knotTolerance = 1E-12;
35
36
37 protected double coefficientTolerance = 1E-6;
38
39
40 protected double interpolationTolerance = 1E-2;
41
42 public SplineInterpolatorTest(String name) {
43 super(name);
44 }
45
46 public static Test suite() {
47 TestSuite suite = new TestSuite(SplineInterpolatorTest.class);
48 suite.setName("UnivariateRealInterpolator Tests");
49 return suite;
50 }
51
52 public void testInterpolateLinearDegenerateTwoSegment()
53 throws Exception {
54 double x[] = { 0.0, 0.5, 1.0 };
55 double y[] = { 0.0, 0.5, 1.0 };
56 UnivariateRealInterpolator i = new SplineInterpolator();
57 UnivariateRealFunction f = i.interpolate(x, y);
58 verifyInterpolation(f, x, y);
59 verifyConsistency((PolynomialSplineFunction) f, x);
60
61
62 PolynomialFunction polynomials[] = ((PolynomialSplineFunction) f).getPolynomials();
63 double target[] = {y[0], 1d, 0d, 0d};
64 TestUtils.assertEquals(polynomials[0].getCoefficients(), target, coefficientTolerance);
65 target = new double[]{y[1], 1d, 0d, 0d};
66 TestUtils.assertEquals(polynomials[1].getCoefficients(), target, coefficientTolerance);
67
68
69 assertEquals(0.0,f.value(0.0), interpolationTolerance);
70 assertEquals(0.4,f.value(0.4), interpolationTolerance);
71 assertEquals(1.0,f.value(1.0), interpolationTolerance);
72 }
73
74 public void testInterpolateLinearDegenerateThreeSegment()
75 throws Exception {
76 double x[] = { 0.0, 0.5, 1.0, 1.5 };
77 double y[] = { 0.0, 0.5, 1.0, 1.5 };
78 UnivariateRealInterpolator i = new SplineInterpolator();
79 UnivariateRealFunction f = i.interpolate(x, y);
80 verifyInterpolation(f, x, y);
81
82
83 PolynomialFunction polynomials[] = ((PolynomialSplineFunction) f).getPolynomials();
84 double target[] = {y[0], 1d, 0d, 0d};
85 TestUtils.assertEquals(polynomials[0].getCoefficients(), target, coefficientTolerance);
86 target = new double[]{y[1], 1d, 0d, 0d};
87 TestUtils.assertEquals(polynomials[1].getCoefficients(), target, coefficientTolerance);
88 target = new double[]{y[2], 1d, 0d, 0d};
89 TestUtils.assertEquals(polynomials[2].getCoefficients(), target, coefficientTolerance);
90
91
92 assertEquals(0,f.value(0), interpolationTolerance);
93 assertEquals(1.4,f.value(1.4), interpolationTolerance);
94 assertEquals(1.5,f.value(1.5), interpolationTolerance);
95 }
96
97 public void testInterpolateLinear() throws Exception {
98 double x[] = { 0.0, 0.5, 1.0 };
99 double y[] = { 0.0, 0.5, 0.0 };
100 UnivariateRealInterpolator i = new SplineInterpolator();
101 UnivariateRealFunction f = i.interpolate(x, y);
102 verifyInterpolation(f, x, y);
103 verifyConsistency((PolynomialSplineFunction) f, x);
104
105
106 PolynomialFunction polynomials[] = ((PolynomialSplineFunction) f).getPolynomials();
107 double target[] = {y[0], 1.5d, 0d, -2d};
108 TestUtils.assertEquals(polynomials[0].getCoefficients(), target, coefficientTolerance);
109 target = new double[]{y[1], 0d, -3d, 2d};
110 TestUtils.assertEquals(polynomials[1].getCoefficients(), target, coefficientTolerance);
111 }
112
113 public void testInterpolateSin() throws Exception {
114 double x[] =
115 {
116 0.0,
117 Math.PI / 6d,
118 Math.PI / 2d,
119 5d * Math.PI / 6d,
120 Math.PI,
121 7d * Math.PI / 6d,
122 3d * Math.PI / 2d,
123 11d * Math.PI / 6d,
124 2.d * Math.PI };
125 double y[] = { 0d, 0.5d, 1d, 0.5d, 0d, -0.5d, -1d, -0.5d, 0d };
126 UnivariateRealInterpolator i = new SplineInterpolator();
127 UnivariateRealFunction f = i.interpolate(x, y);
128 verifyInterpolation(f, x, y);
129 verifyConsistency((PolynomialSplineFunction) f, x);
130
131
132
133
134
135
136
137
138
139
140 PolynomialFunction polynomials[] = ((PolynomialSplineFunction) f).getPolynomials();
141 double target[] = {y[0], 1.002676d, 0d, -0.17415829d};
142 TestUtils.assertEquals(polynomials[0].getCoefficients(), target, coefficientTolerance);
143 target = new double[]{y[1], 8.594367e-01, -2.735672e-01, -0.08707914};
144 TestUtils.assertEquals(polynomials[1].getCoefficients(), target, coefficientTolerance);
145 target = new double[]{y[2], 1.471804e-17,-5.471344e-01, 0.08707914};
146 TestUtils.assertEquals(polynomials[2].getCoefficients(), target, coefficientTolerance);
147 target = new double[]{y[3], -8.594367e-01, -2.735672e-01, 0.17415829};
148 TestUtils.assertEquals(polynomials[3].getCoefficients(), target, coefficientTolerance);
149 target = new double[]{y[4], -1.002676, 6.548562e-17, 0.17415829};
150 TestUtils.assertEquals(polynomials[4].getCoefficients(), target, coefficientTolerance);
151 target = new double[]{y[5], -8.594367e-01, 2.735672e-01, 0.08707914};
152 TestUtils.assertEquals(polynomials[5].getCoefficients(), target, coefficientTolerance);
153 target = new double[]{y[6], 3.466465e-16, 5.471344e-01, -0.08707914};
154 TestUtils.assertEquals(polynomials[6].getCoefficients(), target, coefficientTolerance);
155 target = new double[]{y[7], 8.594367e-01, 2.735672e-01, -0.17415829};
156 TestUtils.assertEquals(polynomials[7].getCoefficients(), target, coefficientTolerance);
157
158
159 assertEquals(Math.sqrt(2d) / 2d,f.value(Math.PI/4d),interpolationTolerance);
160 assertEquals(Math.sqrt(2d) / 2d,f.value(3d*Math.PI/4d),interpolationTolerance);
161 }
162
163
164 public void testIllegalArguments() throws MathException {
165
166 UnivariateRealInterpolator i = new SplineInterpolator();
167 try {
168 double xval[] = { 0.0, 1.0 };
169 double yval[] = { 0.0, 1.0, 2.0 };
170 i.interpolate(xval, yval);
171 fail("Failed to detect data set array with different sizes.");
172 } catch (IllegalArgumentException iae) {
173 }
174
175 try {
176 double xval[] = { 0.0, 1.0, 0.5 };
177 double yval[] = { 0.0, 1.0, 2.0 };
178 i.interpolate(xval, yval);
179 fail("Failed to detect unsorted arguments.");
180 } catch (IllegalArgumentException iae) {
181 }
182 }
183
184
185
186
187 protected void verifyInterpolation(UnivariateRealFunction f, double x[], double y[])
188 throws Exception{
189 for (int i = 0; i < x.length; i++) {
190 assertEquals(f.value(x[i]), y[i], knotTolerance);
191 }
192 }
193
194
195
196
197
198 protected void verifyConsistency(PolynomialSplineFunction f, double x[])
199 throws Exception {
200 PolynomialFunction polynomials[] = f.getPolynomials();
201 for (int i = 1; i < x.length - 2; i++) {
202
203 assertEquals(polynomials[i].value(x[i +1] - x[i]), polynomials[i + 1].value(0), 0.1);
204 assertEquals(polynomials[i].derivative().value(x[i +1] - x[i]),
205 polynomials[i + 1].derivative().value(0), 0.5);
206 assertEquals(polynomials[i].polynomialDerivative().derivative().value(x[i +1] - x[i]),
207 polynomials[i + 1].polynomialDerivative().derivative().value(0), 0.5);
208 }
209 }
210
211 }