FieldBilinearInterpolatingFunction.java

  1. /*
  2.  * Licensed to the Hipparchus project 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 Hipparchus project 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. package org.hipparchus.analysis.interpolation;

  18. import org.hipparchus.CalculusFieldElement;
  19. import org.hipparchus.Field;
  20. import org.hipparchus.analysis.CalculusFieldBivariateFunction;
  21. import org.hipparchus.exception.MathIllegalArgumentException;
  22. import org.hipparchus.util.MathArrays;

  23. /**
  24.  * Interpolate grid data using bi-linear interpolation.
  25.  * <p>
  26.  * This interpolator is thread-safe.
  27.  * </p>
  28.  * @param <T> Type of the field elements.
  29.  * @since 4.0
  30.  */
  31. public class FieldBilinearInterpolatingFunction<T extends CalculusFieldElement<T>>
  32.     implements CalculusFieldBivariateFunction<T> {

  33.     /** Grid along the x axis. */
  34.     private final FieldGridAxis<T> xGrid;

  35.     /** Grid along the y axis. */
  36.     private final FieldGridAxis<T> yGrid;

  37.     /** Grid size along the y axis. */
  38.     private final int ySize;

  39.     /** Values of the interpolation points on all the grid knots (in a flatten array). */
  40.     private final T[] fVal;

  41.     /** Simple constructor.
  42.      * @param xVal All the x-coordinates of the interpolation points, sorted
  43.      * in increasing order.
  44.      * @param yVal All the y-coordinates of the interpolation points, sorted
  45.      * in increasing order.
  46.      * @param fVal The values of the interpolation points on all the grid knots:
  47.      * {@code fVal[i][j] = f(xVal[i], yVal[j])}.
  48.      * @exception MathIllegalArgumentException if grid size is smaller than 2
  49.      * or if the grid is not sorted in strict increasing order
  50.      */
  51.     public FieldBilinearInterpolatingFunction(final T[] xVal, final T[] yVal, final T[][] fVal)
  52.         throws MathIllegalArgumentException {
  53.         final Field<T> field = fVal[0][0].getField();
  54.         this.xGrid = new FieldGridAxis<>(xVal, 2);
  55.         this.yGrid = new FieldGridAxis<>(yVal, 2);
  56.         this.ySize = yVal.length;
  57.         this.fVal  = MathArrays.buildArray(field, xVal.length * ySize);
  58.         int k = 0;
  59.         for (int i = 0; i < xVal.length; ++i) {
  60.             final T[] fi = fVal[i];
  61.             for (int j = 0; j < ySize; ++j) {
  62.                 this.fVal[k++] = fi[j];
  63.             }
  64.         }
  65.     }

  66.     /** Get the lowest grid x coordinate.
  67.      * @return lowest grid x coordinate
  68.      */
  69.     public T getXInf() {
  70.         return xGrid.node(0);
  71.     }

  72.     /** Get the highest grid x coordinate.
  73.      * @return highest grid x coordinate
  74.      */
  75.     public T getXSup() {
  76.         return xGrid.node(xGrid.size() - 1);
  77.     }

  78.     /** Get the lowest grid y coordinate.
  79.      * @return lowest grid y coordinate
  80.      */
  81.     public T getYInf() {
  82.         return yGrid.node(0);
  83.     }

  84.     /** Get the highest grid y coordinate.
  85.      * @return highest grid y coordinate
  86.      */
  87.     public T getYSup() {
  88.         return yGrid.node(yGrid.size() - 1);
  89.     }

  90.     /** {@inheritDoc} */
  91.     @Override
  92.     public T value(T x, T y) {

  93.         // get the interpolation nodes
  94.         final int i   = xGrid.interpolationIndex(x);
  95.         final int j   = yGrid.interpolationIndex(y);
  96.         final T   x0  = xGrid.node(i);
  97.         final T   x1  = xGrid.node(i + 1);
  98.         final T   y0  = yGrid.node(j);
  99.         final T   y1  = yGrid.node(j + 1);

  100.         // get the function values at interpolation nodes
  101.         final int k0  = i * ySize + j;
  102.         final int k1  = k0 + ySize;
  103.         final T   z00 = fVal[k0];
  104.         final T   z01 = fVal[k0 + 1];
  105.         final T   z10 = fVal[k1];
  106.         final T   z11 = fVal[k1 + 1];

  107.         // interpolate
  108.         final T   dx0  = x.subtract(x0);
  109.         final T   mdx1 = x.subtract(x1);
  110.         final T   dx10 = x1.subtract(x0);
  111.         final T   dy0  = y.subtract(y0);
  112.         final T   mdy1 = y.subtract(y1);
  113.         final T   dy10 = y1.subtract(y0);

  114.         return          dy0.multiply(z11).subtract(mdy1.multiply(z10)).multiply(dx0).
  115.                subtract(dy0.multiply(z01).subtract(mdy1.multiply(z00)).multiply(mdx1)).
  116.                divide(dx10.multiply(dy10));

  117.     }

  118. }