NaturalCubicSplineInterpolator1D extends Interpolator1D { @Override public Double interpolate(final Interpolator1DDataBundle data, final Double value) { Validate.notNull(value, "value"); Validate.notNull(data, "data bundle"); Validate.isTrue(data instanceof Interpolator1DCubicSplineDataBundle); Interpolator1DCubicSplineDataBundle splineData = (Interpolator1DCubicSplineDataBundle) data; final int low = data.getLowerBoundIndex(value); final int high = low + 1; final int n = data.size() - 1; final double[] xData = data.getKeys(); final double[] yData = data.getValues(); if (data.getLowerBoundIndex(value) == n) { return yData[n]; } final double delta = xData[high] - xData[low]; if (Math.abs(delta) < _eps) { throw new MathException("x data points were not distinct"); } final double a = (xData[high] - value) / delta; final double b = (value - xData[low]) / delta; final double[] y2 = splineData.getSecondDerivatives(); return a * yData[low] + b * yData[high] + (a * (a * a - 1) * y2[low] + b * (b * b - 1) * y2[high]) * delta * delta / 6.; } NaturalCubicSplineInterpolator1D();  NaturalCubicSplineInterpolator1D(final double eps); @Override Double interpolate(final Interpolator1DDataBundle data, final Double value); @Override double firstDerivative(final Interpolator1DDataBundle data, final Double value); @Override double[] getNodeSensitivitiesForValue(final Interpolator1DDataBundle data, final Double value); @Override Interpolator1DDataBundle getDataBundle(final double[] x, final double[] y); @Override Interpolator1DDataBundle getDataBundleFromSortedArrays(final double[] x, final double[] y);  }
@Test(expectedExceptions = IllegalArgumentException.class) public void nullInputMap() { INTERPOLATOR.interpolate((Interpolator1DCubicSplineDataBundle) null, 3.); }
@Test(expectedExceptions = IllegalArgumentException.class) public void nullInterpolateValue() { INTERPOLATOR.interpolate(MODEL, null); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testHighValue() { INTERPOLATOR.interpolate(MODEL, 15.); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testLowValue() { INTERPOLATOR.interpolate(MODEL, -12.); }
@Test public void test() { for (int i = 0; i < 100; i++) { final double x = RANDOM.nextDouble(); assertEquals(CUBIC.evaluate(x), INTERPOLATOR.interpolate(MODEL, x), EPS); } }