LinearInterpolator implements UnivariateInterpolator { public PolynomialSplineFunction interpolate(double x[], double y[]) throws DimensionMismatchException, NumberIsTooSmallException, NonMonotonicSequenceException { if (x.length != y.length) { throw new DimensionMismatchException(x.length, y.length); } if (x.length < 2) { throw new NumberIsTooSmallException(LocalizedFormats.NUMBER_OF_POINTS, x.length, 2, true); } int n = x.length - 1; MathArrays.checkOrder(x); 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]); } final 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 PolynomialSplineFunction(x, polynomials); }  PolynomialSplineFunction interpolate(double x[], double y[]);  }
@Test public void testInterpolateLinearDegenerateTwoSegment() { double x[] = { 0.0, 0.5, 1.0 }; double y[] = { 0.0, 0.5, 1.0 }; UnivariateInterpolator i = new LinearInterpolator(); UnivariateFunction f = i.interpolate(x, y); verifyInterpolation(f, x, y); PolynomialFunction polynomials[] = ((PolynomialSplineFunction) f).getPolynomials(); double target[] = {y[0], 1d}; TestUtils.assertEquals(polynomials[0].getCoefficients(), target, coefficientTolerance); target = new double[]{y[1], 1d}; TestUtils.assertEquals(polynomials[1].getCoefficients(), target, coefficientTolerance); Assert.assertEquals(0.0,f.value(0.0), interpolationTolerance); Assert.assertEquals(0.4,f.value(0.4), interpolationTolerance); Assert.assertEquals(1.0,f.value(1.0), interpolationTolerance); }
@Test public void testInterpolateLinearDegenerateThreeSegment() { double x[] = { 0.0, 0.5, 1.0, 1.5 }; double y[] = { 0.0, 0.5, 1.0, 1.5 }; UnivariateInterpolator i = new LinearInterpolator(); UnivariateFunction f = i.interpolate(x, y); verifyInterpolation(f, x, y); PolynomialFunction polynomials[] = ((PolynomialSplineFunction) f).getPolynomials(); double target[] = {y[0], 1d}; TestUtils.assertEquals(polynomials[0].getCoefficients(), target, coefficientTolerance); target = new double[]{y[1], 1d}; TestUtils.assertEquals(polynomials[1].getCoefficients(), target, coefficientTolerance); target = new double[]{y[2], 1d}; TestUtils.assertEquals(polynomials[2].getCoefficients(), target, coefficientTolerance); Assert.assertEquals(0,f.value(0), interpolationTolerance); Assert.assertEquals(1.4,f.value(1.4), interpolationTolerance); Assert.assertEquals(1.5,f.value(1.5), interpolationTolerance); }
@Test public void testInterpolateLinear() { double x[] = { 0.0, 0.5, 1.0 }; double y[] = { 0.0, 0.5, 0.0 }; UnivariateInterpolator i = new LinearInterpolator(); UnivariateFunction f = i.interpolate(x, y); verifyInterpolation(f, x, y); PolynomialFunction polynomials[] = ((PolynomialSplineFunction) f).getPolynomials(); double target[] = {y[0], 1d}; TestUtils.assertEquals(polynomials[0].getCoefficients(), target, coefficientTolerance); target = new double[]{y[1], -1d}; TestUtils.assertEquals(polynomials[1].getCoefficients(), target, coefficientTolerance); }
@Test public void testIllegalArguments() { UnivariateInterpolator i = new LinearInterpolator(); try { double xval[] = { 0.0, 1.0 }; double yval[] = { 0.0, 1.0, 2.0 }; i.interpolate(xval, yval); Assert.fail("Failed to detect data set array with different sizes."); } catch (DimensionMismatchException iae) { } try { double xval[] = { 0.0, 1.0, 0.5 }; double yval[] = { 0.0, 1.0, 2.0 }; i.interpolate(xval, yval); Assert.fail("Failed to detect unsorted arguments."); } catch (NonMonotonicSequenceException iae) { } try { double xval[] = { 0.0 }; double yval[] = { 0.0 }; i.interpolate(xval, yval); Assert.fail("Failed to detect unsorted arguments."); } catch (NumberIsTooSmallException iae) { } }