FieldHermiteInterpolator.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 java.util.ArrayList;
  19. import java.util.List;

  20. import org.apache.commons.math4.legacy.core.FieldElement;
  21. import org.apache.commons.math4.legacy.exception.DimensionMismatchException;
  22. import org.apache.commons.math4.legacy.exception.MathArithmeticException;
  23. import org.apache.commons.math4.legacy.exception.NoDataException;
  24. import org.apache.commons.math4.legacy.exception.NullArgumentException;
  25. import org.apache.commons.math4.legacy.exception.ZeroException;
  26. import org.apache.commons.math4.legacy.exception.util.LocalizedFormats;
  27. import org.apache.commons.math4.legacy.core.MathArrays;

  28. /** Polynomial interpolator using both sample values and sample derivatives.
  29.  * <p>
  30.  * The interpolation polynomials match all sample points, including both values
  31.  * and provided derivatives. There is one polynomial for each component of
  32.  * the values vector. All polynomials have the same degree. The degree of the
  33.  * polynomials depends on the number of points and number of derivatives at each
  34.  * point. For example the interpolation polynomials for n sample points without
  35.  * any derivatives all have degree n-1. The interpolation polynomials for n
  36.  * sample points with the two extreme points having value and first derivative
  37.  * and the remaining points having value only all have degree n+1. The
  38.  * interpolation polynomial for n sample points with value, first and second
  39.  * derivative for all points all have degree 3n-1.
  40.  * </p>
  41.  *
  42.  * @param <T> Type of the field elements.
  43.  *
  44.  * @since 3.2
  45.  */
  46. public class FieldHermiteInterpolator<T extends FieldElement<T>> {

  47.     /** Sample abscissae. */
  48.     private final List<T> abscissae;

  49.     /** Top diagonal of the divided differences array. */
  50.     private final List<T[]> topDiagonal;

  51.     /** Bottom diagonal of the divided differences array. */
  52.     private final List<T[]> bottomDiagonal;

  53.     /** Create an empty interpolator.
  54.      */
  55.     public FieldHermiteInterpolator() {
  56.         this.abscissae      = new ArrayList<>();
  57.         this.topDiagonal    = new ArrayList<>();
  58.         this.bottomDiagonal = new ArrayList<>();
  59.     }

  60.     /** Add a sample point.
  61.      * <p>
  62.      * This method must be called once for each sample point. It is allowed to
  63.      * mix some calls with values only with calls with values and first
  64.      * derivatives.
  65.      * </p>
  66.      * <p>
  67.      * The point abscissae for all calls <em>must</em> be different.
  68.      * </p>
  69.      * @param x abscissa of the sample point
  70.      * @param value value and derivatives of the sample point
  71.      * (if only one row is passed, it is the value, if two rows are
  72.      * passed the first one is the value and the second the derivative
  73.      * and so on)
  74.      * @exception ZeroException if the abscissa difference between added point
  75.      * and a previous point is zero (i.e. the two points are at same abscissa)
  76.      * @exception MathArithmeticException if the number of derivatives is larger
  77.      * than 20, which prevents computation of a factorial
  78.      * @throws DimensionMismatchException if derivative structures are inconsistent
  79.      * @throws NullArgumentException if x is null
  80.      */
  81.     @SafeVarargs
  82.     public final void addSamplePoint(final T x, final T[] ... value)
  83.         throws ZeroException, MathArithmeticException,
  84.                DimensionMismatchException, NullArgumentException {

  85.         NullArgumentException.check(x);
  86.         T factorial = x.getField().getOne();
  87.         for (int i = 0; i < value.length; ++i) {

  88.             final T[] y = value[i].clone();
  89.             if (i > 1) {
  90.                 factorial = factorial.multiply(i);
  91.                 final T inv = factorial.reciprocal();
  92.                 for (int j = 0; j < y.length; ++j) {
  93.                     y[j] = y[j].multiply(inv);
  94.                 }
  95.             }

  96.             // update the bottom diagonal of the divided differences array
  97.             final int n = abscissae.size();
  98.             bottomDiagonal.add(n - i, y);
  99.             T[] bottom0 = y;
  100.             for (int j = i; j < n; ++j) {
  101.                 final T[] bottom1 = bottomDiagonal.get(n - (j + 1));
  102.                 if (x.equals(abscissae.get(n - (j + 1)))) {
  103.                     throw new ZeroException(LocalizedFormats.DUPLICATED_ABSCISSA_DIVISION_BY_ZERO, x);
  104.                 }
  105.                 final T inv = x.subtract(abscissae.get(n - (j + 1))).reciprocal();
  106.                 for (int k = 0; k < y.length; ++k) {
  107.                     bottom1[k] = inv.multiply(bottom0[k].subtract(bottom1[k]));
  108.                 }
  109.                 bottom0 = bottom1;
  110.             }

  111.             // update the top diagonal of the divided differences array
  112.             topDiagonal.add(bottom0.clone());

  113.             // update the abscissae array
  114.             abscissae.add(x);
  115.         }
  116.     }

  117.     /** Interpolate value at a specified abscissa.
  118.      * @param x interpolation abscissa
  119.      * @return interpolated value
  120.      * @exception NoDataException if sample is empty
  121.      * @throws NullArgumentException if x is null
  122.      */
  123.     public T[] value(T x) throws NoDataException, NullArgumentException {

  124.         // safety check
  125.         NullArgumentException.check(x);
  126.         if (abscissae.isEmpty()) {
  127.             throw new NoDataException(LocalizedFormats.EMPTY_INTERPOLATION_SAMPLE);
  128.         }

  129.         final T[] value = MathArrays.buildArray(x.getField(), topDiagonal.get(0).length);
  130.         T valueCoeff = x.getField().getOne();
  131.         for (int i = 0; i < topDiagonal.size(); ++i) {
  132.             T[] dividedDifference = topDiagonal.get(i);
  133.             for (int k = 0; k < value.length; ++k) {
  134.                 value[k] = value[k].add(dividedDifference[k].multiply(valueCoeff));
  135.             }
  136.             final T deltaX = x.subtract(abscissae.get(i));
  137.             valueCoeff = valueCoeff.multiply(deltaX);
  138.         }

  139.         return value;
  140.     }

  141.     /** Interpolate value and first derivatives at a specified abscissa.
  142.      * @param x interpolation abscissa
  143.      * @param order maximum derivation order
  144.      * @return interpolated value and derivatives (value in row 0,
  145.      * 1<sup>st</sup> derivative in row 1, ... n<sup>th</sup> derivative in row n)
  146.      * @exception NoDataException if sample is empty
  147.      * @throws NullArgumentException if x is null
  148.      */
  149.     public T[][] derivatives(T x, int order) throws NoDataException, NullArgumentException {

  150.         // safety check
  151.         NullArgumentException.check(x);
  152.         if (abscissae.isEmpty()) {
  153.             throw new NoDataException(LocalizedFormats.EMPTY_INTERPOLATION_SAMPLE);
  154.         }

  155.         final T zero = x.getField().getZero();
  156.         final T one  = x.getField().getOne();
  157.         final T[] tj = MathArrays.buildArray(x.getField(), order + 1);
  158.         tj[0] = zero;
  159.         for (int i = 0; i < order; ++i) {
  160.             tj[i + 1] = tj[i].add(one);
  161.         }

  162.         final T[][] derivatives =
  163.                 MathArrays.buildArray(x.getField(), order + 1, topDiagonal.get(0).length);
  164.         final T[] valueCoeff = MathArrays.buildArray(x.getField(), order + 1);
  165.         valueCoeff[0] = x.getField().getOne();
  166.         for (int i = 0; i < topDiagonal.size(); ++i) {
  167.             T[] dividedDifference = topDiagonal.get(i);
  168.             final T deltaX = x.subtract(abscissae.get(i));
  169.             for (int j = order; j >= 0; --j) {
  170.                 for (int k = 0; k < derivatives[j].length; ++k) {
  171.                     derivatives[j][k] =
  172.                             derivatives[j][k].add(dividedDifference[k].multiply(valueCoeff[j]));
  173.                 }
  174.                 valueCoeff[j] = valueCoeff[j].multiply(deltaX);
  175.                 if (j > 0) {
  176.                     valueCoeff[j] = valueCoeff[j].add(tj[j].multiply(valueCoeff[j - 1]));
  177.                 }
  178.             }
  179.         }

  180.         return derivatives;
  181.     }
  182. }