PiecewiseBicubicSplineInterpolator implements BivariateGridInterpolator { public PiecewiseBicubicSplineInterpolatingFunction interpolate( final double[] xval, final double[] yval, final double[][] fval) throws DimensionMismatchException, NullArgumentException, NoDataException, NonMonotonicSequenceException { if ( xval == null || yval == null || fval == null || fval[0] == null ) { throw new NullArgumentException(); } if ( xval.length == 0 || yval.length == 0 || fval.length == 0 ) { throw new NoDataException(); } MathArrays.checkOrder(xval); MathArrays.checkOrder(yval); return new PiecewiseBicubicSplineInterpolatingFunction( xval, yval, fval ); }  PiecewiseBicubicSplineInterpolatingFunction interpolate( final double[] xval,
                                                                    final double[] yval,
                                                                    final double[][] fval);  }
@Test public void testPreconditions() { double[] xval = new double[] { 3, 4, 5, 6.5, 7.5 }; double[] yval = new double[] { -4, -3, -1, 2.5, 3.5 }; double[][] zval = new double[xval.length][yval.length]; @SuppressWarnings( "unused" ) BivariateGridInterpolator interpolator = new PiecewiseBicubicSplineInterpolator(); try { interpolator.interpolate( null, yval, zval ); Assert.fail( "Failed to detect x null pointer" ); } catch ( NullArgumentException iae ) { } try { interpolator.interpolate( xval, null, zval ); Assert.fail( "Failed to detect y null pointer" ); } catch ( NullArgumentException iae ) { } try { interpolator.interpolate( xval, yval, null ); Assert.fail( "Failed to detect z null pointer" ); } catch ( NullArgumentException iae ) { } try { double xval1[] = { 0.0, 1.0, 2.0, 3.0 }; interpolator.interpolate( xval1, yval, zval ); Assert.fail( "Failed to detect insufficient x data" ); } catch ( InsufficientDataException iae ) { } try { double yval1[] = { 0.0, 1.0, 2.0, 3.0 }; interpolator.interpolate( xval, yval1, zval ); Assert.fail( "Failed to detect insufficient y data" ); } catch ( InsufficientDataException iae ) { } try { double zval1[][] = new double[4][4]; interpolator.interpolate( xval, yval, zval1 ); Assert.fail( "Failed to detect insufficient z data" ); } catch ( InsufficientDataException iae ) { } try { double xval1[] = { 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 }; interpolator.interpolate( xval1, yval, zval ); Assert.fail( "Failed to detect data set array with different sizes." ); } catch ( DimensionMismatchException iae ) { } try { double yval1[] = { 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 }; interpolator.interpolate( xval, yval1, zval ); Assert.fail( "Failed to detect data set array with different sizes." ); } catch ( DimensionMismatchException iae ) { } try { double xval1[] = { 0.0, 1.0, 0.5, 7.0, 3.5 }; interpolator.interpolate( xval1, yval, zval ); Assert.fail( "Failed to detect unsorted x arguments." ); } catch ( NonMonotonicSequenceException iae ) { } try { double yval1[] = { 0.0, 1.0, 1.5, 0.0, 3.0 }; interpolator.interpolate( xval, yval1, zval ); Assert.fail( "Failed to detect unsorted y arguments." ); } catch ( NonMonotonicSequenceException iae ) { } }
@Test public void testInterpolation1() { final int sz = 21; double[] xval = new double[sz]; double[] yval = new double[sz]; final double delta = 1d / (sz - 1); for ( int i = 0; i < sz; i++ ){ xval[i] = -1 + 15 * i * delta; yval[i] = -20 + 30 * i * delta; } BivariateFunction f = new BivariateFunction() { public double value( double x, double y ) { return 2 * x - 3 * y + 5; } }; double[][] zval = new double[xval.length][yval.length]; for ( int i = 0; i < xval.length; i++ ) { for ( int j = 0; j < yval.length; j++ ) { zval[i][j] = f.value(xval[i], yval[j]); } } BivariateGridInterpolator interpolator = new PiecewiseBicubicSplineInterpolator(); BivariateFunction p = interpolator.interpolate(xval, yval, zval); double x, y; final RandomGenerator rng = new Well19937c(1234567L); final UniformRealDistribution distX = new UniformRealDistribution( rng, xval[0], xval[xval.length - 1] ); final UniformRealDistribution distY = new UniformRealDistribution( rng, yval[0], yval[yval.length - 1] ); final int numSamples = 50; final double tol = 2e-14; for ( int i = 0; i < numSamples; i++ ) { x = distX.sample(); for ( int j = 0; j < numSamples; j++ ) { y = distY.sample(); Assert.assertEquals(f.value(x, y), p.value(x, y), tol); } } }
@Test public void testInterpolation2() { final int sz = 21; double[] xval = new double[sz]; double[] yval = new double[sz]; final double delta = 1d / (sz - 1); for ( int i = 0; i < sz; i++ ) { xval[i] = -1 + 15 * i * delta; yval[i] = -20 + 30 * i * delta; } BivariateFunction f = new BivariateFunction() { public double value( double x, double y ) { return 2 * x * x - 3 * y * y + 4 * x * y - 5; } }; double[][] zval = new double[xval.length][yval.length]; for ( int i = 0; i < xval.length; i++ ) { for ( int j = 0; j < yval.length; j++ ) { zval[i][j] = f.value(xval[i], yval[j]); } } BivariateGridInterpolator interpolator = new PiecewiseBicubicSplineInterpolator(); BivariateFunction p = interpolator.interpolate(xval, yval, zval); double x, y; final RandomGenerator rng = new Well19937c(1234567L); final UniformRealDistribution distX = new UniformRealDistribution( rng, xval[0], xval[xval.length - 1] ); final UniformRealDistribution distY = new UniformRealDistribution( rng, yval[0], yval[yval.length - 1] ); final int numSamples = 50; final double tol = 5e-13; for ( int i = 0; i < numSamples; i++ ) { x = distX.sample(); for ( int j = 0; j < numSamples; j++ ) { y = distY.sample(); Assert.assertEquals(f.value(x, y), p.value(x, y), tol); } } }