DividedDifferenceInterpolator implements UnivariateInterpolator, Serializable { public PolynomialFunctionNewtonForm interpolate(double x[], double y[]) throws DimensionMismatchException, NumberIsTooSmallException, NonMonotonicSequenceException { PolynomialFunctionLagrangeForm.verifyInterpolationArray(x, y, true); final double[] c = new double[x.length-1]; System.arraycopy(x, 0, c, 0, c.length); final double[] a = computeDividedDifference(x, y); return new PolynomialFunctionNewtonForm(a, c); }  PolynomialFunctionNewtonForm interpolate(double x[], double y[]);  }
@Test public void testSinFunction() { UnivariateFunction f = new Sin(); UnivariateInterpolator interpolator = new DividedDifferenceInterpolator(); double x[], y[], z, expected, result, tolerance; int n = 6; double min = 0.0, max = 2 * FastMath.PI; x = new double[n]; y = new double[n]; for (int i = 0; i < n; i++) { x[i] = min + i * (max - min) / n; y[i] = f.value(x[i]); } double derivativebound = 1.0; UnivariateFunction p = interpolator.interpolate(x, y); z = FastMath.PI / 4; expected = f.value(z); result = p.value(z); tolerance = FastMath.abs(derivativebound * partialerror(x, z)); Assert.assertEquals(expected, result, tolerance); z = FastMath.PI * 1.5; expected = f.value(z); result = p.value(z); tolerance = FastMath.abs(derivativebound * partialerror(x, z)); Assert.assertEquals(expected, result, tolerance); }
@Test public void testExpm1Function() { UnivariateFunction f = new Expm1(); UnivariateInterpolator interpolator = new DividedDifferenceInterpolator(); double x[], y[], z, expected, result, tolerance; int n = 5; double min = -1.0, max = 1.0; x = new double[n]; y = new double[n]; for (int i = 0; i < n; i++) { x[i] = min + i * (max - min) / n; y[i] = f.value(x[i]); } double derivativebound = FastMath.E; UnivariateFunction p = interpolator.interpolate(x, y); z = 0.0; expected = f.value(z); result = p.value(z); tolerance = FastMath.abs(derivativebound * partialerror(x, z)); Assert.assertEquals(expected, result, tolerance); z = 0.5; expected = f.value(z); result = p.value(z); tolerance = FastMath.abs(derivativebound * partialerror(x, z)); Assert.assertEquals(expected, result, tolerance); z = -0.5; expected = f.value(z); result = p.value(z); tolerance = FastMath.abs(derivativebound * partialerror(x, z)); Assert.assertEquals(expected, result, tolerance); }
@Test public void testParameters() { UnivariateInterpolator interpolator = new DividedDifferenceInterpolator(); try { double x[] = { 1.0, 2.0, 2.0, 4.0 }; double y[] = { 0.0, 4.0, 4.0, 2.5 }; UnivariateFunction p = interpolator.interpolate(x, y); p.value(0.0); Assert.fail("Expecting NonMonotonicSequenceException - bad abscissas array"); } catch (NonMonotonicSequenceException ex) { } }