use of org.matheclipse.core.polynomials.ExprPolynomial in project symja_android_library by axkr.
the class Coefficient method evaluate.
@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
Validate.checkRange(ast, 3, 4);
IExpr arg2 = ast.arg2();
// list of variable expressions extracted from the second argument
IAST listOfVariables = null;
// array of corresponding exponents for the list of variables
long[] exponents = null;
if (arg2.isTimes()) {
// Times(x, y^a,...)
IAST arg2AST = (IAST) arg2;
VariablesSet eVar = new VariablesSet(arg2AST);
listOfVariables = eVar.getVarList();
exponents = new long[listOfVariables.size() - 1];
for (int i = 0; i < exponents.length; i++) {
exponents[i] = 0L;
}
for (int i = 1; i < arg2AST.size(); i++) {
long value = 1L;
IExpr a1 = arg2AST.get(i);
if (arg2AST.get(i).isPower() && arg2AST.get(i).getAt(2).isInteger()) {
a1 = arg2AST.get(i).getAt(1);
IInteger ii = (IInteger) arg2AST.get(i).getAt(2);
try {
value = ii.toLong();
} catch (ArithmeticException ae) {
return F.NIL;
}
}
if (!setExponent(listOfVariables, a1, exponents, value)) {
return F.NIL;
}
}
} else {
listOfVariables = F.List();
listOfVariables.append(arg2);
exponents = new long[1];
exponents[0] = 1;
}
try {
long n = 1;
if (ast.isAST3()) {
if (ast.arg3().isNegativeInfinity()) {
return F.C0;
}
n = Validate.checkLongType(ast.arg3());
for (int i = 0; i < exponents.length; i++) {
exponents[i] *= n;
}
}
ExpVectorLong expArr = new ExpVectorLong(exponents);
IExpr expr = F.evalExpandAll(ast.arg1());
ExprPolynomialRing ring = new ExprPolynomialRing(ExprRingFactory.CONST, listOfVariables, listOfVariables.size() - 1);
ExprPolynomial poly = ring.create(expr, true, true);
return poly.coefficient(expArr);
} catch (RuntimeException ae) {
if (Config.DEBUG) {
ae.printStackTrace();
}
return F.C0;
}
}
use of org.matheclipse.core.polynomials.ExprPolynomial in project symja_android_library by axkr.
the class Roots method rootsOfQuadraticExprPolynomial.
/**
* Solve a polynomial with degree <= 2.
*
* @param expr
* @param varList
* @return <code>F.NIL</code> if no evaluation was possible.
*/
private static IAST rootsOfQuadraticExprPolynomial(final IExpr expr, IAST varList) {
IAST result = F.NIL;
try {
// try to generate a common expression polynomial
ExprPolynomialRing ring = new ExprPolynomialRing(ExprRingFactory.CONST, varList);
ExprPolynomial ePoly = ring.create(expr, false, false);
ePoly = ePoly.multiplyByMinimumNegativeExponents();
result = rootsOfQuadraticPolynomial(ePoly);
if (result.isPresent() && expr.isNumericMode()) {
for (int i = 1; i < result.size(); i++) {
result.set(i, F.chopExpr(result.get(i), Config.DEFAULT_ROOTS_CHOP_DELTA));
}
}
} catch (JASConversionException e2) {
if (Config.SHOW_STACKTRACE) {
e2.printStackTrace();
}
}
return result;
}
use of org.matheclipse.core.polynomials.ExprPolynomial in project symja_android_library by axkr.
the class Roots method rootsOfExprPolynomial.
private static IAST rootsOfExprPolynomial(final IExpr expr, IAST varList, boolean rootsOfQuartic) {
IAST result = F.NIL;
try {
// try to generate a common expression polynomial
ExprPolynomialRing ring = new ExprPolynomialRing(ExprRingFactory.CONST, varList);
ExprPolynomial ePoly = ring.create(expr, false, false);
ePoly = ePoly.multiplyByMinimumNegativeExponents();
if (ePoly.degree(0) >= 3) {
result = unitPolynomial(ePoly.degree(0), ePoly);
if (result.isPresent()) {
result = QuarticSolver.createSet(result);
return result;
}
}
if (!rootsOfQuartic && ePoly.degree(0) > 2) {
return F.NIL;
}
result = rootsOfQuarticPolynomial(ePoly);
if (result.isPresent()) {
if (expr.isNumericMode()) {
for (int i = 1; i < result.size(); i++) {
result.set(i, F.chopExpr(result.get(i), Config.DEFAULT_ROOTS_CHOP_DELTA));
}
}
return result;
}
} catch (JASConversionException e2) {
if (Config.SHOW_STACKTRACE) {
e2.printStackTrace();
}
}
return F.NIL;
}
use of org.matheclipse.core.polynomials.ExprPolynomial in project symja_android_library by axkr.
the class MonomialList method evaluate.
@Override
public IExpr evaluate(final IAST ast, final EvalEngine engine) {
Validate.checkRange(ast, 2, 5);
IExpr expr = F.evalExpandAll(ast.arg1());
VariablesSet eVar;
IAST symbolList = F.List();
List<IExpr> varList;
if (ast.isAST1()) {
// extract all variables from the polynomial expression
eVar = new VariablesSet(ast.arg1());
eVar.appendToList(symbolList.args());
varList = eVar.getArrayList();
} else {
symbolList = Validate.checkSymbolOrSymbolList(ast, 2);
varList = new ArrayList<IExpr>(symbolList.size() - 1);
for (int i = 1; i < symbolList.size(); i++) {
varList.add(symbolList.get(i));
}
}
TermOrder termOrder = TermOrderByName.Lexicographic;
try {
if (ast.size() > 3) {
if (ast.arg3() instanceof IStringX) {
// NegativeLexicographic
String orderStr = ast.arg3().toString();
termOrder = Options.getMonomialOrder(orderStr, termOrder);
}
final Options options = new Options(ast.topHead(), ast, 2, engine);
IExpr option = options.getOption("Modulus");
if (option.isSignedNumber()) {
return monomialListModulus(expr, varList, termOrder, option);
}
}
if (USE_JAS_POLYNOMIAL) {
return monomialList(expr, varList, termOrder);
} else {
ExprPolynomialRing ring = new ExprPolynomialRing(symbolList, new ExprTermOrder(termOrder.getEvord()));
ExprPolynomial poly = ring.create(expr);
return poly.monomialList();
}
} catch (JASConversionException jce) {
// toInt() conversion failed
if (Config.DEBUG) {
jce.printStackTrace();
}
}
return F.NIL;
}
use of org.matheclipse.core.polynomials.ExprPolynomial in project symja_android_library by axkr.
the class Limit method plusLimit.
private static IExpr plusLimit(final IAST arg1, LimitData data) {
// Limit[a_+b_+c_,sym->lim] ->
// Limit[a,sym->lim]+Limit[b,sym->lim]+Limit[c,sym->lim]
// IAST rule = data.getRule();
IExpr limit = data.getLimitValue();
if (limit.isInfinity() || limit.isNegativeInfinity()) {
ISymbol symbol = data.getSymbol();
try {
ExprPolynomialRing ring = new ExprPolynomialRing(symbol);
ExprPolynomial poly = ring.create(arg1);
IExpr coeff = poly.leadingBaseCoefficient();
long oddDegree = poly.degree() % 2;
if (oddDegree == 1) {
// odd degree
return data.limit(F.Times(coeff, limit));
}
// even degree
return data.limit(F.Times(coeff, F.CInfinity));
} catch (RuntimeException e) {
if (Config.DEBUG) {
e.printStackTrace();
}
}
}
return data.mapLimit(arg1);
}
Aggregations