Example usage for org.apache.commons.math3.analysis.solvers LaguerreSolver solve

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

Introduction

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

Prototype

double solve(int maxEval, FUNC f, double min, double max);

Source Link

Document

Solve for a zero root in the given interval.

Usage

From source file:curveavg.SolverTest.java

@Test
public void testQuinticZero_NewtonRaphsonFailureCase() {

    // Solve using NewtonRaphson and count the number of results
    float c[] = { -38.764412f, 76.10117f, -56.993206f, 28.603401f, -10.2824955f, 1.0f };
    MedialAxisTransform.SolverResult res = MedialAxisTransform.solveQuinticNewtonRaphson(c[0], c[1], c[2], c[3],
            c[4], c[5]);//from w  w w  .  j a  v a 2  s  .c o  m
    int rootsNR = 0;
    for (int i = 0; i < res.im.length; i++) {
        if (Math.abs(res.im[i]) < 1e-3)
            rootsNR++;
    }
    System.out.println("# NR roots: " + rootsNR);

    // Solve using NewtonRaphson and count the number of results
    MedialAxisTransform.SolverResult res2 = MedialAxisTransform.solveQuinticLaguerre(c[0], c[1], c[2], c[3],
            c[4], c[5]);
    int rootsL = 0;
    for (int i = 0; i < res.im.length; i++) {
        if (Math.abs(res2.im[i]) < 1e-3)
            rootsL++;
    }
    System.out.println("# L roots: " + rootsL);

    // Solve using Laguerre
    double dt = 0.01;
    double coefficients[] = new double[6];
    for (int i = 0; i < 6; i++)
        coefficients[i] = c[5 - i];
    PolynomialFunction f = new PolynomialFunction(coefficients);
    LaguerreSolver solver = new LaguerreSolver();
    for (double t = 0; t <= (1.0 - dt); t += dt) {

        // Check if there is a sign-crossing between the two times
        double t2 = t + dt;
        double f1 = f.value(t);
        double f2 = f.value(t2);
        if (Math.signum(f1) == Math.signum(f2))
            continue;

        // Compute using Laguerre
        double result = solver.solve(100, f, t, t2);
        System.out.println("Result: " + result);
    }

}

From source file:curveavg.MedialAxisTransform.java

/**
 * Uses the Laguerre algorithm iteratively to span the range [0,1] and find
 * all the real roots.//from  w  w  w .j av  a  2s.c o  m
 */
public static SolverResult solveQuinticLaguerre(float a, float b, float c, float d, float e, float f) {

    // Initialize the result
    //            System.out.println("quintic params: " + a + ", "+ b + ", "+ c + ", "+ d + ", "+ e + ", "+ f);
    SolverResult res = new SolverResult();
    res.re = new float[6];
    res.im = new float[6];
    for (int i = 0; i < 6; i++) {
        res.im[i] = 1.0f;
    }

    // Create the quintic function and the solver
    double coefficients[] = { f, e, d, c, b, a };
    PolynomialFunction qf = new PolynomialFunction(coefficients);
    LaguerreSolver solver = new LaguerreSolver();

    // Iteratively call the NewtonRaphson solution until no solution 
    // exists
    double minBound = 0.0;
    double sol;
    int root_idx = 0;
    final double dt = 0.01;
    for (double t = -dt; t < 1.0; t += dt) {

        // Check if there is a sign-crossing between the two times
        double t2 = t + dt;
        double f1 = qf.value(t);
        double f2 = qf.value(t2);
        if (Math.signum(f1) == Math.signum(f2)) {
            continue;
        }

        // Attempt to get the solution
        try {
            sol = solver.solve(100, qf, t, t2);
        } catch (TooManyEvaluationsException | NumberIsTooLargeException exc) {
            break;
        }

        // Save the root and update the minimum bound
        res.re[root_idx] = (float) sol;
        res.im[root_idx] = 0.0f;
        minBound = sol + 1e-3;
        root_idx++;
    }

    //            System.out.println("#quintic roots: " + root_idx);
    return res;
}