SplineInterpolator 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 < 3) { throw new NumberIsTooSmallException(LocalizedFormats.NUMBER_OF_POINTS, x.length, 3, true); } final int n = x.length - 1; MathArrays.checkOrder(x); final double h[] = new double[n]; for (int i = 0; i < n; i++) { h[i] = x[i + 1] - x[i]; } final double mu[] = new double[n]; final double z[] = new double[n + 1]; mu[0] = 0d; z[0] = 0d; double g = 0; for (int i = 1; i < n; i++) { g = 2d * (x[i+1] - x[i - 1]) - h[i - 1] * mu[i -1]; mu[i] = h[i] / g; z[i] = (3d * (y[i + 1] * h[i - 1] - y[i] * (x[i + 1] - x[i - 1])+ y[i - 1] * h[i]) / (h[i - 1] * h[i]) - h[i - 1] * z[i - 1]) / g; } final double b[] = new double[n]; final double c[] = new double[n + 1]; final double d[] = new double[n]; z[n] = 0d; c[n] = 0d; for (int j = n -1; j >=0; j--) { c[j] = z[j] - mu[j] * c[j + 1]; b[j] = (y[j + 1] - y[j]) / h[j] - h[j] * (c[j + 1] + 2d * c[j]) / 3d; d[j] = (c[j + 1] - c[j]) / (3d * h[j]); } final PolynomialFunction polynomials[] = new PolynomialFunction[n]; final double coefficients[] = new double[4]; for (int i = 0; i < n; i++) { coefficients[0] = y[i]; coefficients[1] = b[i]; coefficients[2] = c[i]; coefficients[3] = d[i]; polynomials[i] = new PolynomialFunction(coefficients); } return new PolynomialSplineFunction(x, polynomials); }  PolynomialSplineFunction interpolate(double x[], double y[]);  }
@Test public void testInterpolateLinearDegenerateTwoSegment() { double tolerance = 1e-15; double x[] = { 0.0, 0.5, 1.0 }; double y[] = { 0.0, 0.5, 1.0 }; UnivariateInterpolator i = new SplineInterpolator(); UnivariateFunction f = i.interpolate(x, y); verifyInterpolation(f, x, y); verifyConsistency((PolynomialSplineFunction) f, x); 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), tolerance); Assert.assertEquals(0.4,f.value(0.4), tolerance); Assert.assertEquals(1.0,f.value(1.0), tolerance); }
@Test public void testInterpolateLinearDegenerateThreeSegment() { double tolerance = 1e-15; double x[] = { 0.0, 0.5, 1.0, 1.5 }; double y[] = { 0.0, 0.5, 1.0, 1.5 }; UnivariateInterpolator i = new SplineInterpolator(); 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), tolerance); Assert.assertEquals(1.4,f.value(1.4), tolerance); Assert.assertEquals(1.5,f.value(1.5), tolerance); }
@Test public void testInterpolateLinear() { double x[] = { 0.0, 0.5, 1.0 }; double y[] = { 0.0, 0.5, 0.0 }; UnivariateInterpolator i = new SplineInterpolator(); UnivariateFunction f = i.interpolate(x, y); verifyInterpolation(f, x, y); verifyConsistency((PolynomialSplineFunction) f, x); PolynomialFunction polynomials[] = ((PolynomialSplineFunction) f).getPolynomials(); double target[] = {y[0], 1.5d, 0d, -2d}; TestUtils.assertEquals(polynomials[0].getCoefficients(), target, coefficientTolerance); target = new double[]{y[1], 0d, -3d, 2d}; TestUtils.assertEquals(polynomials[1].getCoefficients(), target, coefficientTolerance); }
@Test public void testInterpolateSin() { double sineCoefficientTolerance = 1e-6; double sineInterpolationTolerance = 0.0043; double x[] = { 0.0, FastMath.PI / 6d, FastMath.PI / 2d, 5d * FastMath.PI / 6d, FastMath.PI, 7d * FastMath.PI / 6d, 3d * FastMath.PI / 2d, 11d * FastMath.PI / 6d, 2.d * FastMath.PI }; double y[] = { 0d, 0.5d, 1d, 0.5d, 0d, -0.5d, -1d, -0.5d, 0d }; UnivariateInterpolator i = new SplineInterpolator(); UnivariateFunction f = i.interpolate(x, y); verifyInterpolation(f, x, y); verifyConsistency((PolynomialSplineFunction) f, x); PolynomialFunction polynomials[] = ((PolynomialSplineFunction) f).getPolynomials(); double target[] = {y[0], 1.002676d, 0d, -0.17415829d}; TestUtils.assertEquals(polynomials[0].getCoefficients(), target, sineCoefficientTolerance); target = new double[]{y[1], 8.594367e-01, -2.735672e-01, -0.08707914}; TestUtils.assertEquals(polynomials[1].getCoefficients(), target, sineCoefficientTolerance); target = new double[]{y[2], 1.471804e-17,-5.471344e-01, 0.08707914}; TestUtils.assertEquals(polynomials[2].getCoefficients(), target, sineCoefficientTolerance); target = new double[]{y[3], -8.594367e-01, -2.735672e-01, 0.17415829}; TestUtils.assertEquals(polynomials[3].getCoefficients(), target, sineCoefficientTolerance); target = new double[]{y[4], -1.002676, 6.548562e-17, 0.17415829}; TestUtils.assertEquals(polynomials[4].getCoefficients(), target, sineCoefficientTolerance); target = new double[]{y[5], -8.594367e-01, 2.735672e-01, 0.08707914}; TestUtils.assertEquals(polynomials[5].getCoefficients(), target, sineCoefficientTolerance); target = new double[]{y[6], 3.466465e-16, 5.471344e-01, -0.08707914}; TestUtils.assertEquals(polynomials[6].getCoefficients(), target, sineCoefficientTolerance); target = new double[]{y[7], 8.594367e-01, 2.735672e-01, -0.17415829}; TestUtils.assertEquals(polynomials[7].getCoefficients(), target, sineCoefficientTolerance); Assert.assertEquals(FastMath.sqrt(2d) / 2d,f.value(FastMath.PI/4d),sineInterpolationTolerance); Assert.assertEquals(FastMath.sqrt(2d) / 2d,f.value(3d*FastMath.PI/4d),sineInterpolationTolerance); }
@Test public void testIllegalArguments() { UnivariateInterpolator i = new SplineInterpolator(); 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, 1.0 }; double yval[] = { 0.0, 1.0 }; i.interpolate(xval, yval); Assert.fail("Failed to detect unsorted arguments."); } catch (NumberIsTooSmallException iae) { } }