BicubicSplineInterpolator.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 org.apache.commons.math3.analysis.UnivariateFunction;
  19. import org.apache.commons.math3.analysis.polynomials.PolynomialSplineFunction;
  20. import org.apache.commons.math3.exception.DimensionMismatchException;
  21. import org.apache.commons.math3.exception.NoDataException;
  22. import org.apache.commons.math3.exception.NonMonotonicSequenceException;
  23. import org.apache.commons.math3.exception.NumberIsTooSmallException;
  24. import org.apache.commons.math3.util.MathArrays;

  25. /**
  26.  * Generates a bicubic interpolating function. Due to numerical accuracy issues this should not
  27.  * be used.
  28.  *
  29.  * @since 2.2
  30.  * @deprecated as of 3.4 replaced by {@link org.apache.commons.math3.analysis.interpolation.PiecewiseBicubicSplineInterpolator}
  31.  */
  32. @Deprecated
  33. public class BicubicSplineInterpolator
  34.     implements BivariateGridInterpolator {
  35.     /** Whether to initialize internal data used to compute the analytical
  36.         derivatives of the splines. */
  37.     private final boolean initializeDerivatives;

  38.     /**
  39.      * Default constructor.
  40.      * The argument {@link #BicubicSplineInterpolator(boolean) initializeDerivatives}
  41.      * is set to {@code false}.
  42.      */
  43.     public BicubicSplineInterpolator() {
  44.         this(false);
  45.     }

  46.     /**
  47.      * Creates an interpolator.
  48.      *
  49.      * @param initializeDerivatives Whether to initialize the internal data
  50.      * needed for calling any of the methods that compute the partial derivatives
  51.      * of the {@link BicubicSplineInterpolatingFunction function} returned from
  52.      * the call to {@link #interpolate(double[],double[],double[][]) interpolate}.
  53.      */
  54.     public BicubicSplineInterpolator(boolean initializeDerivatives) {
  55.         this.initializeDerivatives = initializeDerivatives;
  56.     }

  57.     /**
  58.      * {@inheritDoc}
  59.      */
  60.     public BicubicSplineInterpolatingFunction interpolate(final double[] xval,
  61.                                                           final double[] yval,
  62.                                                           final double[][] fval)
  63.         throws NoDataException, DimensionMismatchException,
  64.                NonMonotonicSequenceException, NumberIsTooSmallException {
  65.         if (xval.length == 0 || yval.length == 0 || fval.length == 0) {
  66.             throw new NoDataException();
  67.         }
  68.         if (xval.length != fval.length) {
  69.             throw new DimensionMismatchException(xval.length, fval.length);
  70.         }

  71.         MathArrays.checkOrder(xval);
  72.         MathArrays.checkOrder(yval);

  73.         final int xLen = xval.length;
  74.         final int yLen = yval.length;

  75.         // Samples (first index is y-coordinate, i.e. subarray variable is x)
  76.         // 0 <= i < xval.length
  77.         // 0 <= j < yval.length
  78.         // fX[j][i] = f(xval[i], yval[j])
  79.         final double[][] fX = new double[yLen][xLen];
  80.         for (int i = 0; i < xLen; i++) {
  81.             if (fval[i].length != yLen) {
  82.                 throw new DimensionMismatchException(fval[i].length, yLen);
  83.             }

  84.             for (int j = 0; j < yLen; j++) {
  85.                 fX[j][i] = fval[i][j];
  86.             }
  87.         }

  88.         final SplineInterpolator spInterpolator = new SplineInterpolator();

  89.         // For each line y[j] (0 <= j < yLen), construct a 1D spline with
  90.         // respect to variable x
  91.         final PolynomialSplineFunction[] ySplineX = new PolynomialSplineFunction[yLen];
  92.         for (int j = 0; j < yLen; j++) {
  93.             ySplineX[j] = spInterpolator.interpolate(xval, fX[j]);
  94.         }

  95.         // For each line x[i] (0 <= i < xLen), construct a 1D spline with
  96.         // respect to variable y generated by array fY_1[i]
  97.         final PolynomialSplineFunction[] xSplineY = new PolynomialSplineFunction[xLen];
  98.         for (int i = 0; i < xLen; i++) {
  99.             xSplineY[i] = spInterpolator.interpolate(yval, fval[i]);
  100.         }

  101.         // Partial derivatives with respect to x at the grid knots
  102.         final double[][] dFdX = new double[xLen][yLen];
  103.         for (int j = 0; j < yLen; j++) {
  104.             final UnivariateFunction f = ySplineX[j].derivative();
  105.             for (int i = 0; i < xLen; i++) {
  106.                 dFdX[i][j] = f.value(xval[i]);
  107.             }
  108.         }

  109.         // Partial derivatives with respect to y at the grid knots
  110.         final double[][] dFdY = new double[xLen][yLen];
  111.         for (int i = 0; i < xLen; i++) {
  112.             final UnivariateFunction f = xSplineY[i].derivative();
  113.             for (int j = 0; j < yLen; j++) {
  114.                 dFdY[i][j] = f.value(yval[j]);
  115.             }
  116.         }

  117.         // Cross partial derivatives
  118.         final double[][] d2FdXdY = new double[xLen][yLen];
  119.         for (int i = 0; i < xLen ; i++) {
  120.             final int nI = nextIndex(i, xLen);
  121.             final int pI = previousIndex(i);
  122.             for (int j = 0; j < yLen; j++) {
  123.                 final int nJ = nextIndex(j, yLen);
  124.                 final int pJ = previousIndex(j);
  125.                 d2FdXdY[i][j] = (fval[nI][nJ] - fval[nI][pJ] -
  126.                                  fval[pI][nJ] + fval[pI][pJ]) /
  127.                     ((xval[nI] - xval[pI]) * (yval[nJ] - yval[pJ]));
  128.             }
  129.         }

  130.         // Create the interpolating splines
  131.         return new BicubicSplineInterpolatingFunction(xval, yval, fval,
  132.                                                       dFdX, dFdY, d2FdXdY,
  133.                                                       initializeDerivatives);
  134.     }

  135.     /**
  136.      * Computes the next index of an array, clipping if necessary.
  137.      * It is assumed (but not checked) that {@code i >= 0}.
  138.      *
  139.      * @param i Index.
  140.      * @param max Upper limit of the array.
  141.      * @return the next index.
  142.      */
  143.     private int nextIndex(int i, int max) {
  144.         final int index = i + 1;
  145.         return index < max ? index : index - 1;
  146.     }
  147.     /**
  148.      * Computes the previous index of an array, clipping if necessary.
  149.      * It is assumed (but not checked) that {@code i} is smaller than the size
  150.      * of the array.
  151.      *
  152.      * @param i Index.
  153.      * @return the previous index.
  154.      */
  155.     private int previousIndex(int i) {
  156.         final int index = i - 1;
  157.         return index >= 0 ? index : 0;
  158.     }
  159. }