Search in sources :

Example 16 with ExprPolynomialRing

use of org.matheclipse.core.polynomials.ExprPolynomialRing 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 17 with ExprPolynomialRing

use of org.matheclipse.core.polynomials.ExprPolynomialRing 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;
}
Also used : ExprPolynomialRing(org.matheclipse.core.polynomials.ExprPolynomialRing) IAST(org.matheclipse.core.interfaces.IAST) IExpr(org.matheclipse.core.interfaces.IExpr) ExprPolynomial(org.matheclipse.core.polynomials.ExprPolynomial)

Aggregations

ExprPolynomialRing (org.matheclipse.core.polynomials.ExprPolynomialRing)17 ExprPolynomial (org.matheclipse.core.polynomials.ExprPolynomial)15 IExpr (org.matheclipse.core.interfaces.IExpr)14 IAST (org.matheclipse.core.interfaces.IAST)12 WrongArgumentType (org.matheclipse.core.eval.exception.WrongArgumentType)5 VariablesSet (org.matheclipse.core.convert.VariablesSet)4 JASConversionException (org.matheclipse.core.eval.exception.JASConversionException)4 ISymbol (org.matheclipse.core.interfaces.ISymbol)4 JASIExpr (org.matheclipse.core.convert.JASIExpr)3 TermOrder (edu.jas.poly.TermOrder)2 Options (org.matheclipse.core.eval.util.Options)2 IStringX (org.matheclipse.core.interfaces.IStringX)2 ExprTermOrder (org.matheclipse.core.polynomials.ExprTermOrder)2 ExprEvaluator (org.matheclipse.core.eval.ExprEvaluator)1 ASTRange (org.matheclipse.core.expression.ASTRange)1 IInteger (org.matheclipse.core.interfaces.IInteger)1 ISignedNumber (org.matheclipse.core.interfaces.ISignedNumber)1 ExpVectorLong (org.matheclipse.core.polynomials.ExpVectorLong)1 ExprMonomial (org.matheclipse.core.polynomials.ExprMonomial)1 PartialFractionGenerator (org.matheclipse.core.polynomials.PartialFractionGenerator)1