Search in sources :

Example 1 with LaguerreSolver

use of org.hipparchus.analysis.solvers.LaguerreSolver in project symja_android_library by axkr.

the class NRoots method roots.

public static IAST roots(final IExpr arg1, IAST variables, EvalEngine engine) {
    if (variables.size() != 2) {
        // factor only possible for univariate polynomials
        engine.printMessage("NRoots: factorization only possible for univariate polynomials");
        return F.NIL;
    }
    IExpr expr = evalExpandAll(arg1);
    ISymbol sym = (ISymbol) variables.arg1();
    double[] coefficients = Expr2Object.toPolynomial(expr, sym);
    if (coefficients != null) {
        LaguerreSolver solver = new LaguerreSolver(Config.DEFAULT_ROOTS_CHOP_DELTA);
        Complex[] roots = solver.solveAllComplex(coefficients, 0);
        return Object2Expr.convertComplex(roots);
    }
    IExpr denom = F.C1;
    if (expr.isAST()) {
        expr = Algebra.together((IAST) expr);
        // split expr into numerator and denominator
        denom = engine.evaluate(F.Denominator(expr));
        if (!denom.isOne()) {
            // search roots for the numerator expression
            expr = engine.evaluate(F.Numerator(expr));
        }
    }
    return rootsOfVariable(expr, denom);
}
Also used : ISymbol(org.matheclipse.core.interfaces.ISymbol) LaguerreSolver(org.hipparchus.analysis.solvers.LaguerreSolver) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST) Complex(org.hipparchus.complex.Complex)

Example 2 with LaguerreSolver

use of org.hipparchus.analysis.solvers.LaguerreSolver in project symja_android_library by axkr.

the class RootsFunctions method roots.

public static IAST roots(final IExpr arg1, IAST variables, EvalEngine engine) {
    if (variables.size() != 2) {
        // factor only possible for univariate polynomials
        LOGGER.log(engine.getLogLevel(), "NRoots: factorization only possible for univariate polynomials");
        return F.NIL;
    }
    IExpr expr = evalExpandAll(arg1, engine);
    IExpr variable = variables.arg1();
    double[] coefficients = Expr2Object.toPolynomial(expr, variable);
    if (coefficients != null) {
        try {
            IASTMutable list;
            if (coefficients.length <= 4) {
                IASTAppendable p = F.PlusAlloc(coefficients.length);
                for (int i = 0; i < coefficients.length; i++) {
                    p.append(F.Times(F.num(coefficients[i]), F.Power(variable, i)));
                }
                expr = engine.evaluate(p);
                list = QuarticSolver.solve(p, variables.arg1());
                for (int i = 1; i < list.size(); i++) {
                    expr = engine.evaluate(list.get(i));
                    if (expr.isInexactNumber()) {
                        list.set(i, F.chopNumber((INumber) expr, Config.DEFAULT_ROOTS_CHOP_DELTA));
                    }
                }
            } else {
                LaguerreSolver solver = new LaguerreSolver(Config.DEFAULT_ROOTS_CHOP_DELTA);
                // see https://github.com/Hipparchus-Math/hipparchus/issues/177 for initial value
                org.hipparchus.complex.Complex[] roots = solver.solveAllComplex(coefficients, 1.0);
                list = Object2Expr.convertComplex(true, roots);
            }
            EvalAttributes.sort(list);
            return list;
        } catch (org.hipparchus.exception.MathRuntimeException mrex) {
            LOGGER.debug("RootsFunctions.roots() failed", mrex);
            return F.NIL;
        }
    }
    IExpr denom = F.C1;
    if (expr.isAST()) {
        expr = Algebra.together((IAST) expr, engine);
        // split expr into numerator and denominator
        denom = engine.evaluate(F.Denominator(expr));
        if (!denom.isOne()) {
            // search roots for the numerator expression
            expr = engine.evaluate(F.Numerator(expr));
        }
    }
    return rootsOfVariable(expr, denom);
}
Also used : LaguerreSolver(org.hipparchus.analysis.solvers.LaguerreSolver) IASTMutable(org.matheclipse.core.interfaces.IASTMutable) Complex(edu.jas.poly.Complex) IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) INumber(org.matheclipse.core.interfaces.INumber) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST)

Aggregations

LaguerreSolver (org.hipparchus.analysis.solvers.LaguerreSolver)2 IAST (org.matheclipse.core.interfaces.IAST)2 IExpr (org.matheclipse.core.interfaces.IExpr)2 Complex (edu.jas.poly.Complex)1 Complex (org.hipparchus.complex.Complex)1 IASTAppendable (org.matheclipse.core.interfaces.IASTAppendable)1 IASTMutable (org.matheclipse.core.interfaces.IASTMutable)1 INumber (org.matheclipse.core.interfaces.INumber)1 ISymbol (org.matheclipse.core.interfaces.ISymbol)1