Example usage for org.apache.commons.math.analysis.polynomials PolynomialFunction polynomialDerivative

List of usage examples for org.apache.commons.math.analysis.polynomials PolynomialFunction polynomialDerivative

Introduction

In this page you can find the example usage for org.apache.commons.math.analysis.polynomials PolynomialFunction polynomialDerivative.

Prototype

public PolynomialFunction polynomialDerivative() 

Source Link

Document

Returns the derivative as a PolynomialRealFunction

Usage

From source file:geogebra.common.kernel.implicit.AlgoIntersectImplicitpolys.java

private static int getNearRoots(double[] roots, EquationSolverInterface solver, double epsilon) {
    PolynomialFunction poly = new PolynomialFunction(roots);
    double[] rootsDerivative = poly.polynomialDerivative().getCoefficients();

    int nrRoots = getRoots(roots, solver);
    int nrDeRoots = getRoots(rootsDerivative, solver);
    for (int i = 0; i < nrDeRoots; i++) {
        if (Kernel.isEqual(poly.value(rootsDerivative[i]), 0, epsilon)) {
            if (nrRoots < roots.length) {
                roots[nrRoots++] = rootsDerivative[i];
            }/* w w  w .j  a v  a  2s .  co m*/
        }
    }
    if (nrRoots == 0) {
        //a wild guess, test if the root of the n-1 derivative is a root of the original poly as well
        //works in case of a polynomial with one root of really high multiplicity.
        double[] c = poly.getCoefficients();
        int n = c.length - 1;
        if (n > 0) {
            double x = -c[n - 1] / n / c[n];
            if (Kernel.isEqual(poly.value(x), 0)) {
                roots[0] = x;
                return 1;
            }
        }
    }
    if (nrRoots == 0) {
        PolynomialFunction derivative = poly.polynomialDerivative();
        double x = 0;
        double err = Math.abs(poly.value(x));
        double lastErr = err * 2;
        while (err < lastErr && err > Kernel.STANDARD_PRECISION) {
            double devVal = derivative.value(x);
            if (!Kernel.isZero(devVal))
                x = x - poly.value(x) / devVal;
            else
                break;
            lastErr = err;
            err = Math.abs(poly.value(x));
        }
        if (Kernel.isEqual(poly.value(x), 0, epsilon)) {
            roots[0] = x;
            return 1;
        }
    }
    Arrays.sort(roots, 0, nrRoots);
    return nrRoots;
}