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 junit.framework.TestCase;
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36 public final class NevilleInterpolatorTest extends TestCase {
37
38
39
40
41
42
43 public void testSinFunction() throws MathException {
44 UnivariateRealFunction f = new SinFunction();
45 UnivariateRealInterpolator interpolator = new NevilleInterpolator();
46 double x[], y[], z, expected, result, tolerance;
47
48
49 int n = 6;
50 double min = 0.0, max = 2 * Math.PI;
51 x = new double[n];
52 y = new double[n];
53 for (int i = 0; i < n; i++) {
54 x[i] = min + i * (max - min) / n;
55 y[i] = f.value(x[i]);
56 }
57 double derivativebound = 1.0;
58 UnivariateRealFunction p = interpolator.interpolate(x, y);
59
60 z = Math.PI / 4; expected = f.value(z); result = p.value(z);
61 tolerance = Math.abs(derivativebound * partialerror(x, z));
62 assertEquals(expected, result, tolerance);
63
64 z = Math.PI * 1.5; expected = f.value(z); result = p.value(z);
65 tolerance = Math.abs(derivativebound * partialerror(x, z));
66 assertEquals(expected, result, tolerance);
67 }
68
69
70
71
72
73
74 public void testExpm1Function() throws MathException {
75 UnivariateRealFunction f = new Expm1Function();
76 UnivariateRealInterpolator interpolator = new NevilleInterpolator();
77 double x[], y[], z, expected, result, tolerance;
78
79
80 int n = 5;
81 double min = -1.0, max = 1.0;
82 x = new double[n];
83 y = new double[n];
84 for (int i = 0; i < n; i++) {
85 x[i] = min + i * (max - min) / n;
86 y[i] = f.value(x[i]);
87 }
88 double derivativebound = Math.E;
89 UnivariateRealFunction p = interpolator.interpolate(x, y);
90
91 z = 0.0; expected = f.value(z); result = p.value(z);
92 tolerance = Math.abs(derivativebound * partialerror(x, z));
93 assertEquals(expected, result, tolerance);
94
95 z = 0.5; expected = f.value(z); result = p.value(z);
96 tolerance = Math.abs(derivativebound * partialerror(x, z));
97 assertEquals(expected, result, tolerance);
98
99 z = -0.5; expected = f.value(z); result = p.value(z);
100 tolerance = Math.abs(derivativebound * partialerror(x, z));
101 assertEquals(expected, result, tolerance);
102 }
103
104
105
106
107 public void testParameters() throws Exception {
108 UnivariateRealInterpolator interpolator = new NevilleInterpolator();
109
110 try {
111
112 double x[] = { 1.0, 2.0, 2.0, 4.0 };
113 double y[] = { 0.0, 4.0, 4.0, 2.5 };
114 UnivariateRealFunction p = interpolator.interpolate(x, y);
115 p.value(0.0);
116 fail("Expecting MathException - bad abscissas array");
117 } catch (MathException ex) {
118
119 }
120 }
121
122
123
124
125 protected double partialerror(double x[], double z) throws
126 IllegalArgumentException {
127
128 if (x.length < 1) {
129 throw new IllegalArgumentException
130 ("Interpolation array cannot be empty.");
131 }
132 double out = 1;
133 for (int i = 0; i < x.length; i++) {
134 out *= (z - x[i]) / (i + 1);
135 }
136 return out;
137 }
138 }