AkimaSplineInterpolator implements UnivariateInterpolator { public PolynomialSplineFunction interpolate(double[] xvals, double[] yvals) throws DimensionMismatchException, NumberIsTooSmallException, NonMonotonicSequenceException { if (xvals == null || yvals == null) { throw new NullArgumentException(); } if (xvals.length != yvals.length) { throw new DimensionMismatchException(xvals.length, yvals.length); } if (xvals.length < MINIMUM_NUMBER_POINTS) { throw new NumberIsTooSmallException(LocalizedFormats.NUMBER_OF_POINTS, xvals.length, MINIMUM_NUMBER_POINTS, true); } MathArrays.checkOrder(xvals); final int numberOfDiffAndWeightElements = xvals.length - 1; final double[] differences = new double[numberOfDiffAndWeightElements]; final double[] weights = new double[numberOfDiffAndWeightElements]; for (int i = 0; i < differences.length; i++) { differences[i] = (yvals[i + 1] - yvals[i]) / (xvals[i + 1] - xvals[i]); } for (int i = 1; i < weights.length; i++) { weights[i] = FastMath.abs(differences[i] - differences[i - 1]); } final double[] firstDerivatives = new double[xvals.length]; for (int i = 2; i < firstDerivatives.length - 2; i++) { final double wP = weights[i + 1]; final double wM = weights[i - 1]; if (Precision.equals(wP, 0.0) && Precision.equals(wM, 0.0)) { final double xv = xvals[i]; final double xvP = xvals[i + 1]; final double xvM = xvals[i - 1]; firstDerivatives[i] = (((xvP - xv) * differences[i - 1]) + ((xv - xvM) * differences[i])) / (xvP - xvM); } else { firstDerivatives[i] = ((wP * differences[i - 1]) + (wM * differences[i])) / (wP + wM); } } firstDerivatives[0] = differentiateThreePoint(xvals, yvals, 0, 0, 1, 2); firstDerivatives[1] = differentiateThreePoint(xvals, yvals, 1, 0, 1, 2); firstDerivatives[xvals.length - 2] = differentiateThreePoint(xvals, yvals, xvals.length - 2, xvals.length - 3, xvals.length - 2, xvals.length - 1); firstDerivatives[xvals.length - 1] = differentiateThreePoint(xvals, yvals, xvals.length - 1, xvals.length - 3, xvals.length - 2, xvals.length - 1); return interpolateHermiteSorted(xvals, yvals, firstDerivatives); }  PolynomialSplineFunction interpolate(double[] xvals,
                                                double[] yvals);  }
@Test public void testIllegalArguments() { UnivariateInterpolator i = new AkimaSplineInterpolator(); try { double yval[] = { 0.0, 1.0, 2.0, 3.0, 4.0 }; i.interpolate( null, yval ); Assert.fail( "Failed to detect x null pointer" ); } catch ( NullArgumentException iae ) { } try { double xval[] = { 0.0, 1.0, 2.0, 3.0, 4.0 }; i.interpolate( xval, null ); Assert.fail( "Failed to detect y null pointer" ); } catch ( NullArgumentException iae ) { } try { double xval[] = { 0.0, 1.0, 2.0, 3.0 }; double yval[] = { 0.0, 1.0, 2.0, 3.0 }; i.interpolate( xval, yval ); Assert.fail( "Failed to detect insufficient data" ); } catch ( NumberIsTooSmallException iae ) { } try { double xval[] = { 0.0, 1.0, 2.0, 3.0, 4.0 }; double yval[] = { 0.0, 1.0, 2.0, 3.0, 4.0, 5.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, 7.0, 3.5, 2.2, 8.0 }; double yval[] = { 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 }; i.interpolate( xval, yval ); Assert.fail( "Failed to detect unsorted arguments." ); } catch ( NonMonotonicSequenceException iae ) { } }