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