Example usage for org.apache.commons.math.analysis.solvers LaguerreSolver LaguerreSolver

List of usage examples for org.apache.commons.math.analysis.solvers LaguerreSolver LaguerreSolver

Introduction

In this page you can find the example usage for org.apache.commons.math.analysis.solvers LaguerreSolver LaguerreSolver.

Prototype

@Deprecated
public LaguerreSolver() 

Source Link

Document

Construct a solver.

Usage

From source file:geogebra.common.kernel.EquationSolver.java

/**
 * Calculates all roots of a polynomial given by eqn using Laguerres method.
 * Polishes roots found. The roots are stored in eqn again.
 * /* w w  w.  ja v  a 2  s  . c o m*/
 * @param eqn
 *            coefficients of polynomial
 */
@SuppressWarnings("deprecation")
private int laguerreAll(double[] eqn) {
    // for fast evaluation of polynomial (used for root polishing)
    PolyFunction polyFunc = new PolyFunction(eqn);
    PolyFunction derivFunc = polyFunc.getDerivative();

    /*
     * double estx = 0; try { estx = rootPolisher.newtonRaphson(polyFunc,
     * LAGUERRE_START); Application.debug("newton estx: " + estx); if
     * (Double.isNaN(estx)) { estx = LAGUERRE_START;
     * Application.debug("corrected estx: " + estx); } } catch (Exception e)
     * {}
     */

    // calc roots with Laguerre method

    /*
     * old code using Flanagan library //ComplexPoly poly = new
     * ComplexPoly(eqn); //Complex [] complexRoots = poly.roots(false, new
     * Complex(LAGUERRE_START, 0)); // don't polish here
     */

    Complex[] complexRoots = null;
    try {
        if (laguerreSolver == null) {
            laguerreSolver = new LaguerreSolver();
        }
        complexRoots = laguerreSolver.solveAll(eqn, LAGUERRE_START);
    } catch (Exception e) {
        System.err.println("EquationSolver.LaguerreSolver: " + e.getLocalizedMessage());
    }

    if (complexRoots == null)
        complexRoots = new Complex[0];

    // sort complexRoots by real part into laguerreRoots
    double[] laguerreRoots = new double[complexRoots.length];
    for (int i = 0; i < laguerreRoots.length; i++) {
        laguerreRoots[i] = complexRoots[i].getReal();
    }
    Arrays.sort(laguerreRoots);

    // get the roots from Laguerre method
    int realRoots = 0;
    double root;

    for (int i = 0; i < laguerreRoots.length; i++) {
        // System.out.println("laguerreRoots[" + i + "] = " +
        // laguerreRoots[i]);

        // let's polish all complex roots to get all real roots
        root = laguerreRoots[i];

        // check if root is bounded in intervall [root-eps, root+eps]
        double left = i == 0 ? root - 1 : (root + laguerreRoots[i - 1]) / 2;
        double right = i == laguerreRoots.length - 1 ? root + 1 : (root + laguerreRoots[i + 1]) / 2;
        double f_left = polyFunc.evaluate(left);
        double f_right = polyFunc.evaluate(right);
        boolean bounded = f_left * f_right < 0.0;

        try {
            if (bounded) {
                if (rootFinderBrent == null) {
                    UnivariateRealSolverFactory fact = UnivariateRealSolverFactory.newInstance();
                    rootFinderBrent = fact.newBrentSolver();
                }

                // small f'(root): don't go too far from our laguerre root !
                double brentRoot = rootFinderBrent.solve(new RealRootAdapter(polyFunc), left, right, root);
                if (Math.abs(polyFunc.evaluate(brentRoot)) < Math.abs(polyFunc.evaluate(root))) {
                    root = brentRoot;
                }
                // System.out.println("Polish bisectNewtonRaphson: " +
                // root);
            } else {
                if (rootFinderNewton == null) {
                    UnivariateRealSolverFactory fact = UnivariateRealSolverFactory.newInstance();
                    rootFinderNewton = fact.newNewtonSolver();
                }

                // the root is not bounded: give Mr. Newton a chance
                double newtonRoot = rootFinderNewton.solve(new RealRootDerivAdapter(polyFunc), left, right,
                        root);
                if (Math.abs(polyFunc.evaluate(newtonRoot)) < Math.abs(polyFunc.evaluate(root))) {
                    root = newtonRoot;
                }
                // System.out.println("Polish newtonRaphson: " + root);
            }
        } catch (Exception e) {
            // Application.debug("Polish FAILED: ");
            // polishing failed: maybe we have an extremum here
            // try to find a local extremum
            try {
                if (rootFinderBrent == null) {
                    UnivariateRealSolverFactory fact = UnivariateRealSolverFactory.newInstance();
                    rootFinderBrent = fact.newBrentSolver();
                }

                double brentRoot = rootFinderBrent.solve(new RealRootAdapter(derivFunc), left, right);
                if (Math.abs(polyFunc.evaluate(brentRoot)) < Math.abs(polyFunc.evaluate(root))) {
                    root = brentRoot;
                }
                // System.out.println("    find extremum successfull: " +
                // root);
            } catch (Exception ex) {
                App.debug(ex.getMessage());
            }
        }

        // check if the found root is really ok
        double[] val = polyFunc.evaluateDerivFunc(root); // get f(root) and
        // f'(root)
        double error = Math.abs(val[0]); // | f(root) |
        double slope = Math.abs(val[1]);

        boolean success;
        if (slope < 1)
            success = error < LAGUERRE_EPS;
        else
            success = error < LAGUERRE_EPS * slope;

        if (success) {
            // Application.debug("FOUND ROOT: " + root);
            eqn[realRoots] = root;
            realRoots++;
        } else {
            // Application.debug("no root: " + root + ", error " + error);
        }
    }
    return realRoots;
}

From source file:geogebra.kernel.EquationSolver.java

/**
 * Calculates all roots of a polynomial given by eqn using Laguerres method.
 * Polishes roots found. The roots are stored in eqn again.
 * @param eqn: coefficients of polynomial    
 *//*  ww w  .  jav  a2s . c om*/
private int laguerreAll(double[] eqn) {
    // for fast evaluation of polynomial (used for root polishing)
    PolyFunction polyFunc = new PolyFunction(eqn);
    PolyFunction derivFunc = polyFunc.getDerivative();

    /*
    double estx = 0;      
    try {
       estx = rootPolisher.newtonRaphson(polyFunc, LAGUERRE_START);   
       Application.debug("newton estx: " + estx);
       if (Double.isNaN(estx)) {
    estx = LAGUERRE_START;
    Application.debug("corrected estx: " + estx);
       }
    } catch (Exception e) {}
    */

    // calc roots with Laguerre method

    /* old code using Flanagan library
    //ComplexPoly poly = new ComplexPoly(eqn);   
    //Complex [] complexRoots = poly.roots(false, new Complex(LAGUERRE_START, 0)); // don't polish here 
    */

    Complex[] complexRoots = null;
    try {
        if (laguerreSolver == null) {
            laguerreSolver = new LaguerreSolver();
        }
        complexRoots = laguerreSolver.solveAll(eqn, LAGUERRE_START);
    } catch (Exception e) {
        System.err.println("EquationSolver.LaguerreSolver: " + e.getLocalizedMessage());
    }

    if (complexRoots == null)
        complexRoots = new Complex[0];

    // sort complexRoots by real part into laguerreRoots
    double[] laguerreRoots = new double[complexRoots.length];
    for (int i = 0; i < laguerreRoots.length; i++) {
        laguerreRoots[i] = complexRoots[i].getReal();
    }
    Arrays.sort(laguerreRoots);

    // get the roots from Laguerre method
    int realRoots = 0;
    double root;

    for (int i = 0; i < laguerreRoots.length; i++) {
        //System.out.println("laguerreRoots[" + i + "] = " + laguerreRoots[i]);   

        // let's polish all complex roots to get all real roots
        root = laguerreRoots[i];

        // check if root is bounded in intervall [root-eps, root+eps]
        double left = i == 0 ? root - 1 : (root + laguerreRoots[i - 1]) / 2;
        double right = i == laguerreRoots.length - 1 ? root + 1 : (root + laguerreRoots[i + 1]) / 2;
        double f_left = polyFunc.evaluate(left);
        double f_right = polyFunc.evaluate(right);
        boolean bounded = f_left * f_right < 0.0;

        try {
            if (bounded) {
                if (rootFinderBrent == null) {
                    UnivariateRealSolverFactory fact = UnivariateRealSolverFactory.newInstance();
                    rootFinderBrent = fact.newBrentSolver();
                }

                //   small f'(root): don't go too far from our laguerre root !   
                double brentRoot = rootFinderBrent.solve(new RealRootAdapter(polyFunc), left, right, root);
                if (Math.abs(polyFunc.evaluate(brentRoot)) < Math.abs(polyFunc.evaluate(root))) {
                    root = brentRoot;
                }
                //System.out.println("Polish bisectNewtonRaphson: " + root);
            } else {
                if (rootFinderNewton == null) {
                    UnivariateRealSolverFactory fact = UnivariateRealSolverFactory.newInstance();
                    rootFinderNewton = fact.newNewtonSolver();
                }

                // the root is not bounded: give Mr. Newton a chance
                double newtonRoot = rootFinderNewton.solve(new RealRootDerivAdapter(polyFunc), left, right,
                        root);
                if (Math.abs(polyFunc.evaluate(newtonRoot)) < Math.abs(polyFunc.evaluate(root))) {
                    root = newtonRoot;
                }
                //System.out.println("Polish newtonRaphson: " + root);
            }
        } catch (Exception e) {
            // Application.debug("Polish FAILED: ");
            // polishing failed: maybe we have an extremum here
            // try to find a local extremum
            try {
                if (rootFinderBrent == null) {
                    UnivariateRealSolverFactory fact = UnivariateRealSolverFactory.newInstance();
                    rootFinderBrent = fact.newBrentSolver();
                }

                double brentRoot = rootFinderBrent.solve(new RealRootAdapter(derivFunc), left, right);
                if (Math.abs(polyFunc.evaluate(brentRoot)) < Math.abs(polyFunc.evaluate(root))) {
                    root = brentRoot;
                }
                //System.out.println("    find extremum successfull: " + root);
            } catch (Exception ex) {
                Application.debug(ex.getMessage());
            }
        }

        //   check if the found root is really ok                                     
        double[] val = polyFunc.evaluateDerivFunc(root); // get f(root) and f'(root)
        double error = Math.abs(val[0]); // | f(root) |
        double slope = Math.abs(val[1]);

        boolean success;
        if (slope < 1)
            success = error < LAGUERRE_EPS;
        else
            success = error < LAGUERRE_EPS * slope;

        if (success) {
            // Application.debug("FOUND ROOT: " + root);
            eqn[realRoots] = root;
            realRoots++;
        } else {
            //Application.debug("no root: " + root + ", error " + error);
        }
    }
    return realRoots;
}

From source file:geogebra.common.kernel.EquationSolver.java

/**
 * Calculates all roots of a polynomial given by eqn using Laguerres method.
 * Polishes roots found. The roots are stored in eqn again.
 *//*ww w  . j  a va  2  s .c  om*/
@SuppressWarnings("deprecation")
private int laguerreAllComplex(double[] real, double[] complex) {

    Complex[] complexRoots = null;
    try {
        if (laguerreSolver == null) {
            laguerreSolver = new LaguerreSolver();
        }
        complexRoots = laguerreSolver.solveAll(real, LAGUERRE_START);
    } catch (Exception e) {
        App.debug("Problem solving with LaguerreSolver" + e.getLocalizedMessage());
        return 0;
    }

    // sort by real part & remove duplicates

    TreeSet<Complex> sortedSet = new TreeSet<Complex>(getComparatorReal());

    for (int i = 0; i < complexRoots.length; i++) {
        sortedSet.add(complexRoots[i]);
    }

    int roots = 0;
    Complex temp;
    Iterator<Complex> iterator = sortedSet.iterator();
    while (iterator.hasNext()) {
        temp = iterator.next();
        real[roots] = temp.getReal();
        complex[roots] = temp.getImaginary();
        roots++;
    }

    return roots;
}

From source file:geogebra.kernel.EquationSolver.java

/**
 * Calculates all roots of a polynomial given by eqn using Laguerres method.
 * Polishes roots found. The roots are stored in eqn again.
 * @param eqn: coefficients of polynomial    
 *///from   ww  w .java  2  s  . c om
private int laguerreAllComplex(double[] real, double[] complex) {

    Complex[] complexRoots = null;
    try {
        if (laguerreSolver == null) {
            laguerreSolver = new LaguerreSolver();
        }
        complexRoots = laguerreSolver.solveAll(real, LAGUERRE_START);
    } catch (Exception e) {
        Application.debug("Problem solving with LaguerreSolver" + e.getLocalizedMessage());
        return 0;
    }

    // sort by real part & remove duplicates

    TreeSet<Complex> sortedSet = new TreeSet<Complex>(getComparatorReal());

    for (int i = 0; i < complexRoots.length; i++) {
        sortedSet.add(complexRoots[i]);
    }

    int roots = 0;
    Complex temp;
    Iterator<Complex> iterator = sortedSet.iterator();
    while (iterator.hasNext()) {
        temp = iterator.next();
        real[roots] = temp.getReal();
        complex[roots] = temp.getImaginary();
        roots++;
    }

    return roots;
}