/** * Get the derivative of the polynomial spline function. * * @return the derivative function. */ public PolynomialSplineFunction polynomialSplineDerivative() { PolynomialFunction derivativePolynomials[] = new PolynomialFunction[n]; for (int i = 0; i < n; i++) { derivativePolynomials[i] = polynomials[i].polynomialDerivative(); } return new PolynomialSplineFunction(knots, derivativePolynomials); }
/** * Returns the derivative as a {@link PolynomialFunction}. * * @return the derivative polynomial. */ public PolynomialFunction polynomialDerivative() { return new PolynomialFunction(differentiate(coefficients)); }
/** * Negate the instance. * * @return a new polynomial with all coefficients negated */ public PolynomialFunction negate() { double[] newCoefficients = new double[coefficients.length]; for (int i = 0; i < coefficients.length; ++i) { newCoefficients[i] = -coefficients[i]; } return new PolynomialFunction(newCoefficients); }
/** * @return the coefficients of the polynomial function. */ protected double[] getCoefficients() { return polynomialFunction.getCoefficients(); } }
/** * Returns the derivative as a {@link UnivariateFunction}. * * @return the derivative function. */ public UnivariateFunction derivative() { return polynomialDerivative(); }
/** {@inheritDoc} */ public double value(final double x, final double ... parameters) throws NoDataException { return PolynomialFunction.evaluate(parameters, x); } }
/** * Evaluate the Newton polynomial using nested multiplication. It is * also called <a href="http://mathworld.wolfram.com/HornersRule.html"> * Horner's Rule</a> and takes O(N) time. * * @param a Coefficients in Newton form formula. * @param c Centers. * @param z Point at which the function value is to be computed. * @return the function value. * @throws NullArgumentException if any argument is {@code null}. * @throws NoDataException if any array has zero length. * @throws DimensionMismatchException if the size difference between * {@code a} and {@code c} is not equal to 1. */ public static double evaluate(double a[], double c[], double z) throws NullArgumentException, DimensionMismatchException, NoDataException { verifyInputArray(a, c); final int n = c.length; double value = a[n]; for (int i = n - 1; i >= 0; i--) { value = a[i] + (z - c[i]) * value; } return value; }
/** * Get the coefficients of the polynomial fitting the weighted data points. * The degree of the fitting polynomial is {@code guess.length - 1}. * * @param guess First guess for the coefficients. They must be sorted in * increasing order of the polynomial's degree. * @return the coefficients of the polynomial that best fits the observed points. * @throws org.apache.commons.math3.exception.ConvergenceException * if the algorithm failed to converge. */ public double[] fit(double[] guess) { return fit(new PolynomialFunction.Parametric(), guess); } }
/** * Create a Hermite polynomial. * <p><a href="http://mathworld.wolfram.com/HermitePolynomial.html">Hermite * polynomials</a> are orthogonal polynomials. * They can be defined by the following recurrence relations:</p><p> * \( * H_0(x) = 1 \\ * H_1(x) = 2x \\ * H_{k+1}(x) = 2x H_k(X) - 2k H_{k-1}(x) * \) * </p> * @param degree degree of the polynomial * @return Hermite polynomial of specified degree */ public static PolynomialFunction createHermitePolynomial(final int degree) { return buildPolynomial(degree, HERMITE_COEFFICIENTS, new RecurrenceCoefficientsGenerator() { /** {@inheritDoc} */ public BigFraction[] generate(int k) { return new BigFraction[] { BigFraction.ZERO, BigFraction.TWO, new BigFraction(2 * k)}; } }); }
/** * Calculate the function value at the given point. * * @param z Point at which the function value is to be computed. * @return the function value. */ public double value(double z) { return evaluate(a, c, z); }
/** * Get the derivative of the polynomial spline function. * * @return the derivative function. */ public UnivariateFunction derivative() { return polynomialSplineDerivative(); }
/** * Calculate the function value at the given point. * * @param z Point at which the function value is to be computed. * @return the function value. * @throws DimensionMismatchException if {@code x} and {@code y} have * different lengths. * @throws org.apache.commons.math3.exception.NonMonotonicSequenceException * if {@code x} is not sorted in strictly increasing order. * @throws NumberIsTooSmallException if the size of {@code x} is less * than 2. */ public double value(double z) { return evaluateInternal(x, y, z); }
/** * Computes an interpolating function for the data set. * * @param x Interpolating points. * @param y Interpolating values. * @return a function which interpolates the data set * @throws DimensionMismatchException if the array lengths are different. * @throws NumberIsTooSmallException if the number of points is less than 2. * @throws NonMonotonicSequenceException if two abscissae have the same * value. */ public PolynomialFunctionLagrangeForm interpolate(double x[], double y[]) throws DimensionMismatchException, NumberIsTooSmallException, NonMonotonicSequenceException { return new PolynomialFunctionLagrangeForm(x, y); } }
/** * Calculate the normal polynomial coefficients given the Newton form. * It also uses nested multiplication but takes O(N^2) time. */ protected void computeCoefficients() { final int n = degree(); coefficients = new double[n+1]; for (int i = 0; i <= n; i++) { coefficients[i] = 0.0; } coefficients[0] = a[n]; for (int i = n-1; i >= 0; i--) { for (int j = n-i; j > 0; j--) { coefficients[j] = coefficients[j-1] - c[i] * coefficients[j]; } coefficients[0] = a[i] - c[i] * coefficients[0]; } coefficientsComputed = true; }
/** * Returns a copy of the coefficients array. * <p> * Changes made to the returned copy will not affect the polynomial.</p> * * @return a fresh copy of the coefficients array. */ public double[] getCoefficients() { if (!coefficientsComputed) { computeCoefficients(); } double[] out = new double[coefficients.length]; System.arraycopy(coefficients, 0, out, 0, coefficients.length); return out; }
/** Create a polynomial from its coefficients. * @param c polynomials coefficients * @return polynomial */ private PolynomialFunction polynomial(double ... c) { return new PolynomialFunction(c); }
/** * Compute the value of the function for the given argument. * <p> * The value returned is </p><p> * {@code coefficients[n] * x^n + ... + coefficients[1] * x + coefficients[0]} * </p> * * @param x Argument for which the function value should be computed. * @return the value of the polynomial at the given point. * @see UnivariateFunction#value(double) */ public double value(double x) { return evaluate(coefficients, x); }
/** * {@inheritDoc} * @since 3.1 */ public DerivativeStructure value(final DerivativeStructure t) { verifyInputArray(a, c); final int n = c.length; DerivativeStructure value = new DerivativeStructure(t.getFreeParameters(), t.getOrder(), a[n]); for (int i = n - 1; i >= 0; i--) { value = t.subtract(c[i]).multiply(value).add(a[i]); } return value; }
/** * Get the coefficients of the polynomial fitting the weighted data points. * The degree of the fitting polynomial is {@code guess.length - 1}. * * @param guess First guess for the coefficients. They must be sorted in * increasing order of the polynomial's degree. * @return the coefficients of the polynomial that best fits the observed points. * @throws org.apache.commons.math3.exception.ConvergenceException * if the algorithm failed to converge. * @since 3.1 */ public double[] fit(double[] guess) { return fit(new PolynomialFunction.Parametric(), guess); } }
/** * Create a Laguerre polynomial. * <p><a href="http://mathworld.wolfram.com/LaguerrePolynomial.html">Laguerre * polynomials</a> are orthogonal polynomials. * They can be defined by the following recurrence relations:</p><p> * \( * L_0(x) = 1 \\ * L_1(x) = 1 - x \\ * (k+1) L_{k+1}(x) = (2k + 1 - x) L_k(x) - k L_{k-1}(x) * \) * </p> * @param degree degree of the polynomial * @return Laguerre polynomial of specified degree */ public static PolynomialFunction createLaguerrePolynomial(final int degree) { return buildPolynomial(degree, LAGUERRE_COEFFICIENTS, new RecurrenceCoefficientsGenerator() { /** {@inheritDoc} */ public BigFraction[] generate(int k) { final int kP1 = k + 1; return new BigFraction[] { new BigFraction(2 * k + 1, kP1), new BigFraction(-1, kP1), new BigFraction(k, kP1)}; } }); }