use of org.matheclipse.core.eval.exception.WrongArgumentType 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.eval.exception.WrongArgumentType in project symja_android_library by axkr.
the class Eliminate method checkEquations.
/**
* Check if the argument at the given position is an equation (i.e.
* Equal[a,b]) or a list of equations and return a list of expressions,
* which should be equal to <code>0</code>.
*
* @param ast
* @param position
* @return
*/
private static IAST checkEquations(final IAST ast, int position) {
IAST equalList = F.List();
IAST eqns = F.NIL;
IAST eq;
if (ast.get(position).isList()) {
eqns = (IAST) ast.get(position);
for (int i = 1; i < eqns.size(); i++) {
if (eqns.get(i).isAST(F.Equal, 3)) {
eq = (IAST) eqns.get(i);
// equalList.add(F.Equal(F.evalExpandAll(eq.arg1()),
// F.evalExpandAll(eq.arg2())));
equalList.append(BooleanFunctions.equals(eq));
} else {
// not an equation
throw new WrongArgumentType(eqns, eqns.get(i), i, "Equal[] expression (a==b) expected");
}
}
} else {
if (ast.get(position).isAST(F.Equal, 3)) {
eq = (IAST) ast.get(position);
equalList.append(F.Equal(F.evalExpandAll(eq.arg1()), F.evalExpandAll(eq.arg2())));
} else {
// not an equation
throw new WrongArgumentType(ast, ast.arg1(), 1, "Equal[] expression (a==b) expected");
}
}
return equalList;
}
Aggregations