PiecewiseBicubicSplineInterpolatingFunction.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.math3.analysis.interpolation;

  18. import java.util.Arrays;
  19. import org.apache.commons.math3.analysis.BivariateFunction;
  20. import org.apache.commons.math3.analysis.polynomials.PolynomialSplineFunction;
  21. import org.apache.commons.math3.exception.DimensionMismatchException;
  22. import org.apache.commons.math3.exception.InsufficientDataException;
  23. import org.apache.commons.math3.exception.NoDataException;
  24. import org.apache.commons.math3.exception.NullArgumentException;
  25. import org.apache.commons.math3.exception.OutOfRangeException;
  26. import org.apache.commons.math3.exception.NonMonotonicSequenceException;
  27. import org.apache.commons.math3.util.MathArrays;

  28. /**
  29.  * Function that implements the
  30.  * <a href="http://www.paulinternet.nl/?page=bicubic">bicubic spline</a>
  31.  * interpolation.
  32.  * This implementation currently uses {@link AkimaSplineInterpolator} as the
  33.  * underlying one-dimensional interpolator, which requires 5 sample points;
  34.  * insufficient data will raise an exception when the
  35.  * {@link #value(double,double) value} method is called.
  36.  *
  37.  * @since 3.4
  38.  */
  39. public class PiecewiseBicubicSplineInterpolatingFunction
  40.     implements BivariateFunction {
  41.     /** The minimum number of points that are needed to compute the function. */
  42.     private static final int MIN_NUM_POINTS = 5;
  43.     /** Samples x-coordinates */
  44.     private final double[] xval;
  45.     /** Samples y-coordinates */
  46.     private final double[] yval;
  47.     /** Set of cubic splines patching the whole data grid */
  48.     private final double[][] fval;

  49.     /**
  50.      * @param x Sample values of the x-coordinate, in increasing order.
  51.      * @param y Sample values of the y-coordinate, in increasing order.
  52.      * @param f Values of the function on every grid point. the expected number
  53.      *        of elements.
  54.      * @throws NonMonotonicSequenceException if {@code x} or {@code y} are not
  55.      *         strictly increasing.
  56.      * @throws NullArgumentException if any of the arguments are null
  57.      * @throws NoDataException if any of the arrays has zero length.
  58.      * @throws DimensionMismatchException if the length of x and y don't match the row, column
  59.      *         height of f
  60.      */
  61.     public PiecewiseBicubicSplineInterpolatingFunction(double[] x,
  62.                                                        double[] y,
  63.                                                        double[][] f)
  64.         throws DimensionMismatchException,
  65.                NullArgumentException,
  66.                NoDataException,
  67.                NonMonotonicSequenceException {
  68.         if (x == null ||
  69.             y == null ||
  70.             f == null ||
  71.             f[0] == null) {
  72.             throw new NullArgumentException();
  73.         }

  74.         final int xLen = x.length;
  75.         final int yLen = y.length;

  76.         if (xLen == 0 ||
  77.             yLen == 0 ||
  78.             f.length == 0 ||
  79.             f[0].length == 0) {
  80.             throw new NoDataException();
  81.         }

  82.         if (xLen < MIN_NUM_POINTS ||
  83.             yLen < MIN_NUM_POINTS ||
  84.             f.length < MIN_NUM_POINTS ||
  85.             f[0].length < MIN_NUM_POINTS) {
  86.             throw new InsufficientDataException();
  87.         }

  88.         if (xLen != f.length) {
  89.             throw new DimensionMismatchException(xLen, f.length);
  90.         }

  91.         if (yLen != f[0].length) {
  92.             throw new DimensionMismatchException(yLen, f[0].length);
  93.         }

  94.         MathArrays.checkOrder(x);
  95.         MathArrays.checkOrder(y);

  96.         xval = x.clone();
  97.         yval = y.clone();
  98.         fval = f.clone();
  99.     }

  100.     /**
  101.      * {@inheritDoc}
  102.      */
  103.     public double value(double x,
  104.                         double y)
  105.         throws OutOfRangeException {
  106.         final AkimaSplineInterpolator interpolator = new AkimaSplineInterpolator();
  107.         final int offset = 2;
  108.         final int count = offset + 3;
  109.         final int i = searchIndex(x, xval, offset, count);
  110.         final int j = searchIndex(y, yval, offset, count);

  111.         final double xArray[] = new double[count];
  112.         final double yArray[] = new double[count];
  113.         final double zArray[] = new double[count];
  114.         final double interpArray[] = new double[count];

  115.         for (int index = 0; index < count; index++) {
  116.             xArray[index] = xval[i + index];
  117.             yArray[index] = yval[j + index];
  118.         }

  119.         for (int zIndex = 0; zIndex < count; zIndex++) {
  120.             for (int index = 0; index < count; index++) {
  121.                 zArray[index] = fval[i + index][j + zIndex];
  122.             }
  123.             final PolynomialSplineFunction spline = interpolator.interpolate(xArray, zArray);
  124.             interpArray[zIndex] = spline.value(x);
  125.         }

  126.         final PolynomialSplineFunction spline = interpolator.interpolate(yArray, interpArray);

  127.         double returnValue = spline.value(y);

  128.         return returnValue;
  129.     }

  130.     /**
  131.      * Indicates whether a point is within the interpolation range.
  132.      *
  133.      * @param x First coordinate.
  134.      * @param y Second coordinate.
  135.      * @return {@code true} if (x, y) is a valid point.
  136.      * @since 3.3
  137.      */
  138.     public boolean isValidPoint(double x,
  139.                                 double y) {
  140.         if (x < xval[0] ||
  141.             x > xval[xval.length - 1] ||
  142.             y < yval[0] ||
  143.             y > yval[yval.length - 1]) {
  144.             return false;
  145.         } else {
  146.             return true;
  147.         }
  148.     }

  149.     /**
  150.      * @param c Coordinate.
  151.      * @param val Coordinate samples.
  152.      * @param offset how far back from found value to offset for querying
  153.      * @param count total number of elements forward from beginning that will be
  154.      *        queried
  155.      * @return the index in {@code val} corresponding to the interval containing
  156.      *         {@code c}.
  157.      * @throws OutOfRangeException if {@code c} is out of the range defined by
  158.      *         the boundary values of {@code val}.
  159.      */
  160.     private int searchIndex(double c,
  161.                             double[] val,
  162.                             int offset,
  163.                             int count) {
  164.         int r = Arrays.binarySearch(val, c);

  165.         if (r == -1 || r == -val.length - 1) {
  166.             throw new OutOfRangeException(c, val[0], val[val.length - 1]);
  167.         }

  168.         if (r < 0) {
  169.             // "c" in within an interpolation sub-interval, which returns
  170.             // negative
  171.             // need to remove the negative sign for consistency
  172.             r = -r - offset - 1;
  173.         } else {
  174.             r -= offset;
  175.         }

  176.         if (r < 0) {
  177.             r = 0;
  178.         }

  179.         if ((r + count) >= val.length) {
  180.             // "c" is the last sample of the range: Return the index
  181.             // of the sample at the lower end of the last sub-interval.
  182.             r = val.length - count;
  183.         }

  184.         return r;
  185.     }
  186. }