TimeSquareInterpolator1D extends Interpolator1D { @Override public Double interpolate(final Interpolator1DDataBundle data, final Double value) { ArgumentChecker.notNull(value, "Value to be interpolated must not be null"); ArgumentChecker.isTrue(value > 0, "Value should be stricly positive"); ArgumentChecker.notNull(data, "Data bundle must not be null"); final InterpolationBoundedValues boundedValues = data.getBoundedValues(value); final double x1 = boundedValues.getLowerBoundKey(); final double y1 = boundedValues.getLowerBoundValue(); if (boundedValues.getLowerBoundIndex() == data.size() - 1) { return y1; } final double x2 = boundedValues.getHigherBoundKey(); final double y2 = boundedValues.getHigherBoundValue(); final double w = (x2 - value) / (x2 - x1); final double xy21 = x1 * y1 * y1; final double xy22 = x2 * y2 * y2; final double xy2 = w * xy21 + (1 - w) * xy22; return Math.sqrt(xy2 / value); }  @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 nullDataBundle() { INTERPOLATOR.interpolate(null, 2.3); }
@Test(expectedExceptions = IllegalArgumentException.class) public void nullValue() { INTERPOLATOR.interpolate(DATA, null); }
@Test(expectedExceptions = IllegalArgumentException.class) public void lowValue() { INTERPOLATOR.interpolate(DATA, -4.); }
@Test(expectedExceptions = IllegalArgumentException.class) public void highValue() { INTERPOLATOR.interpolate(DATA, 10.0); }
@Test public void interpolation() { final Interpolator1D interpolatorFromFactory = Interpolator1DFactory.TIME_SQUARE_INSTANCE; double[] x = new double[] {1.0, 1.5, 2, 2.75}; for (int loopx = 0; loopx < x.length; loopx++) { double yCalculated = INTERPOLATOR.interpolate(DATA, x[loopx]); double yCalculatedFactory = interpolatorFromFactory.interpolate(DATA, x[loopx]); final int index = DATA.getLowerBoundIndex(x[loopx]); final double weight = (DATA.getKeys()[index + 1] - x[loopx]) / (DATA.getKeys()[index + 1] - DATA.getKeys()[index]); final double val1 = DATA.getKeys()[index] * DATA.getValues()[index] * DATA.getValues()[index]; final double val2 = DATA.getKeys()[index + 1] * DATA.getValues()[index + 1] * DATA.getValues()[index + 1]; final double yExpected = Math.sqrt((weight * val1 + (1.0 - weight) * val2) / x[loopx]); assertEquals("TimeSquare interpolator: data point " + loopx, yExpected, yCalculated, TOLERANCE_Y); assertEquals("TimeSquare interpolator: data point " + loopx, yCalculated, yCalculatedFactory, TOLERANCE_Y); } int lenghtx = DATA.getKeys().length; double lastx = DATA.getKeys()[lenghtx - 1]; double yCalculated = INTERPOLATOR.interpolate(DATA, lastx); final double yExpected = DATA.getValues()[lenghtx - 1]; assertEquals("TimeSquare interpolator: last point", yExpected, yCalculated, TOLERANCE_Y); }