Java Code Examples for org.apache.commons.math.complex.Complex#getReal()

The following examples show how to use org.apache.commons.math.complex.Complex#getReal() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LaguerreSolver.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Find a real root in the given interval.
 *
 * Despite the bracketing condition, the root returned by
 * {@link LaguerreSolver.ComplexSolver#solve(Complex[],Complex)} may
 * not be a real zero inside {@code [min, max]}.
 * For example, <code>p(x) = x<sup>3</sup> + 1,</code>
 * with {@code min = -2}, {@code max = 2}, {@code initial = 0}.
 * When it occurs, this code calls
 * {@link LaguerreSolver.ComplexSolver#solveAll(Complex[],Complex)}
 * in order to obtain all roots and picks up one real root.
 *
 * @param lo Lower bound of the search interval.
 * @param hi Higher bound of the search interval.
 * @param fLo Function value at the lower bound of the search interval.
 * @param fHi Function value at the higher bound of the search interval.
 * @return the point at which the function value is zero.
 */
public double laguerre(double lo, double hi,
                       double fLo, double fHi) {
    double coefficients[] = getCoefficients();
    Complex c[] = new Complex[coefficients.length];
    for (int i = 0; i < coefficients.length; i++) {
        c[i] = new Complex(coefficients[i], 0);
    }
    Complex initial = new Complex(0.5 * (lo + hi), 0);
    Complex z = complexSolver.solve(c, initial);
    if (complexSolver.isRoot(lo, hi, z)) {
        return z.getReal();
    } else {
        double r = Double.NaN;
        // Solve all roots and select the one we are seeking.
        Complex[] root = complexSolver.solveAll(c, initial);
        for (int i = 0; i < root.length; i++) {
            if (complexSolver.isRoot(lo, hi, root[i])) {
                r = root[i].getReal();
                break;
            }
        }
        return r;
    }
}
 
Example 2
Source File: LaguerreSolver.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Find a real root in the given interval.
 *
 * Despite the bracketing condition, the root returned by
 * {@link LaguerreSolver.ComplexSolver#solve(Complex[],Complex)} may
 * not be a real zero inside {@code [min, max]}.
 * For example, <code>p(x) = x<sup>3</sup> + 1,</code>
 * with {@code min = -2}, {@code max = 2}, {@code initial = 0}.
 * When it occurs, this code calls
 * {@link LaguerreSolver.ComplexSolver#solveAll(Complex[],Complex)}
 * in order to obtain all roots and picks up one real root.
 *
 * @param lo Lower bound of the search interval.
 * @param hi Higher bound of the search interval.
 * @param fLo Function value at the lower bound of the search interval.
 * @param fHi Function value at the higher bound of the search interval.
 * @return the point at which the function value is zero.
 */
public double laguerre(double lo, double hi,
                       double fLo, double fHi) {
    double coefficients[] = getCoefficients();
    Complex c[] = new Complex[coefficients.length];
    for (int i = 0; i < coefficients.length; i++) {
        c[i] = new Complex(coefficients[i], 0);
    }
    Complex initial = new Complex(0.5 * (lo + hi), 0);
    Complex z = complexSolver.solve(c, initial);
    if (complexSolver.isRoot(lo, hi, z)) {
        return z.getReal();
    } else {
        double r = Double.NaN;
        // Solve all roots and select the one we are seeking.
        Complex[] root = complexSolver.solveAll(c, initial);
        for (int i = 0; i < root.length; i++) {
            if (complexSolver.isRoot(lo, hi, root[i])) {
                r = root[i].getReal();
                break;
            }
        }
        return r;
    }
}
 
Example 3
Source File: LaguerreSolver.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Find a real root in the given interval.
 *
 * Despite the bracketing condition, the root returned by
 * {@link LaguerreSolver.ComplexSolver#solve(Complex[],Complex)} may
 * not be a real zero inside {@code [min, max]}.
 * For example, <code>p(x) = x<sup>3</sup> + 1,</code>
 * with {@code min = -2}, {@code max = 2}, {@code initial = 0}.
 * When it occurs, this code calls
 * {@link LaguerreSolver.ComplexSolver#solveAll(Complex[],Complex)}
 * in order to obtain all roots and picks up one real root.
 *
 * @param lo Lower bound of the search interval.
 * @param hi Higher bound of the search interval.
 * @param fLo Function value at the lower bound of the search interval.
 * @param fHi Function value at the higher bound of the search interval.
 * @return the point at which the function value is zero.
 */
public double laguerre(double lo, double hi,
                       double fLo, double fHi) {
    double coefficients[] = getCoefficients();
    Complex c[] = new Complex[coefficients.length];
    for (int i = 0; i < coefficients.length; i++) {
        c[i] = new Complex(coefficients[i], 0);
    }
    Complex initial = new Complex(0.5 * (lo + hi), 0);
    Complex z = complexSolver.solve(c, initial);
    if (complexSolver.isRoot(lo, hi, z)) {
        return z.getReal();
    } else {
        double r = Double.NaN;
        // Solve all roots and select the one we are seeking.
        Complex[] root = complexSolver.solveAll(c, initial);
        for (int i = 0; i < root.length; i++) {
            if (complexSolver.isRoot(lo, hi, root[i])) {
                r = root[i].getReal();
                break;
            }
        }
        return r;
    }
}