BicubicInterpolator.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.  *      https://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. /*
  18.  * This is not the original file distributed by the Apache Software Foundation
  19.  * It has been modified by the Hipparchus project
  20.  */
  21. package org.hipparchus.analysis.interpolation;

  22. import org.hipparchus.exception.LocalizedCoreFormats;
  23. import org.hipparchus.exception.MathIllegalArgumentException;
  24. import org.hipparchus.util.MathArrays;
  25. import org.hipparchus.util.MathUtils;

  26. /**
  27.  * Generates a {@link BicubicInterpolatingFunction bicubic interpolating
  28.  * function}.
  29.  * <p>
  30.  *  Caveat: Because the interpolation scheme requires that derivatives be
  31.  *  specified at the sample points, those are approximated with finite
  32.  *  differences (using the 2-points symmetric formulae).
  33.  *  Since their values are undefined at the borders of the provided
  34.  *  interpolation ranges, the interpolated values will be wrong at the
  35.  *  edges of the patch.
  36.  *  The {@code interpolate} method will return a function that overrides
  37.  *  {@link BicubicInterpolatingFunction#isValidPoint(double,double)} to
  38.  *  indicate points where the interpolation will be inaccurate.
  39.  * </p>
  40.  *
  41.  */
  42. public class BicubicInterpolator
  43.     implements BivariateGridInterpolator {

  44.     /** Empty constructor.
  45.      * <p>
  46.      * This constructor is not strictly necessary, but it prevents spurious
  47.      * javadoc warnings with JDK 18 and later.
  48.      * </p>
  49.      * @since 3.0
  50.      */
  51.     public BicubicInterpolator() { // NOPMD - unnecessary constructor added intentionally to make javadoc happy
  52.         // nothing to do
  53.     }

  54.     /**
  55.      * {@inheritDoc}
  56.      */
  57.     @Override
  58.     public BicubicInterpolatingFunction interpolate(final double[] xval,
  59.                                                     final double[] yval,
  60.                                                     final double[][] fval)
  61.         throws MathIllegalArgumentException {
  62.         if (xval.length == 0 || yval.length == 0 || fval.length == 0) {
  63.             throw new MathIllegalArgumentException(LocalizedCoreFormats.NO_DATA);
  64.         }
  65.         MathUtils.checkDimension(xval.length, fval.length);
  66.         MathArrays.checkOrder(xval);
  67.         MathArrays.checkOrder(yval);

  68.         final int xLen = xval.length;
  69.         final int yLen = yval.length;

  70.         // Approximation to the partial derivatives using finite differences.
  71.         final double[][] dFdX = new double[xLen][yLen];
  72.         final double[][] dFdY = new double[xLen][yLen];
  73.         final double[][] d2FdXdY = new double[xLen][yLen];
  74.         for (int i = 1; i < xLen - 1; i++) {
  75.             final int nI = i + 1;
  76.             final int pI = i - 1;

  77.             final double nX = xval[nI];
  78.             final double pX = xval[pI];

  79.             final double deltaX = nX - pX;

  80.             for (int j = 1; j < yLen - 1; j++) {
  81.                 final int nJ = j + 1;
  82.                 final int pJ = j - 1;

  83.                 final double nY = yval[nJ];
  84.                 final double pY = yval[pJ];

  85.                 final double deltaY = nY - pY;

  86.                 dFdX[i][j] = (fval[nI][j] - fval[pI][j]) / deltaX;
  87.                 dFdY[i][j] = (fval[i][nJ] - fval[i][pJ]) / deltaY;

  88.                 final double deltaXY = deltaX * deltaY;

  89.                 d2FdXdY[i][j] = (fval[nI][nJ] - fval[nI][pJ] - fval[pI][nJ] + fval[pI][pJ]) / deltaXY;
  90.             }
  91.         }

  92.         // Create the interpolating function.
  93.         return new BicubicInterpolatingFunction(xval, yval, fval,
  94.                                                 dFdX, dFdY, d2FdXdY) {
  95.             /** {@inheritDoc} */
  96.             @Override
  97.             public boolean isValidPoint(double x, double y) {
  98.                 if (x < xval[1] ||
  99.                     x > xval[xval.length - 2] ||
  100.                     y < yval[1] ||
  101.                     y > yval[yval.length - 2]) {
  102.                     return false;
  103.                 } else {
  104.                     return true;
  105.                 }
  106.             }
  107.         };
  108.     }
  109. }