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.  *      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.math4.legacy.analysis.interpolation;

  18. import org.apache.commons.math4.legacy.exception.DimensionMismatchException;
  19. import org.apache.commons.math4.legacy.exception.NoDataException;
  20. import org.apache.commons.math4.legacy.exception.NonMonotonicSequenceException;
  21. import org.apache.commons.math4.legacy.exception.NumberIsTooSmallException;
  22. import org.apache.commons.math4.legacy.core.MathArrays;

  23. /**
  24.  * Generates a tricubic interpolating function.
  25.  *
  26.  * @since 3.4
  27.  */
  28. public class TricubicInterpolator
  29.     implements TrivariateGridInterpolator {
  30.     /**
  31.      * {@inheritDoc}
  32.      */
  33.     @Override
  34.     public TricubicInterpolatingFunction interpolate(final double[] xval,
  35.                                                      final double[] yval,
  36.                                                      final double[] zval,
  37.                                                      final double[][][] fval)
  38.         throws NoDataException, NumberIsTooSmallException,
  39.                DimensionMismatchException, NonMonotonicSequenceException {
  40.         if (xval.length == 0 || yval.length == 0 || zval.length == 0 || fval.length == 0) {
  41.             throw new NoDataException();
  42.         }
  43.         if (xval.length != fval.length) {
  44.             throw new DimensionMismatchException(xval.length, fval.length);
  45.         }

  46.         MathArrays.checkOrder(xval);
  47.         MathArrays.checkOrder(yval);
  48.         MathArrays.checkOrder(zval);

  49.         final int xLen = xval.length;
  50.         final int yLen = yval.length;
  51.         final int zLen = zval.length;

  52.         // Approximation to the partial derivatives using finite differences.
  53.         final double[][][] dFdX = new double[xLen][yLen][zLen];
  54.         final double[][][] dFdY = new double[xLen][yLen][zLen];
  55.         final double[][][] dFdZ = new double[xLen][yLen][zLen];
  56.         final double[][][] d2FdXdY = new double[xLen][yLen][zLen];
  57.         final double[][][] d2FdXdZ = new double[xLen][yLen][zLen];
  58.         final double[][][] d2FdYdZ = new double[xLen][yLen][zLen];
  59.         final double[][][] d3FdXdYdZ = new double[xLen][yLen][zLen];

  60.         for (int i = 1; i < xLen - 1; i++) {
  61.             if (yval.length != fval[i].length) {
  62.                 throw new DimensionMismatchException(yval.length, fval[i].length);
  63.             }

  64.             final int nI = i + 1;
  65.             final int pI = i - 1;

  66.             final double nX = xval[nI];
  67.             final double pX = xval[pI];

  68.             final double deltaX = nX - pX;

  69.             for (int j = 1; j < yLen - 1; j++) {
  70.                 if (zval.length != fval[i][j].length) {
  71.                     throw new DimensionMismatchException(zval.length, fval[i][j].length);
  72.                 }

  73.                 final int nJ = j + 1;
  74.                 final int pJ = j - 1;

  75.                 final double nY = yval[nJ];
  76.                 final double pY = yval[pJ];

  77.                 final double deltaY = nY - pY;
  78.                 final double deltaXY = deltaX * deltaY;

  79.                 for (int k = 1; k < zLen - 1; k++) {
  80.                     final int nK = k + 1;
  81.                     final int pK = k - 1;

  82.                     final double nZ = zval[nK];
  83.                     final double pZ = zval[pK];

  84.                     final double deltaZ = nZ - pZ;

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

  88.                     final double deltaXZ = deltaX * deltaZ;
  89.                     final double deltaYZ = deltaY * deltaZ;

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

  93.                     final double deltaXYZ = deltaXY * deltaZ;

  94.                     d3FdXdYdZ[i][j][k] = (fval[nI][nJ][nK] - fval[nI][pJ][nK] -
  95.                                           fval[pI][nJ][nK] + fval[pI][pJ][nK] -
  96.                                           fval[nI][nJ][pK] + fval[nI][pJ][pK] +
  97.                                           fval[pI][nJ][pK] - fval[pI][pJ][pK]) / deltaXYZ;
  98.                 }
  99.             }
  100.         }

  101.         // Create the interpolating function.
  102.         return new TricubicInterpolatingFunction(xval, yval, zval, fval,
  103.                                                  dFdX, dFdY, dFdZ,
  104.                                                  d2FdXdY, d2FdXdZ, d2FdYdZ,
  105.                                                  d3FdXdYdZ) {
  106.             /** {@inheritDoc} */
  107.             @Override
  108.             public boolean isValidPoint(double x, double y, double z) {
  109.                 return !(x < xval[1] ||
  110.                     x > xval[xval.length - 2] ||
  111.                     y < yval[1] ||
  112.                     y > yval[yval.length - 2] ||
  113.                     z < zval[1] ||
  114.                     z > zval[zval.length - 2]);
  115.             }
  116.         };
  117.     }
  118. }