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