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.  *      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 java.util.ArrayList;
  23. import java.util.List;

  24. import org.hipparchus.FieldElement;
  25. import org.hipparchus.exception.LocalizedCoreFormats;
  26. import org.hipparchus.exception.MathIllegalArgumentException;
  27. import org.hipparchus.exception.MathRuntimeException;
  28. import org.hipparchus.exception.NullArgumentException;
  29. import org.hipparchus.util.MathArrays;
  30. import org.hipparchus.util.MathUtils;

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

  49.     /** Sample abscissae. */
  50.     private final List<T> abscissae;

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

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

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

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

  87.         MathUtils.checkNotNull(x);
  88.         T factorial = x.getField().getOne();
  89.         for (int i = 0; i < value.length; ++i) {

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

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

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

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

  117.         }

  118.     }

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

  126.         // safety check
  127.         MathUtils.checkNotNull(x);
  128.         if (abscissae.isEmpty()) {
  129.             throw new MathIllegalArgumentException(LocalizedCoreFormats.EMPTY_INTERPOLATION_SAMPLE);
  130.         }

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

  141.         return value;

  142.     }

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

  152.         // safety check
  153.         MathUtils.checkNotNull(x);
  154.         if (abscissae.isEmpty()) {
  155.             throw new MathIllegalArgumentException(LocalizedCoreFormats.EMPTY_INTERPOLATION_SAMPLE);
  156.         }

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

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

  182.         return derivatives;

  183.     }

  184. }