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);
}
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);
}
Aggregations