TricubicInterpolator.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 tricubic interpolating function.
  28.  *
  29.  */
  30. public class TricubicInterpolator
  31.     implements TrivariateGridInterpolator {

  32.     /** Empty constructor.
  33.      * <p>
  34.      * This constructor is not strictly necessary, but it prevents spurious
  35.      * javadoc warnings with JDK 18 and later.
  36.      * </p>
  37.      * @since 3.0
  38.      */
  39.     public TricubicInterpolator() { // NOPMD - unnecessary constructor added intentionally to make javadoc happy
  40.         // nothing to do
  41.     }

  42.     /**
  43.      * {@inheritDoc}
  44.      */
  45.     @Override
  46.     public TricubicInterpolatingFunction interpolate(final double[] xval,
  47.                                                      final double[] yval,
  48.                                                      final double[] zval,
  49.                                                      final double[][][] fval)
  50.         throws MathIllegalArgumentException {
  51.         if (xval.length == 0 || yval.length == 0 || zval.length == 0 || fval.length == 0) {
  52.             throw new MathIllegalArgumentException(LocalizedCoreFormats.NO_DATA);
  53.         }
  54.         if (xval.length != fval.length) {
  55.             throw new MathIllegalArgumentException(LocalizedCoreFormats.DIMENSIONS_MISMATCH,
  56.                                                    xval.length, fval.length);
  57.         }

  58.         MathArrays.checkOrder(xval);
  59.         MathArrays.checkOrder(yval);
  60.         MathArrays.checkOrder(zval);

  61.         final int xLen = xval.length;
  62.         final int yLen = yval.length;
  63.         final int zLen = zval.length;

  64.         // Approximation to the partial derivatives using finite differences.
  65.         final double[][][] dFdX = new double[xLen][yLen][zLen];
  66.         final double[][][] dFdY = new double[xLen][yLen][zLen];
  67.         final double[][][] dFdZ = new double[xLen][yLen][zLen];
  68.         final double[][][] d2FdXdY = new double[xLen][yLen][zLen];
  69.         final double[][][] d2FdXdZ = new double[xLen][yLen][zLen];
  70.         final double[][][] d2FdYdZ = new double[xLen][yLen][zLen];
  71.         final double[][][] d3FdXdYdZ = new double[xLen][yLen][zLen];

  72.         for (int i = 1; i < xLen - 1; i++) {
  73.             MathUtils.checkDimension(yval.length, fval[i].length);

  74.             final int nI = i + 1;
  75.             final int pI = i - 1;

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

  78.             final double deltaX = nX - pX;

  79.             for (int j = 1; j < yLen - 1; j++) {
  80.                 MathUtils.checkDimension(zval.length, fval[i][j].length);

  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.                 final double deltaXY = deltaX * deltaY;

  87.                 for (int k = 1; k < zLen - 1; k++) {
  88.                     final int nK = k + 1;
  89.                     final int pK = k - 1;

  90.                     final double nZ = zval[nK];
  91.                     final double pZ = zval[pK];

  92.                     final double deltaZ = nZ - pZ;

  93.                     dFdX[i][j][k] = (fval[nI][j][k] - fval[pI][j][k]) / deltaX;
  94.                     dFdY[i][j][k] = (fval[i][nJ][k] - fval[i][pJ][k]) / deltaY;
  95.                     dFdZ[i][j][k] = (fval[i][j][nK] - fval[i][j][pK]) / deltaZ;

  96.                     final double deltaXZ = deltaX * deltaZ;
  97.                     final double deltaYZ = deltaY * deltaZ;

  98.                     d2FdXdY[i][j][k] = (fval[nI][nJ][k] - fval[nI][pJ][k] - fval[pI][nJ][k] + fval[pI][pJ][k]) / deltaXY;
  99.                     d2FdXdZ[i][j][k] = (fval[nI][j][nK] - fval[nI][j][pK] - fval[pI][j][nK] + fval[pI][j][pK]) / deltaXZ;
  100.                     d2FdYdZ[i][j][k] = (fval[i][nJ][nK] - fval[i][nJ][pK] - fval[i][pJ][nK] + fval[i][pJ][pK]) / deltaYZ;

  101.                     final double deltaXYZ = deltaXY * deltaZ;

  102.                     d3FdXdYdZ[i][j][k] = (fval[nI][nJ][nK] - fval[nI][pJ][nK] -
  103.                                           fval[pI][nJ][nK] + fval[pI][pJ][nK] -
  104.                                           fval[nI][nJ][pK] + fval[nI][pJ][pK] +
  105.                                           fval[pI][nJ][pK] - fval[pI][pJ][pK]) / deltaXYZ;
  106.                 }
  107.             }
  108.         }

  109.         // Create the interpolating function.
  110.         return new TricubicInterpolatingFunction(xval, yval, zval, fval,
  111.                                                  dFdX, dFdY, dFdZ,
  112.                                                  d2FdXdY, d2FdXdZ, d2FdYdZ,
  113.                                                  d3FdXdYdZ) {
  114.             /** {@inheritDoc} */
  115.             @Override
  116.             public boolean isValidPoint(double x, double y, double z) {
  117.                 if (x < xval[1] ||
  118.                     x > xval[xval.length - 2] ||
  119.                     y < yval[1] ||
  120.                     y > yval[yval.length - 2] ||
  121.                     z < zval[1] ||
  122.                     z > zval[zval.length - 2]) {
  123.                     return false;
  124.                 } else {
  125.                     return true;
  126.                 }
  127.             }
  128.         };
  129.     }
  130. }