PolynomialFunction.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.polynomials;

  22. import java.io.Serializable;
  23. import java.util.Arrays;

  24. import org.hipparchus.CalculusFieldElement;
  25. import org.hipparchus.analysis.FieldUnivariateFunction;
  26. import org.hipparchus.analysis.ParametricUnivariateFunction;
  27. import org.hipparchus.analysis.differentiation.Derivative;
  28. import org.hipparchus.analysis.differentiation.UnivariateDifferentiableFunction;
  29. import org.hipparchus.exception.LocalizedCoreFormats;
  30. import org.hipparchus.exception.MathIllegalArgumentException;
  31. import org.hipparchus.exception.NullArgumentException;
  32. import org.hipparchus.util.FastMath;
  33. import org.hipparchus.util.MathUtils;

  34. /**
  35.  * Immutable representation of a real polynomial function with real coefficients.
  36.  * <p>
  37.  * <a href="http://mathworld.wolfram.com/HornersMethod.html">Horner's Method</a>
  38.  * is used to evaluate the function.</p>
  39.  *
  40.  */
  41. public class PolynomialFunction implements UnivariateDifferentiableFunction, FieldUnivariateFunction, Serializable {
  42.     /**
  43.      * Serialization identifier
  44.      */
  45.     private static final long serialVersionUID = -7726511984200295583L;
  46.     /**
  47.      * The coefficients of the polynomial, ordered by degree -- i.e.,
  48.      * coefficients[0] is the constant term and coefficients[n] is the
  49.      * coefficient of x^n where n is the degree of the polynomial.
  50.      */
  51.     private final double[] coefficients;

  52.     /**
  53.      * Construct a polynomial with the given coefficients.  The first element
  54.      * of the coefficients array is the constant term.  Higher degree
  55.      * coefficients follow in sequence.  The degree of the resulting polynomial
  56.      * is the index of the last non-null element of the array, or 0 if all elements
  57.      * are null.
  58.      * <p>
  59.      * The constructor makes a copy of the input array and assigns the copy to
  60.      * the coefficients property.</p>
  61.      *
  62.      * @param c Polynomial coefficients.
  63.      * @throws NullArgumentException if {@code c} is {@code null}.
  64.      * @throws MathIllegalArgumentException if {@code c} is empty.
  65.      */
  66.     public PolynomialFunction(double... c)
  67.         throws MathIllegalArgumentException, NullArgumentException {
  68.         super();
  69.         MathUtils.checkNotNull(c);
  70.         int n = c.length;
  71.         if (n == 0) {
  72.             throw new MathIllegalArgumentException(LocalizedCoreFormats.EMPTY_POLYNOMIALS_COEFFICIENTS_ARRAY);
  73.         }
  74.         while ((n > 1) && (c[n - 1] == 0)) {
  75.             --n;
  76.         }
  77.         this.coefficients = new double[n];
  78.         System.arraycopy(c, 0, this.coefficients, 0, n);
  79.     }

  80.     /**
  81.      * Compute the value of the function for the given argument.
  82.      * <p>
  83.      *  The value returned is </p><p>
  84.      *  {@code coefficients[n] * x^n + ... + coefficients[1] * x  + coefficients[0]}
  85.      * </p>
  86.      *
  87.      * @param x Argument for which the function value should be computed.
  88.      * @return the value of the polynomial at the given point.
  89.      *
  90.      * @see org.hipparchus.analysis.UnivariateFunction#value(double)
  91.      */
  92.     @Override
  93.     public double value(double x) {
  94.        return evaluate(coefficients, x);
  95.     }

  96.     /**
  97.      * Returns the degree of the polynomial.
  98.      *
  99.      * @return the degree of the polynomial.
  100.      */
  101.     public int degree() {
  102.         return coefficients.length - 1;
  103.     }

  104.     /**
  105.      * Returns a copy of the coefficients array.
  106.      * <p>
  107.      * Changes made to the returned copy will not affect the coefficients of
  108.      * the polynomial.</p>
  109.      *
  110.      * @return a fresh copy of the coefficients array.
  111.      */
  112.     public double[] getCoefficients() {
  113.         return coefficients.clone();
  114.     }

  115.     /**
  116.      * Uses Horner's Method to evaluate the polynomial with the given coefficients at
  117.      * the argument.
  118.      *
  119.      * @param coefficients Coefficients of the polynomial to evaluate.
  120.      * @param argument Input value.
  121.      * @return the value of the polynomial.
  122.      * @throws MathIllegalArgumentException if {@code coefficients} is empty.
  123.      * @throws NullArgumentException if {@code coefficients} is {@code null}.
  124.      */
  125.     protected static double evaluate(double[] coefficients, double argument)
  126.         throws MathIllegalArgumentException, NullArgumentException {
  127.         MathUtils.checkNotNull(coefficients);
  128.         int n = coefficients.length;
  129.         if (n == 0) {
  130.             throw new MathIllegalArgumentException(LocalizedCoreFormats.EMPTY_POLYNOMIALS_COEFFICIENTS_ARRAY);
  131.         }
  132.         double result = coefficients[n - 1];
  133.         for (int j = n - 2; j >= 0; j--) {
  134.             result = argument * result + coefficients[j];
  135.         }
  136.         return result;
  137.     }


  138.     /** {@inheritDoc}
  139.      * @throws MathIllegalArgumentException if {@code coefficients} is empty.
  140.      * @throws NullArgumentException if {@code coefficients} is {@code null}.
  141.      */
  142.     @Override
  143.     public <T extends Derivative<T>> T value(final T t)
  144.         throws MathIllegalArgumentException, NullArgumentException {
  145.         MathUtils.checkNotNull(coefficients);
  146.         int n = coefficients.length;
  147.         if (n == 0) {
  148.             throw new MathIllegalArgumentException(LocalizedCoreFormats.EMPTY_POLYNOMIALS_COEFFICIENTS_ARRAY);
  149.         }
  150.         T result = t.getField().getZero().add(coefficients[n - 1]);
  151.         for (int j = n - 2; j >= 0; j--) {
  152.             result = result.multiply(t).add(coefficients[j]);
  153.         }
  154.         return result;
  155.     }

  156.     /** {@inheritDoc}
  157.      * @throws MathIllegalArgumentException if {@code coefficients} is empty.
  158.      * @throws NullArgumentException if {@code coefficients} is {@code null}.
  159.      * @since 1.3
  160.      */
  161.     @Override
  162.     public <T extends CalculusFieldElement<T>> T value(final T t)
  163.         throws MathIllegalArgumentException, NullArgumentException {
  164.         MathUtils.checkNotNull(coefficients);
  165.         int n = coefficients.length;
  166.         if (n == 0) {
  167.             throw new MathIllegalArgumentException(LocalizedCoreFormats.EMPTY_POLYNOMIALS_COEFFICIENTS_ARRAY);
  168.         }
  169.         T result = t.getField().getZero().add(coefficients[n - 1]);
  170.         for (int j = n - 2; j >= 0; j--) {
  171.             result = result.multiply(t).add(coefficients[j]);
  172.         }
  173.         return result;
  174.     }

  175.     /**
  176.      * Add a polynomial to the instance.
  177.      *
  178.      * @param p Polynomial to add.
  179.      * @return a new polynomial which is the sum of the instance and {@code p}.
  180.      */
  181.     public PolynomialFunction add(final PolynomialFunction p) {
  182.         // identify the lowest degree polynomial
  183.         final int lowLength  = FastMath.min(coefficients.length, p.coefficients.length);
  184.         final int highLength = FastMath.max(coefficients.length, p.coefficients.length);

  185.         // build the coefficients array
  186.         double[] newCoefficients = new double[highLength];
  187.         for (int i = 0; i < lowLength; ++i) {
  188.             newCoefficients[i] = coefficients[i] + p.coefficients[i];
  189.         }
  190.         System.arraycopy((coefficients.length < p.coefficients.length) ?
  191.                          p.coefficients : coefficients,
  192.                          lowLength,
  193.                          newCoefficients, lowLength,
  194.                          highLength - lowLength);

  195.         return new PolynomialFunction(newCoefficients);
  196.     }

  197.     /**
  198.      * Subtract a polynomial from the instance.
  199.      *
  200.      * @param p Polynomial to subtract.
  201.      * @return a new polynomial which is the instance minus {@code p}.
  202.      */
  203.     public PolynomialFunction subtract(final PolynomialFunction p) {
  204.         // identify the lowest degree polynomial
  205.         int lowLength  = FastMath.min(coefficients.length, p.coefficients.length);
  206.         int highLength = FastMath.max(coefficients.length, p.coefficients.length);

  207.         // build the coefficients array
  208.         double[] newCoefficients = new double[highLength];
  209.         for (int i = 0; i < lowLength; ++i) {
  210.             newCoefficients[i] = coefficients[i] - p.coefficients[i];
  211.         }
  212.         if (coefficients.length < p.coefficients.length) {
  213.             for (int i = lowLength; i < highLength; ++i) {
  214.                 newCoefficients[i] = -p.coefficients[i];
  215.             }
  216.         } else {
  217.             System.arraycopy(coefficients, lowLength, newCoefficients, lowLength,
  218.                              highLength - lowLength);
  219.         }

  220.         return new PolynomialFunction(newCoefficients);
  221.     }

  222.     /**
  223.      * Negate the instance.
  224.      *
  225.      * @return a new polynomial with all coefficients negated
  226.      */
  227.     public PolynomialFunction negate() {
  228.         double[] newCoefficients = new double[coefficients.length];
  229.         for (int i = 0; i < coefficients.length; ++i) {
  230.             newCoefficients[i] = -coefficients[i];
  231.         }
  232.         return new PolynomialFunction(newCoefficients);
  233.     }

  234.     /**
  235.      * Multiply the instance by a polynomial.
  236.      *
  237.      * @param p Polynomial to multiply by.
  238.      * @return a new polynomial equal to this times {@code p}
  239.      */
  240.     public PolynomialFunction multiply(final PolynomialFunction p) {
  241.         double[] newCoefficients = new double[coefficients.length + p.coefficients.length - 1];

  242.         for (int i = 0; i < newCoefficients.length; ++i) {
  243.             newCoefficients[i] = 0.0;
  244.             for (int j = FastMath.max(0, i + 1 - p.coefficients.length);
  245.                  j < FastMath.min(coefficients.length, i + 1);
  246.                  ++j) {
  247.                 newCoefficients[i] += coefficients[j] * p.coefficients[i-j];
  248.             }
  249.         }

  250.         return new PolynomialFunction(newCoefficients);
  251.     }

  252.     /**
  253.      * Returns the coefficients of the derivative of the polynomial with the given coefficients.
  254.      *
  255.      * @param coefficients Coefficients of the polynomial to differentiate.
  256.      * @return the coefficients of the derivative or {@code null} if coefficients has length 1.
  257.      * @throws MathIllegalArgumentException if {@code coefficients} is empty.
  258.      * @throws NullArgumentException if {@code coefficients} is {@code null}.
  259.      */
  260.     protected static double[] differentiate(double[] coefficients)
  261.         throws MathIllegalArgumentException, NullArgumentException {
  262.         MathUtils.checkNotNull(coefficients);
  263.         int n = coefficients.length;
  264.         if (n == 0) {
  265.             throw new MathIllegalArgumentException(LocalizedCoreFormats.EMPTY_POLYNOMIALS_COEFFICIENTS_ARRAY);
  266.         }
  267.         if (n == 1) {
  268.             return new double[]{0};
  269.         }
  270.         double[] result = new double[n - 1];
  271.         for (int i = n - 1; i > 0; i--) {
  272.             result[i - 1] = i * coefficients[i];
  273.         }
  274.         return result;
  275.     }

  276.     /**
  277.      * Returns an anti-derivative of this polynomial, with 0 constant term.
  278.      *
  279.      * @return a polynomial whose derivative has the same coefficients as this polynomial
  280.      */
  281.     public PolynomialFunction antiDerivative() {
  282.         final int d = degree();
  283.         final double[] anti = new double[d + 2];
  284.         anti[0] = 0d;
  285.         for (int i = 1; i <= d + 1; i++) {
  286.             anti[i] = coefficients[i - 1]  / i;
  287.         }
  288.         return new PolynomialFunction(anti);
  289.     }

  290.     /**
  291.      * Returns the definite integral of this polymomial over the given interval.
  292.      * <p>
  293.      * [lower, upper] must describe a finite interval (neither can be infinite
  294.      * and lower must be less than or equal to upper).
  295.      *
  296.      * @param lower lower bound for the integration
  297.      * @param upper upper bound for the integration
  298.      * @return the integral of this polymomial over the given interval
  299.      * @throws MathIllegalArgumentException if the bounds do not describe a finite interval
  300.      */
  301.     public double integrate(final double lower, final double upper) {
  302.         if (Double.isInfinite(lower) || Double.isInfinite(upper)) {
  303.             throw new MathIllegalArgumentException(LocalizedCoreFormats.INFINITE_BOUND);
  304.         }
  305.         if (lower > upper) {
  306.             throw new MathIllegalArgumentException(LocalizedCoreFormats.LOWER_BOUND_NOT_BELOW_UPPER_BOUND);
  307.         }
  308.         final PolynomialFunction anti = antiDerivative();
  309.         return anti.value(upper) - anti.value(lower);
  310.     }

  311.     /**
  312.      * Returns the derivative as a {@link PolynomialFunction}.
  313.      *
  314.      * @return the derivative polynomial.
  315.      */
  316.     public PolynomialFunction polynomialDerivative() {
  317.         return new PolynomialFunction(differentiate(coefficients));
  318.     }

  319.     /**
  320.      * Returns a string representation of the polynomial.
  321.      *
  322.      * <p>The representation is user oriented. Terms are displayed lowest
  323.      * degrees first. The multiplications signs, coefficients equals to
  324.      * one and null terms are not displayed (except if the polynomial is 0,
  325.      * in which case the 0 constant term is displayed). Addition of terms
  326.      * with negative coefficients are replaced by subtraction of terms
  327.      * with positive coefficients except for the first displayed term
  328.      * (i.e. we display <code>-3</code> for a constant negative polynomial,
  329.      * but <code>1 - 3 x + x^2</code> if the negative coefficient is not
  330.      * the first one displayed).</p>
  331.      *
  332.      * @return a string representation of the polynomial.
  333.      */
  334.     @Override
  335.     public String toString() {
  336.         StringBuilder s = new StringBuilder();
  337.         if (coefficients[0] == 0.0) {
  338.             if (coefficients.length == 1) {
  339.                 return "0";
  340.             }
  341.         } else {
  342.             s.append(toString(coefficients[0]));
  343.         }

  344.         for (int i = 1; i < coefficients.length; ++i) {
  345.             if (coefficients[i] != 0) {
  346.                 if (s.length() > 0) {
  347.                     if (coefficients[i] < 0) {
  348.                         s.append(" - ");
  349.                     } else {
  350.                         s.append(" + ");
  351.                     }
  352.                 } else {
  353.                     if (coefficients[i] < 0) {
  354.                         s.append('-');
  355.                     }
  356.                 }

  357.                 double absAi = FastMath.abs(coefficients[i]);
  358.                 if ((absAi - 1) != 0) {
  359.                     s.append(toString(absAi)).append(' ');
  360.                 }

  361.                 s.append('x');
  362.                 if (i > 1) {
  363.                     s.append('^').append(i);
  364.                 }
  365.             }
  366.         }

  367.         return s.toString();
  368.     }

  369.     /**
  370.      * Creates a string representing a coefficient, removing ".0" endings.
  371.      *
  372.      * @param coeff Coefficient.
  373.      * @return a string representation of {@code coeff}.
  374.      */
  375.     private static String toString(double coeff) {
  376.         final String c = Double.toString(coeff);
  377.         if (c.endsWith(".0")) {
  378.             return c.substring(0, c.length() - 2);
  379.         } else {
  380.             return c;
  381.         }
  382.     }

  383.     /** {@inheritDoc} */
  384.     @Override
  385.     public int hashCode() {
  386.         final int prime = 31;
  387.         int result = 1;
  388.         result = prime * result + Arrays.hashCode(coefficients);
  389.         return result;
  390.     }

  391.     /** {@inheritDoc} */
  392.     @Override
  393.     public boolean equals(Object obj) {
  394.         if (this == obj) {
  395.             return true;
  396.         }
  397.         if (!(obj instanceof PolynomialFunction)) {
  398.             return false;
  399.         }
  400.         PolynomialFunction other = (PolynomialFunction) obj;
  401.         return Arrays.equals(coefficients, other.coefficients);
  402.     }

  403.     /**
  404.      * Dedicated parametric polynomial class.
  405.      *
  406.      */
  407.     public static class Parametric implements ParametricUnivariateFunction {

  408.         /** Empty constructor.
  409.          * <p>
  410.          * This constructor is not strictly necessary, but it prevents spurious
  411.          * javadoc warnings with JDK 18 and later.
  412.          * </p>
  413.          * @since 3.0
  414.          */
  415.         public Parametric() { // NOPMD - unnecessary constructor added intentionally to make javadoc happy
  416.             // nothing to do
  417.         }

  418.         /** {@inheritDoc} */
  419.         @Override
  420.         public double[] gradient(double x, double ... parameters) {
  421.             final double[] gradient = new double[parameters.length];
  422.             double xn = 1.0;
  423.             for (int i = 0; i < parameters.length; ++i) {
  424.                 gradient[i] = xn;
  425.                 xn *= x;
  426.             }
  427.             return gradient;
  428.         }

  429.         /** {@inheritDoc} */
  430.         @Override
  431.         public double value(final double x, final double ... parameters)
  432.             throws MathIllegalArgumentException {
  433.             return evaluate(parameters, x);
  434.         }
  435.     }
  436. }