Search in sources :

Example 6 with ExprPolynomial

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

Example 7 with ExprPolynomial

use of org.matheclipse.core.polynomials.ExprPolynomial in project symja_android_library by axkr.

the class Roots method rootsOfQuadraticExprPolynomial.

/**
	 * Solve a polynomial with degree &lt;= 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;
}
Also used : ExprPolynomialRing(org.matheclipse.core.polynomials.ExprPolynomialRing) IAST(org.matheclipse.core.interfaces.IAST) JASConversionException(org.matheclipse.core.eval.exception.JASConversionException) ExprPolynomial(org.matheclipse.core.polynomials.ExprPolynomial)

Example 8 with ExprPolynomial

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

Example 9 with ExprPolynomial

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;
}
Also used : Options(org.matheclipse.core.eval.util.Options) TermOrder(edu.jas.poly.TermOrder) ExprTermOrder(org.matheclipse.core.polynomials.ExprTermOrder) VariablesSet(org.matheclipse.core.convert.VariablesSet) JASConversionException(org.matheclipse.core.eval.exception.JASConversionException) ExprPolynomialRing(org.matheclipse.core.polynomials.ExprPolynomialRing) ExprTermOrder(org.matheclipse.core.polynomials.ExprTermOrder) JASIExpr(org.matheclipse.core.convert.JASIExpr) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST) IStringX(org.matheclipse.core.interfaces.IStringX) ExprPolynomial(org.matheclipse.core.polynomials.ExprPolynomial)

Example 10 with ExprPolynomial

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

Aggregations

ExprPolynomial (org.matheclipse.core.polynomials.ExprPolynomial)15 ExprPolynomialRing (org.matheclipse.core.polynomials.ExprPolynomialRing)15 IAST (org.matheclipse.core.interfaces.IAST)12 IExpr (org.matheclipse.core.interfaces.IExpr)12 VariablesSet (org.matheclipse.core.convert.VariablesSet)4 JASConversionException (org.matheclipse.core.eval.exception.JASConversionException)4 WrongArgumentType (org.matheclipse.core.eval.exception.WrongArgumentType)4 JASIExpr (org.matheclipse.core.convert.JASIExpr)3 ISymbol (org.matheclipse.core.interfaces.ISymbol)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