Search in sources :

Example 1 with ExpVectorLong

use of org.matheclipse.core.polynomials.ExpVectorLong 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)

Aggregations

VariablesSet (org.matheclipse.core.convert.VariablesSet)1 IAST (org.matheclipse.core.interfaces.IAST)1 IExpr (org.matheclipse.core.interfaces.IExpr)1 IInteger (org.matheclipse.core.interfaces.IInteger)1 ExpVectorLong (org.matheclipse.core.polynomials.ExpVectorLong)1 ExprPolynomial (org.matheclipse.core.polynomials.ExprPolynomial)1 ExprPolynomialRing (org.matheclipse.core.polynomials.ExprPolynomialRing)1