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.interpolation;
23
24 import org.hipparchus.analysis.UnivariateFunction;
25 import org.hipparchus.analysis.function.Expm1;
26 import org.hipparchus.analysis.function.Sin;
27 import org.hipparchus.exception.MathIllegalArgumentException;
28 import org.hipparchus.util.FastMath;
29 import org.junit.Assert;
30 import org.junit.Test;
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46 public final class DividedDifferenceInterpolatorTest {
47
48
49
50
51
52
53 @Test
54 public void testSinFunction() {
55 UnivariateFunction f = new Sin();
56 UnivariateInterpolator interpolator = new DividedDifferenceInterpolator();
57 double[] x;
58 double[] y;
59 double z;
60 double expected;
61 double result;
62 double tolerance;
63
64
65 int n = 6;
66 double min = 0.0, max = 2 * FastMath.PI;
67 x = new double[n];
68 y = new double[n];
69 for (int i = 0; i < n; i++) {
70 x[i] = min + i * (max - min) / n;
71 y[i] = f.value(x[i]);
72 }
73 double derivativebound = 1.0;
74 UnivariateFunction p = interpolator.interpolate(x, y);
75
76 z = FastMath.PI / 4; expected = f.value(z); result = p.value(z);
77 tolerance = FastMath.abs(derivativebound * partialerror(x, z));
78 Assert.assertEquals(expected, result, tolerance);
79
80 z = FastMath.PI * 1.5; expected = f.value(z); result = p.value(z);
81 tolerance = FastMath.abs(derivativebound * partialerror(x, z));
82 Assert.assertEquals(expected, result, tolerance);
83 }
84
85
86
87
88
89
90 @Test
91 public void testExpm1Function() {
92 UnivariateFunction f = new Expm1();
93 UnivariateInterpolator interpolator = new DividedDifferenceInterpolator();
94 double[] x;
95 double[] y;
96 double z;
97 double expected;
98 double result;
99 double tolerance;
100
101
102 int n = 5;
103 double min = -1.0, max = 1.0;
104 x = new double[n];
105 y = new double[n];
106 for (int i = 0; i < n; i++) {
107 x[i] = min + i * (max - min) / n;
108 y[i] = f.value(x[i]);
109 }
110 double derivativebound = FastMath.E;
111 UnivariateFunction p = interpolator.interpolate(x, y);
112
113 z = 0.0; expected = f.value(z); result = p.value(z);
114 tolerance = FastMath.abs(derivativebound * partialerror(x, z));
115 Assert.assertEquals(expected, result, tolerance);
116
117 z = 0.5; expected = f.value(z); result = p.value(z);
118 tolerance = FastMath.abs(derivativebound * partialerror(x, z));
119 Assert.assertEquals(expected, result, tolerance);
120
121 z = -0.5; expected = f.value(z); result = p.value(z);
122 tolerance = FastMath.abs(derivativebound * partialerror(x, z));
123 Assert.assertEquals(expected, result, tolerance);
124 }
125
126
127
128
129 @Test
130 public void testParameters() {
131 UnivariateInterpolator interpolator = new DividedDifferenceInterpolator();
132
133 try {
134
135 double[] x = { 1.0, 2.0, 2.0, 4.0 };
136 double[] y = { 0.0, 4.0, 4.0, 2.5 };
137 UnivariateFunction p = interpolator.interpolate(x, y);
138 p.value(0.0);
139 Assert.fail("Expecting MathIllegalArgumentException - bad abscissas array");
140 } catch (MathIllegalArgumentException ex) {
141
142 }
143 }
144
145
146
147
148 protected double partialerror(double[] x, double z) throws
149 IllegalArgumentException {
150
151 if (x.length < 1) {
152 throw new IllegalArgumentException
153 ("Interpolation array cannot be empty.");
154 }
155 double out = 1;
156 for (int i = 0; i < x.length; i++) {
157 out *= (z - x[i]) / (i + 1);
158 }
159 return out;
160 }
161 }