LinearInterpolator implements Interpolator { public InterpolatingFunction interpolate(double[] x, double[] y) { if (x.length != y.length) { throw new IllegalArgumentException(MessageFormat.format( "Dimension mismatch {0} != {1}.", x.length, y.length)); } if (x.length < minNumPoints) { throw new IllegalArgumentException(MessageFormat.format( "{0} points are required, got only {1}.", minNumPoints, x.length)); } int n = x.length - 1; for (int i = 0; i < n; i++) { if (x[i] >= x[i + 1]) { throw new IllegalArgumentException(MessageFormat.format( "Points {0} and {1} are not strictly increasing ({2} >= {3}).", i, i + 1, x[i], x[i + 1])); } } final double[] m = new double[n]; for (int i = 0; i < n; i++) { m[i] = (y[i + 1] - y[i]) / (x[i + 1] - x[i]); } PolynomialFunction[] polynomials = new PolynomialFunction[n]; final double[] coefficients = new double[2]; for (int i = 0; i < n; i++) { coefficients[0] = y[i]; coefficients[1] = m[i]; polynomials[i] = new PolynomialFunction(coefficients); } return new InterpolatingFunction(x, polynomials); }  @Override int getMinNumPoints(); InterpolatingFunction interpolate(double[] x, double[] y);  }
@Test public void testInterpolationOfStraightLine() { double[] x = new double[]{0d, 2d, 4d, 6d, 8d}; double[] y = new double[]{1d, 1d, 1d, 1d, 1d}; final InterpolatingFunction function = linearInterpolator.interpolate(x, y); assertKnownValuesAreAssignedCorrectly(x, y, function); assertUnknownValuesAreGuessedCorrectly(y, function); }
@Test public void testInterpolationOfFunctionWithPositiveAndNegativeSlope() { double[] x = new double[]{0d, 2d, 4d, 6d, 8d}; double[] y = new double[]{1d, 3d, 5d, 3d, 1d}; final InterpolatingFunction function = linearInterpolator.interpolate(x, y); assertKnownValuesAreAssignedCorrectly(x, y, function); assertUnknownValuesAreGuessedCorrectly(y, function); }
@Test public void testInterpolationOfQuadraticFunction() { double[] x = new double[]{0d, 2d, 4d, 6d, 8d}; double[] y = new double[]{Math.pow(0d, 2), Math.pow(2d, 2), Math.pow(4d, 2), Math.pow(6d, 2), Math.pow(8d, 2)}; final InterpolatingFunction function = linearInterpolator.interpolate(x, y); assertKnownValuesAreAssignedCorrectly(x, y, function); assertUnknownValuesAreGuessedCorrectly(y, function); }
@Test public void testInterpolationOfSinusFunction() { double[] x = new double[]{0, 2, 4, 6, 8}; double[] y = new double[]{Math.sin(0d), Math.sin(2d), Math.sin(4d), Math.sin(6d), Math.sin(8d)}; final InterpolatingFunction function = linearInterpolator.interpolate(x, y); assertKnownValuesAreAssignedCorrectly(x, y, function); assertUnknownValuesAreGuessedCorrectly(y, function); }
@Test public void testInterpolationOfRandomValues() { double[] x = new double[]{0d, 4d, 7d, 8d, 10d, 14d, 16d}; double[] y = new double[]{2d, 1d, 8d, 5d, 4d, 1d, 7d}; double[] testValues = new double[]{2d, 5.5, 7.5, 9d, 12d, 15d}; final InterpolatingFunction function = linearInterpolator.interpolate(x, y); assertKnownValuesAreAssignedCorrectly(x, y, function); assertUnknownValuesAreGuessedCorrectly(y, function, testValues); }