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.math3.analysis.interpolation;

  18. import org.apache.commons.math3.exception.DimensionMismatchException;
  19. import org.apache.commons.math3.exception.NoDataException;
  20. import org.apache.commons.math3.exception.NonMonotonicSequenceException;
  21. import org.apache.commons.math3.exception.NumberIsTooSmallException;
  22. import org.apache.commons.math3.util.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.     public TricubicInterpolatingFunction interpolate(final double[] xval,
  34.                                                      final double[] yval,
  35.                                                      final double[] zval,
  36.                                                      final double[][][] fval)
  37.         throws NoDataException, NumberIsTooSmallException,
  38.                DimensionMismatchException, NonMonotonicSequenceException {
  39.         if (xval.length == 0 || yval.length == 0 || zval.length == 0 || fval.length == 0) {
  40.             throw new NoDataException();
  41.         }
  42.         if (xval.length != fval.length) {
  43.             throw new DimensionMismatchException(xval.length, fval.length);
  44.         }

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

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

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

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

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

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

  67.             final double deltaX = nX - pX;

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

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

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

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

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

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

  83.                     final double deltaZ = nZ - pZ;

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

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

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

  92.                     final double deltaXYZ = deltaXY * deltaZ;

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

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