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