use of org.matheclipse.core.polynomials.ExprPolynomial in project symja_android_library by axkr.
the class CoefficientList method evaluate.
@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
Validate.checkSize(ast, 3);
IExpr expr = F.evalExpandAll(ast.arg1());
ISymbol arg2 = Validate.checkSymbolType(ast, 2);
try {
ExprPolynomialRing ring = new ExprPolynomialRing(F.List(arg2));
ExprPolynomial poly = ring.create(expr);
if (poly.isZero()) {
return F.List();
}
return poly.coefficientList();
} catch (RuntimeException ex) {
throw new WrongArgumentType(ast, expr, 1, "Polynomial expected!");
}
}
use of org.matheclipse.core.polynomials.ExprPolynomial in project symja_android_library by axkr.
the class CoefficientList method coefficientList.
/**
* Get the coefficient list of a univariate polynomial.
*
* @param polynomial
* @param variable
* @return <code>null</code> if the list couldn't be evaluated.
*/
public static double[] coefficientList(IExpr polynomial, final ISymbol variable) throws JASConversionException {
try {
ExprPolynomialRing ring = new ExprPolynomialRing(F.List(variable));
ExprPolynomial poly = ring.create(polynomial);
// PolynomialOld poly = new PolynomialOld(polynomial, (ISymbol) variable);
// if (!poly.isPolynomial()) {
// throw new WrongArgumentType(polynomial, "Polynomial expected!");
// }
IAST list = poly.coefficientList();
int degree = list.size() - 2;
double[] result = new double[degree + 1];
for (int i = 1; i < list.size(); i++) {
ISignedNumber temp = list.get(i).evalSignedNumber();
if (temp != null) {
result[i - 1] = temp.doubleValue();
} else {
return null;
}
}
return result;
} catch (RuntimeException ex) {
throw new WrongArgumentType(polynomial, "Polynomial expected!");
}
}
use of org.matheclipse.core.polynomials.ExprPolynomial in project symja_android_library by axkr.
the class CoefficientRules 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((ISymbol) symbolList.get(i));
}
}
TermOrder termOrder = TermOrderByName.Lexicographic;
try {
if (ast.size() > 3) {
if (ast.arg3() instanceof IStringX) {
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 coefficientRulesModulus(expr, varList, termOrder, option);
}
}
if (MonomialList.USE_JAS_POLYNOMIAL) {
return coefficientRules(expr, varList, termOrder);
} else {
ExprPolynomialRing ring = new ExprPolynomialRing(symbolList, new ExprTermOrder(termOrder.getEvord()));
ExprPolynomial poly = ring.create(expr);
return poly.coefficientRules();
}
} 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 Discriminant method evaluate.
@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
Validate.checkSize(ast, 3);
IExpr arg2 = ast.arg2();
if (!arg2.isSymbol()) {
return F.NIL;
}
IExpr expr = F.evalExpandAll(ast.arg1());
try {
ExprPolynomialRing ring = new ExprPolynomialRing(F.List(arg2));
ExprPolynomial poly = ring.create(expr);
long n = poly.degree();
if (n >= 2L && n <= 5L) {
IAST result = poly.coefficientList();
IAST rules = F.List();
for (int i = 1; i < result.size(); i++) {
rules.append(F.Rule(vars[i - 1], result.get(i)));
}
switch((int) n) {
case 2:
return QUADRATIC.replaceAll(rules);
case 3:
return CUBIC.replaceAll(rules);
case 4:
return QUARTIC.replaceAll(rules);
case 5:
return QUINTIC.replaceAll(rules);
}
}
// coefficient(n);
IExpr fN = poly.leadingBaseCoefficient();
ExprPolynomial polyDiff = poly.derivative();
// http://en.wikipedia.org/wiki/Discriminant#Discriminant_of_a_polynomial
return F.Divide(F.Times(F.Power(F.CN1, (n * (n - 1) / 2)), F.Resultant(poly.getExpr(), polyDiff.getExpr(), arg2)), fN);
} catch (RuntimeException ex) {
throw new WrongArgumentType(ast, expr, 1, "Polynomial expected!");
}
}
use of org.matheclipse.core.polynomials.ExprPolynomial in project symja_android_library by axkr.
the class DSolve method solveSingleODE.
private IExpr solveSingleODE(IExpr equation, IAST uFunction1Arg, IExpr xVar, IAST listOfVariables, IExpr C_1, EvalEngine engine) {
ExprPolynomialRing ring = new ExprPolynomialRing(ExprRingFactory.CONST, listOfVariables, listOfVariables.size() - 1);
if (equation.isAST()) {
IAST eq = ((IAST) equation).clone();
if (!eq.isPlus()) {
// create artificial Plus(...) expression
eq = F.Plus(eq);
}
int j = 1;
IAST[] deriveExpr = null;
while (j < eq.size()) {
IAST[] temp = eq.get(j).isDerivativeAST1();
if (temp != null) {
if (deriveExpr != null) {
// expression
return F.NIL;
}
deriveExpr = temp;
// eliminate deriveExpr from Plus(...) expression
eq.remove(j);
continue;
}
j++;
}
if (deriveExpr != null) {
int order = derivativeOrder(deriveExpr);
if (order < 0) {
return F.NIL;
}
try {
ExprPolynomial poly = ring.create(eq.getOneIdentity(F.C0), false, true);
if (order == 1 && poly.degree() <= 1) {
IAST coeffs = poly.coefficientList();
// degree 0
IExpr q = coeffs.get(1);
IExpr p = F.C0;
if (poly.degree() == 1) {
// degree 1
p = coeffs.get(2);
}
return linearODE(p, q, uFunction1Arg, xVar, C_1, engine);
}
} catch (RuntimeException rex) {
if (Config.SHOW_STACKTRACE) {
rex.printStackTrace();
}
}
}
}
return F.NIL;
}
Aggregations