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

  18. import java.util.ArrayList;
  19. import java.util.List;

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

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

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

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

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

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

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

  85.         MathUtils.checkNotNull(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.         MathUtils.checkNotNull(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.         MathUtils.checkNotNull(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. }