1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.math.analysis.polynomials;
18
19 import java.util.Arrays;
20 import junit.framework.TestCase;
21
22 import org.apache.commons.math.FunctionEvaluationException;
23 import org.apache.commons.math.analysis.UnivariateRealFunction;
24
25
26
27
28
29
30 public class PolynomialSplineFunctionTest extends TestCase {
31
32
33 protected double tolerance = 1.0e-12;
34
35
36
37
38
39
40
41
42
43
44
45 protected PolynomialFunction[] polynomials = {
46 new PolynomialFunction(new double[] {0d, 1d, 1d}),
47 new PolynomialFunction(new double[] {2d, 1d, 1d}),
48 new PolynomialFunction(new double[] {4d, 1d, 1d})
49 };
50
51
52 protected double[] knots = {-1, 0, 1, 2};
53
54
55 protected PolynomialFunction dp =
56 new PolynomialFunction(new double[] {1d, 2d});
57
58
59 public void testConstructor() {
60 PolynomialSplineFunction spline =
61 new PolynomialSplineFunction(knots, polynomials);
62 assertTrue(Arrays.equals(knots, spline.getKnots()));
63 assertEquals(1d, spline.getPolynomials()[0].getCoefficients()[2], 0);
64 assertEquals(3, spline.getN());
65
66 try {
67 new PolynomialSplineFunction(new double[] {0}, polynomials);
68 fail("Expecting IllegalArgumentException");
69 } catch (IllegalArgumentException ex) {
70
71 }
72
73 try {
74 new PolynomialSplineFunction(new double[] {0,1,2,3,4}, polynomials);
75 fail("Expecting IllegalArgumentException");
76 } catch (IllegalArgumentException ex) {
77
78 }
79
80 try {
81 new PolynomialSplineFunction(new double[] {0,1, 3, 2}, polynomials);
82 fail("Expecting IllegalArgumentException");
83 } catch (IllegalArgumentException ex) {
84
85 }
86 }
87
88 public void testValues() throws Exception {
89 PolynomialSplineFunction spline =
90 new PolynomialSplineFunction(knots, polynomials);
91 UnivariateRealFunction dSpline = spline.derivative();
92
93
94
95
96
97
98 double x = -1;
99 int index = 0;
100 for (int i = 0; i < 10; i++) {
101 x+=0.25;
102 index = findKnot(knots, x);
103 assertEquals("spline function evaluation failed for x=" + x,
104 polynomials[index].value(x - knots[index]), spline.value(x), tolerance);
105 assertEquals("spline derivative evaluation failed for x=" + x,
106 dp.value(x - knots[index]), dSpline.value(x), tolerance);
107 }
108
109
110 for (int i = 0; i < 3; i++) {
111 assertEquals("spline function evaluation failed for knot=" + knots[i],
112 polynomials[i].value(0), spline.value(knots[i]), tolerance);
113 assertEquals("spline function evaluation failed for knot=" + knots[i],
114 dp.value(0), dSpline.value(knots[i]), tolerance);
115 }
116
117 try {
118 x = spline.value(-1.5);
119 fail("Expecting IllegalArgumentException");
120 } catch (FunctionEvaluationException ex) {
121
122 }
123
124 try {
125 x = spline.value(2.5);
126 fail("Expecting IllegalArgumentException");
127 } catch (FunctionEvaluationException ex) {
128
129 }
130 }
131
132
133
134
135
136 protected int findKnot(double[] knots, double x) {
137 if (x < knots[0] || x >= knots[knots.length -1]) {
138 throw new IllegalArgumentException("x is out of range");
139 }
140 for (int i = 0; i < knots.length; i++) {
141 if (knots[i] > x) {
142 return i -1;
143 }
144 }
145 throw new IllegalArgumentException("x is out of range");
146 }
147 }
148