Search in sources :

Example 36 with WrongArgumentType

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!");
    }
}
Also used : ExprPolynomialRing(org.matheclipse.core.polynomials.ExprPolynomialRing) WrongArgumentType(org.matheclipse.core.eval.exception.WrongArgumentType) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST) ExprPolynomial(org.matheclipse.core.polynomials.ExprPolynomial)

Example 37 with WrongArgumentType

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;
}
Also used : WrongArgumentType(org.matheclipse.core.eval.exception.WrongArgumentType) IAST(org.matheclipse.core.interfaces.IAST)

Aggregations

WrongArgumentType (org.matheclipse.core.eval.exception.WrongArgumentType)37 IAST (org.matheclipse.core.interfaces.IAST)30 IExpr (org.matheclipse.core.interfaces.IExpr)29 ISymbol (org.matheclipse.core.interfaces.ISymbol)12 ISignedNumber (org.matheclipse.core.interfaces.ISignedNumber)7 IInteger (org.matheclipse.core.interfaces.IInteger)6 ExprPolynomialRing (org.matheclipse.core.polynomials.ExprPolynomialRing)5 ExprPolynomial (org.matheclipse.core.polynomials.ExprPolynomial)4 ExpVector (edu.jas.poly.ExpVector)3 LinearConstraint (org.hipparchus.optim.linear.LinearConstraint)3 IFraction (org.matheclipse.core.interfaces.IFraction)3 ModLong (edu.jas.arith.ModLong)2 GenPolynomial (edu.jas.poly.GenPolynomial)2 ArrayList (java.util.ArrayList)2 Apint (org.apfloat.Apint)2 PrettyPrint (edu.jas.kern.PrettyPrint)1 Function (java.util.function.Function)1 IntVariable (jp.ac.kobe_u.cs.cream.IntVariable)1 Apfloat (org.apfloat.Apfloat)1 MathIllegalStateException (org.hipparchus.exception.MathIllegalStateException)1