FiniteDifferencesDifferentiator.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.differentiation;

  18. import org.apache.commons.math4.legacy.analysis.UnivariateFunction;
  19. import org.apache.commons.math4.legacy.analysis.UnivariateMatrixFunction;
  20. import org.apache.commons.math4.legacy.analysis.UnivariateVectorFunction;
  21. import org.apache.commons.math4.legacy.exception.MathIllegalArgumentException;
  22. import org.apache.commons.math4.legacy.exception.NotPositiveException;
  23. import org.apache.commons.math4.legacy.exception.NumberIsTooLargeException;
  24. import org.apache.commons.math4.legacy.exception.NumberIsTooSmallException;
  25. import org.apache.commons.math4.core.jdkmath.JdkMath;

  26. /** Univariate functions differentiator using finite differences.
  27.  * <p>
  28.  * This class creates some wrapper objects around regular
  29.  * {@link UnivariateFunction univariate functions} (or {@link
  30.  * UnivariateVectorFunction univariate vector functions} or {@link
  31.  * UnivariateMatrixFunction univariate matrix functions}). These
  32.  * wrapper objects compute derivatives in addition to function
  33.  * values.
  34.  * </p>
  35.  * <p>
  36.  * The wrapper objects work by calling the underlying function on
  37.  * a sampling grid around the current point and performing polynomial
  38.  * interpolation. A finite differences scheme with n points is
  39.  * theoretically able to compute derivatives up to order n-1, but
  40.  * it is generally better to have a slight margin. The step size must
  41.  * also be small enough in order for the polynomial approximation to
  42.  * be good in the current point neighborhood, but it should not be too
  43.  * small because numerical instability appears quickly (there are several
  44.  * differences of close points). Choosing the number of points and
  45.  * the step size is highly problem dependent.
  46.  * </p>
  47.  * <p>
  48.  * As an example of good and bad settings, lets consider the quintic
  49.  * polynomial function {@code f(x) = (x-1)*(x-0.5)*x*(x+0.5)*(x+1)}.
  50.  * Since it is a polynomial, finite differences with at least 6 points
  51.  * should theoretically recover the exact same polynomial and hence
  52.  * compute accurate derivatives for any order. However, due to numerical
  53.  * errors, we get the following results for a 7 points finite differences
  54.  * for abscissae in the [-10, 10] range:
  55.  * <ul>
  56.  *   <li>step size = 0.25, second order derivative error about 9.97e-10</li>
  57.  *   <li>step size = 0.25, fourth order derivative error about 5.43e-8</li>
  58.  *   <li>step size = 1.0e-6, second order derivative error about 148</li>
  59.  *   <li>step size = 1.0e-6, fourth order derivative error about 6.35e+14</li>
  60.  * </ul>
  61.  * <p>
  62.  * This example shows that the small step size is really bad, even simply
  63.  * for second order derivative!</p>
  64.  *
  65.  * @since 3.1
  66.  */
  67. public class FiniteDifferencesDifferentiator
  68.     implements UnivariateFunctionDifferentiator,
  69.                UnivariateVectorFunctionDifferentiator,
  70.                UnivariateMatrixFunctionDifferentiator {
  71.     /** Number of points to use. */
  72.     private final int nbPoints;

  73.     /** Step size. */
  74.     private final double stepSize;

  75.     /** Half sample span. */
  76.     private final double halfSampleSpan;

  77.     /** Lower bound for independent variable. */
  78.     private final double tMin;

  79.     /** Upper bound for independent variable. */
  80.     private final double tMax;

  81.     /**
  82.      * Build a differentiator with number of points and step size when independent variable is unbounded.
  83.      * <p>
  84.      * Beware that wrong settings for the finite differences differentiator
  85.      * can lead to highly unstable and inaccurate results, especially for
  86.      * high derivation orders. Using very small step sizes is often a
  87.      * <em>bad</em> idea.
  88.      * </p>
  89.      * @param nbPoints number of points to use
  90.      * @param stepSize step size (gap between each point)
  91.      * @exception NotPositiveException if {@code stepsize <= 0} (note that
  92.      * {@link NotPositiveException} extends {@link NumberIsTooSmallException})
  93.      * @exception NumberIsTooSmallException {@code nbPoint <= 1}
  94.      */
  95.     public FiniteDifferencesDifferentiator(final int nbPoints, final double stepSize)
  96.         throws NotPositiveException, NumberIsTooSmallException {
  97.         this(nbPoints, stepSize, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY);
  98.     }

  99.     /**
  100.      * Build a differentiator with number of points and step size when independent variable is bounded.
  101.      * <p>
  102.      * When the independent variable is bounded (tLower &lt; t &lt; tUpper), the sampling
  103.      * points used for differentiation will be adapted to ensure the constraint holds
  104.      * even near the boundaries. This means the sample will not be centered anymore in
  105.      * these cases. At an extreme case, computing derivatives exactly at the lower bound
  106.      * will lead the sample to be entirely on the right side of the derivation point.
  107.      * </p>
  108.      * <p>
  109.      * Note that the boundaries are considered to be excluded for function evaluation.
  110.      * </p>
  111.      * <p>
  112.      * Beware that wrong settings for the finite differences differentiator
  113.      * can lead to highly unstable and inaccurate results, especially for
  114.      * high derivation orders. Using very small step sizes is often a
  115.      * <em>bad</em> idea.
  116.      * </p>
  117.      * @param nbPoints number of points to use
  118.      * @param stepSize step size (gap between each point)
  119.      * @param tLower lower bound for independent variable (may be {@code Double.NEGATIVE_INFINITY}
  120.      * if there are no lower bounds)
  121.      * @param tUpper upper bound for independent variable (may be {@code Double.POSITIVE_INFINITY}
  122.      * if there are no upper bounds)
  123.      * @exception NotPositiveException if {@code stepsize <= 0} (note that
  124.      * {@link NotPositiveException} extends {@link NumberIsTooSmallException})
  125.      * @exception NumberIsTooSmallException {@code nbPoint <= 1}
  126.      * @exception NumberIsTooLargeException {@code stepSize * (nbPoints - 1) >= tUpper - tLower}
  127.      */
  128.     public FiniteDifferencesDifferentiator(final int nbPoints, final double stepSize,
  129.                                            final double tLower, final double tUpper)
  130.             throws NotPositiveException, NumberIsTooSmallException, NumberIsTooLargeException {

  131.         if (nbPoints <= 1) {
  132.             throw new NumberIsTooSmallException(stepSize, 1, false);
  133.         }
  134.         this.nbPoints = nbPoints;

  135.         if (stepSize <= 0) {
  136.             throw new NotPositiveException(stepSize);
  137.         }
  138.         this.stepSize = stepSize;

  139.         halfSampleSpan = 0.5 * stepSize * (nbPoints - 1);
  140.         if (2 * halfSampleSpan >= tUpper - tLower) {
  141.             throw new NumberIsTooLargeException(2 * halfSampleSpan, tUpper - tLower, false);
  142.         }
  143.         final double safety = JdkMath.ulp(halfSampleSpan);
  144.         this.tMin = tLower + halfSampleSpan + safety;
  145.         this.tMax = tUpper - halfSampleSpan - safety;
  146.     }

  147.     /**
  148.      * Get the number of points to use.
  149.      * @return number of points to use
  150.      */
  151.     public int getNbPoints() {
  152.         return nbPoints;
  153.     }

  154.     /**
  155.      * Get the step size.
  156.      * @return step size
  157.      */
  158.     public double getStepSize() {
  159.         return stepSize;
  160.     }

  161.     /**
  162.      * Evaluate derivatives from a sample.
  163.      * <p>
  164.      * Evaluation is done using divided differences.
  165.      * </p>
  166.      * @param t evaluation abscissa value and derivatives
  167.      * @param t0 first sample point abscissa
  168.      * @param y function values sample {@code y[i] = f(t[i]) = f(t0 + i * stepSize)}
  169.      * @return value and derivatives at {@code t}
  170.      * @exception NumberIsTooLargeException if the requested derivation order
  171.      * is larger or equal to the number of points
  172.      */
  173.     private DerivativeStructure evaluate(final DerivativeStructure t, final double t0,
  174.                                          final double[] y)
  175.         throws NumberIsTooLargeException {

  176.         // create divided differences diagonal arrays
  177.         final double[] top    = new double[nbPoints];
  178.         final double[] bottom = new double[nbPoints];

  179.         for (int i = 0; i < nbPoints; ++i) {

  180.             // update the bottom diagonal of the divided differences array
  181.             bottom[i] = y[i];
  182.             for (int j = 1; j <= i; ++j) {
  183.                 bottom[i - j] = (bottom[i - j + 1] - bottom[i - j]) / (j * stepSize);
  184.             }

  185.             // update the top diagonal of the divided differences array
  186.             top[i] = bottom[0];
  187.         }

  188.         // evaluate interpolation polynomial (represented by top diagonal) at t
  189.         final int order            = t.getOrder();
  190.         final int parameters       = t.getFreeParameters();
  191.         final double[] derivatives = t.getAllDerivatives();
  192.         final double dt0           = t.getValue() - t0;
  193.         DerivativeStructure interpolation = new DerivativeStructure(parameters, order, 0.0);
  194.         DerivativeStructure monomial = null;
  195.         for (int i = 0; i < nbPoints; ++i) {
  196.             if (i == 0) {
  197.                 // start with monomial(t) = 1
  198.                 monomial = new DerivativeStructure(parameters, order, 1.0);
  199.             } else {
  200.                 // monomial(t) = (t - t0) * (t - t1) * ... * (t - t(i-1))
  201.                 derivatives[0] = dt0 - (i - 1) * stepSize;
  202.                 final DerivativeStructure deltaX = new DerivativeStructure(parameters, order, derivatives);
  203.                 monomial = monomial.multiply(deltaX);
  204.             }
  205.             interpolation = interpolation.add(monomial.multiply(top[i]));
  206.         }

  207.         return interpolation;
  208.     }

  209.     /** {@inheritDoc}
  210.      * <p>The returned object cannot compute derivatives to arbitrary orders. The
  211.      * value function will throw a {@link NumberIsTooLargeException} if the requested
  212.      * derivation order is larger or equal to the number of points.
  213.      * </p>
  214.      */
  215.     @Override
  216.     public UnivariateDifferentiableFunction differentiate(final UnivariateFunction function) {
  217.         return new UnivariateDifferentiableFunction() {

  218.             /** {@inheritDoc} */
  219.             @Override
  220.             public double value(final double x) throws MathIllegalArgumentException {
  221.                 return function.value(x);
  222.             }

  223.             /** {@inheritDoc} */
  224.             @Override
  225.             public DerivativeStructure value(final DerivativeStructure t)
  226.                 throws MathIllegalArgumentException {

  227.                 // check we can achieve the requested derivation order with the sample
  228.                 if (t.getOrder() >= nbPoints) {
  229.                     throw new NumberIsTooLargeException(t.getOrder(), nbPoints, false);
  230.                 }

  231.                 // compute sample position, trying to be centered if possible
  232.                 final double t0 = JdkMath.max(JdkMath.min(t.getValue(), tMax), tMin) - halfSampleSpan;

  233.                 // compute sample points
  234.                 final double[] y = new double[nbPoints];
  235.                 for (int i = 0; i < nbPoints; ++i) {
  236.                     y[i] = function.value(t0 + i * stepSize);
  237.                 }

  238.                 // evaluate derivatives
  239.                 return evaluate(t, t0, y);
  240.             }
  241.         };
  242.     }

  243.     /** {@inheritDoc}
  244.      * <p>The returned object cannot compute derivatives to arbitrary orders. The
  245.      * value function will throw a {@link NumberIsTooLargeException} if the requested
  246.      * derivation order is larger or equal to the number of points.
  247.      * </p>
  248.      */
  249.     @Override
  250.     public UnivariateDifferentiableVectorFunction differentiate(final UnivariateVectorFunction function) {
  251.         return new UnivariateDifferentiableVectorFunction() {

  252.             /** {@inheritDoc} */
  253.             @Override
  254.             public double[]value(final double x) throws MathIllegalArgumentException {
  255.                 return function.value(x);
  256.             }

  257.             /** {@inheritDoc} */
  258.             @Override
  259.             public DerivativeStructure[] value(final DerivativeStructure t)
  260.                 throws MathIllegalArgumentException {

  261.                 // check we can achieve the requested derivation order with the sample
  262.                 if (t.getOrder() >= nbPoints) {
  263.                     throw new NumberIsTooLargeException(t.getOrder(), nbPoints, false);
  264.                 }

  265.                 // compute sample position, trying to be centered if possible
  266.                 final double t0 = JdkMath.max(JdkMath.min(t.getValue(), tMax), tMin) - halfSampleSpan;

  267.                 // compute sample points
  268.                 double[][] y = null;
  269.                 for (int i = 0; i < nbPoints; ++i) {
  270.                     final double[] v = function.value(t0 + i * stepSize);
  271.                     if (i == 0) {
  272.                         y = new double[v.length][nbPoints];
  273.                     }
  274.                     for (int j = 0; j < v.length; ++j) {
  275.                         y[j][i] = v[j];
  276.                     }
  277.                 }

  278.                 // evaluate derivatives
  279.                 final DerivativeStructure[] value = new DerivativeStructure[y.length];
  280.                 for (int j = 0; j < value.length; ++j) {
  281.                     value[j] = evaluate(t, t0, y[j]);
  282.                 }

  283.                 return value;
  284.             }
  285.         };
  286.     }

  287.     /** {@inheritDoc}
  288.      * <p>The returned object cannot compute derivatives to arbitrary orders. The
  289.      * value function will throw a {@link NumberIsTooLargeException} if the requested
  290.      * derivation order is larger or equal to the number of points.
  291.      * </p>
  292.      */
  293.     @Override
  294.     public UnivariateDifferentiableMatrixFunction differentiate(final UnivariateMatrixFunction function) {
  295.         return new UnivariateDifferentiableMatrixFunction() {

  296.             /** {@inheritDoc} */
  297.             @Override
  298.             public double[][]  value(final double x) throws MathIllegalArgumentException {
  299.                 return function.value(x);
  300.             }

  301.             /** {@inheritDoc} */
  302.             @Override
  303.             public DerivativeStructure[][]  value(final DerivativeStructure t)
  304.                 throws MathIllegalArgumentException {

  305.                 // check we can achieve the requested derivation order with the sample
  306.                 if (t.getOrder() >= nbPoints) {
  307.                     throw new NumberIsTooLargeException(t.getOrder(), nbPoints, false);
  308.                 }

  309.                 // compute sample position, trying to be centered if possible
  310.                 final double t0 = JdkMath.max(JdkMath.min(t.getValue(), tMax), tMin) - halfSampleSpan;

  311.                 // compute sample points
  312.                 double[][][] y = null;
  313.                 for (int i = 0; i < nbPoints; ++i) {
  314.                     final double[][] v = function.value(t0 + i * stepSize);
  315.                     if (i == 0) {
  316.                         y = new double[v.length][v[0].length][nbPoints];
  317.                     }
  318.                     for (int j = 0; j < v.length; ++j) {
  319.                         for (int k = 0; k < v[j].length; ++k) {
  320.                             y[j][k][i] = v[j][k];
  321.                         }
  322.                     }
  323.                 }

  324.                 // evaluate derivatives
  325.                 final DerivativeStructure[][] value = new DerivativeStructure[y.length][y[0].length];
  326.                 for (int j = 0; j < value.length; ++j) {
  327.                     for (int k = 0; k < y[j].length; ++k) {
  328.                         value[j][k] = evaluate(t, t0, y[j][k]);
  329.                     }
  330.                 }

  331.                 return value;
  332.             }
  333.         };
  334.     }
  335. }