Search in sources :

Example 1 with ASTSeriesData

use of org.matheclipse.core.expression.ASTSeriesData in project symja_android_library by axkr.

the class PlusOp method plus.

/**
 * Add an argument <code>arg</code> to this <code>Plus()</code> expression.
 *
 * @param arg
 * @return <code>F.Indeterminate</code> if the result is indeterminated, <code>F.NIL</code>
 *         otherwise.
 */
public IExpr plus(final IExpr arg) {
    // }
    if (arg.isIndeterminate()) {
        return S.Indeterminate;
    }
    try {
        if (numberValue.isPresent() && numberValue.isDirectedInfinity()) {
            if (numberValue.isComplexInfinity()) {
                if (arg.isDirectedInfinity()) {
                    return S.Indeterminate;
                }
                numberValue = F.CComplexInfinity;
                evaled = true;
                return F.NIL;
            } else if (numberValue.isInfinity()) {
                if (arg.isInfinity()) {
                    evaled = true;
                    return F.NIL;
                }
                if (arg.isDirectedInfinity()) {
                    return S.Indeterminate;
                }
                if (arg.isRealResult()) {
                    evaled = true;
                    return F.NIL;
                }
            } else if (numberValue.isNegativeInfinity()) {
                if (arg.isNegativeInfinity()) {
                    evaled = true;
                    return F.NIL;
                }
                if (arg.isDirectedInfinity()) {
                    // Indeterminate expression `1` encountered.
                    IOFunctions.printMessage(S.Infinity, "indet", F.list(F.Plus(numberValue, arg)), EvalEngine.get());
                    return S.Indeterminate;
                }
                if (arg.isRealResult()) {
                    evaled = true;
                    return F.NIL;
                }
            }
        }
        if (arg.isNumber()) {
            if (arg.isZero()) {
                evaled = true;
                return F.NIL;
            }
            if (!numberValue.isPresent()) {
                numberValue = arg;
                return F.NIL;
            }
            if (numberValue.isNumber()) {
                numberValue = numberValue.plus(arg);
                evaled = true;
                return F.NIL;
            }
            if (numberValue.isInfinity()) {
                if (arg.isNegativeInfinity()) {
                    // Indeterminate expression `1` encountered.
                    IOFunctions.printMessage(S.Infinity, "indet", F.list(F.Plus(numberValue, arg)), EvalEngine.get());
                    return S.Indeterminate;
                }
                numberValue = F.CInfinity;
                evaled = true;
                return F.NIL;
            }
            if (numberValue.isNegativeInfinity()) {
                numberValue = negativeInfinityPlus(arg);
                if (numberValue.isIndeterminate()) {
                    return S.Indeterminate;
                }
                evaled = true;
                return F.NIL;
            }
            return F.NIL;
        } else if (arg.isQuantity()) {
            // if (arg.isQuantity()) {
            if (!numberValue.isPresent()) {
                numberValue = arg;
                return F.NIL;
            }
            IQuantity q = (IQuantity) arg;
            IExpr temp = q.plus(numberValue, true);
            if (temp.isPresent()) {
                evaled = true;
                numberValue = temp;
            } else {
                if (addMerge(q, F.C1)) {
                    evaled = true;
                }
            }
            return F.NIL;
        // }
        } else if (arg.isAST()) {
            final IAST ast = (IAST) arg;
            final int headID = ((IAST) arg).headID();
            if (headID >= ID.DirectedInfinity) {
                switch(headID) {
                    case ID.DirectedInfinity:
                        if (arg.isDirectedInfinity()) {
                            if (!numberValue.isPresent()) {
                                numberValue = arg;
                                if (arg.isComplexInfinity()) {
                                    if (plusMap != null && plusMap.size() > 0) {
                                        evaled = true;
                                    }
                                } else {
                                    if (plusMap != null) {
                                        Iterator<Entry<IExpr, IExpr>> iterator = plusMap.entrySet().iterator();
                                        while (iterator.hasNext()) {
                                            Entry<IExpr, IExpr> entry = iterator.next();
                                            if (entry.getKey().isRealResult()) {
                                                iterator.remove();
                                                evaled = true;
                                            }
                                        }
                                    }
                                }
                                return F.NIL;
                            }
                            if (arg.isInfinity()) {
                                if (numberValue.isNegativeInfinity()) {
                                    // Indeterminate expression `1` encountered.
                                    IOFunctions.printMessage(S.Infinity, "indet", F.list(F.Plus(arg, numberValue)), EvalEngine.get());
                                    return S.Indeterminate;
                                }
                                numberValue = F.CInfinity;
                                evaled = true;
                                return F.NIL;
                            } else if (arg.isNegativeInfinity()) {
                                numberValue = negativeInfinityPlus(numberValue);
                                if (numberValue.isIndeterminate()) {
                                    return S.Indeterminate;
                                }
                                evaled = true;
                                return F.NIL;
                            } else if (arg.isComplexInfinity()) {
                                if (numberValue.isDirectedInfinity()) {
                                    // Indeterminate expression `1` encountered.
                                    IOFunctions.printMessage(S.Infinity, "indet", F.list(F.Plus(arg, numberValue)), EvalEngine.get());
                                    return S.Indeterminate;
                                }
                                numberValue = F.CComplexInfinity;
                                evaled = true;
                                return F.NIL;
                            }
                        }
                        break;
                    case ID.Times:
                        if (ast.size() > 1) {
                            if (ast.arg1().isNumber()) {
                                if (addMerge(ast.rest().oneIdentity1(), ast.arg1())) {
                                    evaled = true;
                                }
                                return F.NIL;
                            }
                            if (addMerge(ast, F.C1)) {
                                evaled = true;
                            }
                        }
                        return F.NIL;
                    case ID.Interval:
                        if (arg.isInterval()) {
                            if (!numberValue.isPresent()) {
                                numberValue = arg;
                                return F.NIL;
                            }
                            IExpr temp;
                            if (numberValue.isInterval()) {
                                temp = IntervalSym.plus((IAST) numberValue, (IAST) arg);
                            } else {
                                temp = IntervalSym.plus(numberValue, (IAST) arg);
                            }
                            if (temp.isPresent()) {
                                numberValue = temp;
                                evaled = true;
                            } else {
                                if (addMerge(arg, F.C1)) {
                                    evaled = true;
                                }
                            }
                            return F.NIL;
                        }
                        break;
                    // break;
                    case ID.SeriesData:
                        if (arg instanceof ASTSeriesData) {
                            if (!numberValue.isPresent()) {
                                numberValue = arg;
                                return F.NIL;
                            }
                            numberValue = ((ASTSeriesData) arg).plus(numberValue);
                            evaled = true;
                            return F.NIL;
                        }
                        break;
                }
            }
        // } else if (!numberValue.isPresent() && arg.isRealResult()) {
        // numberValue = arg;
        // return F.NIL;
        }
        if (addMerge(arg, F.C1)) {
            evaled = true;
        }
    } catch (ValidateException | LimitException e) {
        LOGGER.debug("PlusOp.plus() failed", e);
        throw e;
    } catch (SymjaMathException sme) {
        LOGGER.debug("PlusOp.plus() failed", sme);
    }
    return F.NIL;
}
Also used : ValidateException(org.matheclipse.core.eval.exception.ValidateException) IQuantity(org.matheclipse.core.tensor.qty.IQuantity) Entry(java.util.Map.Entry) ASTSeriesData(org.matheclipse.core.expression.ASTSeriesData) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST) LimitException(org.matheclipse.core.eval.exception.LimitException) SymjaMathException(org.matheclipse.core.eval.exception.SymjaMathException)

Example 2 with ASTSeriesData

use of org.matheclipse.core.expression.ASTSeriesData in project symja_android_library by axkr.

the class JASIExpr method expr2IExprPoly.

private GenPolynomial<IExpr> expr2IExprPoly(final IExpr exprPoly) throws ArithmeticException, ClassCastException {
    if (exprPoly instanceof IAST) {
        final IAST ast = (IAST) exprPoly;
        GenPolynomial<IExpr> result = fPolyFactory.getZERO();
        GenPolynomial<IExpr> p = fPolyFactory.getZERO();
        if (ast.isPlus()) {
            IExpr expr = ast.arg1();
            result = expr2IExprPoly(expr);
            for (int i = 2; i < ast.size(); i++) {
                expr = ast.get(i);
                p = expr2IExprPoly(expr);
                result = result.sum(p);
            }
            return result;
        } else if (ast.isTimes()) {
            IExpr expr = ast.arg1();
            result = expr2IExprPoly(expr);
            for (int i = 2; i < ast.size(); i++) {
                expr = ast.get(i);
                p = expr2IExprPoly(expr);
                result = result.multiply(p);
            }
            return result;
        } else if (ast.isPower()) {
            final IExpr base = ast.base();
            if (base instanceof ISymbol) {
                ExpVector leer = fPolyFactory.evzero;
                int ix = leer.indexVar(base.toString(), fPolyFactory.getVars());
                if (ix >= 0) {
                    int exponent = ast.exponent().toIntDefault();
                    if (exponent < 0) {
                        throw new ArithmeticException("JASConvert:expr2Poly - invalid exponent: " + ast.exponent().toString());
                    }
                    ExpVector e = ExpVector.create(fVariables.size(), ix, exponent);
                    return fPolyFactory.getONE().multiply(e);
                }
            }
        } else if (fNumericFunction) {
            if (ast.isNumericFunction(true)) {
                return new GenPolynomial<IExpr>(fPolyFactory, ast);
            }
        }
    } else if (exprPoly instanceof ISymbol) {
        ExpVector leer = fPolyFactory.evzero;
        int ix = leer.indexVar(exprPoly.toString(), fPolyFactory.getVars());
        if (ix >= 0) {
            ExpVector e = ExpVector.create(fVariables.size(), ix, 1L);
            return fPolyFactory.getONE().multiply(e);
        }
        if (fNumericFunction) {
            if (exprPoly.isNumericFunction(true)) {
                return new GenPolynomial<IExpr>(fPolyFactory, exprPoly);
            }
            throw new ClassCastException(exprPoly.toString());
        } else {
            return new GenPolynomial<IExpr>(fPolyFactory, exprPoly);
        }
    } else if (exprPoly instanceof IInteger) {
        return new GenPolynomial<IExpr>(fPolyFactory, exprPoly);
    } else if (exprPoly instanceof IFraction) {
        return new GenPolynomial<IExpr>(fPolyFactory, exprPoly);
    }
    if (exprPoly.isFree(t -> fVariables.contains(t), true)) {
        return new GenPolynomial<IExpr>(fPolyFactory, exprPoly);
    } else {
        for (int i = 0; i < fVariables.size(); i++) {
            if (fVariables.get(i).equals(exprPoly)) {
                ExpVector e = ExpVector.create(fVariables.size(), i, 1L);
                return fPolyFactory.getONE().multiply(e);
            }
        }
    }
    if (exprPoly instanceof ASTSeriesData) {
        return new GenPolynomial<IExpr>(fPolyFactory, exprPoly);
    }
    throw new ClassCastException(exprPoly.toString());
}
Also used : IFraction(org.matheclipse.core.interfaces.IFraction) GenPolynomial(edu.jas.poly.GenPolynomial) ISymbol(org.matheclipse.core.interfaces.ISymbol) IInteger(org.matheclipse.core.interfaces.IInteger) ExpVector(edu.jas.poly.ExpVector) ASTSeriesData(org.matheclipse.core.expression.ASTSeriesData) IAST(org.matheclipse.core.interfaces.IAST) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 3 with ASTSeriesData

use of org.matheclipse.core.expression.ASTSeriesData in project symja_android_library by axkr.

the class MathMLFormFactory method convertAST.

private void convertAST(final StringBuilder buf, final IAST ast, final int precedence) {
    final IAST list = ast;
    IExpr header = list.head();
    if (!header.isSymbol()) {
        // print expressions like: f(#1, y)& [x]
        IAST[] derivStruct = list.isDerivativeAST1();
        if (derivStruct != null) {
            IAST a1Head = derivStruct[0];
            IAST headAST = derivStruct[1];
            if (a1Head.isAST1() && headAST.isAST1() && (headAST.arg1().isSymbol() || headAST.arg1().isAST())) {
                try {
                    int n = a1Head.arg1().toIntDefault();
                    if (n == 1 || n == 2) {
                        tagStart(buf, "mrow");
                        tagStart(buf, "msup");
                        IExpr symbolOrAST = headAST.arg1();
                        convertInternal(buf, symbolOrAST, Integer.MAX_VALUE, false);
                        if (n == 1) {
                            tag(buf, "mo", "&#8242;");
                        } else if (n == 2) {
                            tag(buf, "mo", "&#8242;&#8242;");
                        }
                        tagEnd(buf, "msup");
                        if (derivStruct[2] != null) {
                            convertArgs(buf, symbolOrAST, list);
                        }
                        tagEnd(buf, "mrow");
                        return;
                    }
                    tagStart(buf, "mrow");
                    IExpr symbolOrAST = headAST.arg1();
                    tagStart(buf, "msup");
                    convertInternal(buf, symbolOrAST, Integer.MAX_VALUE, false);
                    tagStart(buf, "mrow");
                    tag(buf, "mo", "(");
                    // supscript "(n)"
                    convertInternal(buf, a1Head.arg1(), Integer.MIN_VALUE, false);
                    tag(buf, "mo", ")");
                    tagEnd(buf, "mrow");
                    tagEnd(buf, "msup");
                    if (derivStruct[2] != null) {
                        convertArgs(buf, symbolOrAST, list);
                    }
                    tagEnd(buf, "mrow");
                    return;
                } catch (ArithmeticException ae) {
                }
            }
        }
        convertInternal(buf, header, Integer.MIN_VALUE, false);
        convertFunctionArgs(buf, list);
        return;
    }
    ISymbol head = list.topHead();
    final org.matheclipse.parser.client.operator.Operator operator = OutputFormFactory.getOperator(head);
    if (operator != null) {
        if (operator instanceof PostfixOperator) {
            if (list.isAST1()) {
                convertPostfixOperator(buf, list, (PostfixOperator) operator, operator.getPrecedence());
                return;
            }
        } else {
            if (convertOperator(operator, list, buf, operator.getPrecedence(), head)) {
                return;
            }
        }
    }
    if (list instanceof ASTSeriesData) {
        if (convertSeriesData(buf, (ASTSeriesData) list, precedence)) {
            return;
        }
    }
    if (list.isList() || list instanceof ASTRealVector || list instanceof ASTRealMatrix) {
        convertList(buf, list);
        return;
    }
    if (list.isAST(S.Parenthesis)) {
        convertArgs(buf, S.Parenthesis, list);
        return;
    }
    if (list.isInterval() && convertInterval(buf, list)) {
        return;
    }
    if (list.isAssociation()) {
        convertAssociation(buf, (IAssociation) list);
        return;
    }
    int functionID = ((ISymbol) list.head()).ordinal();
    if (functionID > ID.UNKNOWN) {
        switch(functionID) {
            case ID.Inequality:
                if (list.size() > 3 && convertInequality(buf, list, precedence)) {
                    return;
                }
                break;
            case ID.Part:
                if ((list.size() >= 3)) {
                    convertPart(buf, list);
                    return;
                }
                break;
            case ID.Slot:
                if ((list.isAST1()) && (list.arg1() instanceof IInteger)) {
                    convertSlot(buf, list);
                    return;
                }
                break;
            case ID.SlotSequence:
                if ((list.isAST1()) && (list.arg1() instanceof IInteger)) {
                    convertSlotSequence(buf, list);
                    return;
                }
                break;
            case ID.SparseArray:
                if (list.isSparseArray()) {
                    tagStart(buf, "mtext");
                    buf.append(list.toString());
                    tagEnd(buf, "mtext");
                    return;
                }
                break;
            case ID.Defer:
            case ID.HoldForm:
                if ((list.isAST1())) {
                    convertInternal(buf, list.arg1(), precedence, false);
                    return;
                }
                break;
            case ID.DirectedInfinity:
                if (list.isDirectedInfinity()) {
                    // head.equals(F.DirectedInfinity))
                    if (list.isAST0()) {
                        convertSymbol(buf, S.ComplexInfinity);
                        return;
                    }
                    if (list.isAST1()) {
                        if (list.arg1().isOne()) {
                            convertSymbol(buf, S.Infinity);
                            return;
                        } else if (list.arg1().isMinusOne()) {
                            convertInternal(buf, F.Times(F.CN1, S.Infinity), precedence, false);
                            return;
                        } else if (list.arg1().isImaginaryUnit()) {
                            convertInternal(buf, F.Times(F.CI, S.Infinity), precedence, false);
                            return;
                        } else if (list.arg1().isNegativeImaginaryUnit()) {
                            convertInternal(buf, F.Times(F.CNI, S.Infinity), precedence, false);
                            return;
                        }
                    }
                }
                break;
        }
    }
    // if (head.equals(F.SeriesData) && (list.size() == 7)) {
    // if (convertSeriesData(buf, list, precedence)) {
    // return;
    // }
    tagStart(buf, "mrow");
    convertHead(buf, ast.head());
    // &af; &#x2061;
    // tag(buf, "mo", "&#x2061;");
    tagStart(buf, "mrow");
    if (fRelaxedSyntax) {
        tag(buf, "mo", "(");
    } else {
        tag(buf, "mo", "[");
    }
    tagStart(buf, "mrow");
    for (int i = 1; i < ast.size(); i++) {
        convertInternal(buf, ast.get(i), Integer.MIN_VALUE, false);
        if (i < ast.argSize()) {
            tag(buf, "mo", ",");
        }
    }
    tagEnd(buf, "mrow");
    if (fRelaxedSyntax) {
        tag(buf, "mo", ")");
    } else {
        tag(buf, "mo", "]");
    }
    tagEnd(buf, "mrow");
    tagEnd(buf, "mrow");
}
Also used : ISymbol(org.matheclipse.core.interfaces.ISymbol) ASTRealMatrix(org.matheclipse.core.expression.ASTRealMatrix) PostfixOperator(org.matheclipse.parser.client.operator.PostfixOperator) IInteger(org.matheclipse.core.interfaces.IInteger) ASTSeriesData(org.matheclipse.core.expression.ASTSeriesData) IAST(org.matheclipse.core.interfaces.IAST) IExpr(org.matheclipse.core.interfaces.IExpr) ASTRealVector(org.matheclipse.core.expression.ASTRealVector)

Example 4 with ASTSeriesData

use of org.matheclipse.core.expression.ASTSeriesData in project symja_android_library by axkr.

the class Integrate method evaluate.

@Override
public IExpr evaluate(IAST holdallAST, EvalEngine engine) {
    if (Config.JAS_NO_THREADS) {
        // Android changed: call static initializer in evaluate() method.
        new IntegrateInitializer().run();
    } else {
    // see #setUp() method
    }
    try {
        // wait for initializer run is completed, no matter how many threads call evaluate() method
        await();
    } catch (InterruptedException ignored) {
    }
    IAssumptions oldAssumptions = engine.getAssumptions();
    boolean numericMode = engine.isNumericMode();
    try {
        OptionArgs options = null;
        if (holdallAST.size() > 3) {
            options = new OptionArgs(S.Integrate, holdallAST, holdallAST.size() - 1, engine);
            if (!options.isInvalidPosition()) {
                holdallAST = holdallAST.most();
            }
        }
        IExpr assumptionExpr = OptionArgs.determineAssumptions(holdallAST, -1, options);
        if (assumptionExpr.isPresent() && assumptionExpr.isAST()) {
            IAssumptions assumptions = org.matheclipse.core.eval.util.Assumptions.getInstance(assumptionExpr);
            if (assumptions != null) {
                engine.setAssumptions(assumptions);
            }
        }
        boolean evaled = false;
        IExpr result;
        engine.setNumericMode(false);
        if (holdallAST.size() < 3 || holdallAST.isEvalFlagOn(IAST.BUILT_IN_EVALED)) {
            return F.NIL;
        }
        final IExpr arg1Holdall = holdallAST.arg1();
        final IExpr a1 = NumberTheory.rationalize(arg1Holdall, false).orElse(arg1Holdall);
        IExpr arg1 = engine.evaluateNIL(a1);
        if (arg1.isPresent()) {
            evaled = true;
        } else {
            arg1 = a1;
        }
        if (arg1.isIndeterminate()) {
            return S.Indeterminate;
        }
        if (holdallAST.size() > 3) {
            // Integrate[Integrate[fxy, y], x] ...
            return holdallAST.foldRight((x, y) -> engine.evaluateNIL(F.Integrate(x, y)), arg1, 2);
        }
        IExpr arg2 = engine.evaluateNIL(holdallAST.arg2());
        if (arg2.isPresent()) {
            evaled = true;
        } else {
            arg2 = holdallAST.arg2();
        }
        if (arg2.isList()) {
            IAST xList = (IAST) arg2;
            if (xList.isVector() == 3) {
                // Integrate[f[x], {x,a,b}]
                IAST copy = holdallAST.setAtCopy(2, xList.arg1());
                IExpr temp = engine.evaluate(copy);
                if (temp.isFreeAST(S.Integrate)) {
                    return definiteIntegral(temp, xList, engine);
                }
            }
            return F.NIL;
        }
        if (arg1.isList() && arg2.isSymbol()) {
            return mapIntegrate((IAST) arg1, arg2);
        }
        final IASTAppendable ast = holdallAST.setAtClone(1, arg1);
        ast.set(2, arg2);
        final IExpr x = ast.arg2();
        if (!x.isVariable()) {
            // `1` is not a valid variable.
            return IOFunctions.printMessage(ast.topHead(), "ivar", F.list(x), engine);
        }
        if (arg1.isNumber()) {
            // Integrate[x_?NumberQ,y_Symbol] -> x*y
            return Times(arg1, x);
        }
        if (arg1 instanceof ASTSeriesData) {
            ASTSeriesData series = ((ASTSeriesData) arg1);
            if (series.getX().equals(x)) {
                final IExpr temp = ((ASTSeriesData) arg1).integrate(x);
                if (temp != null) {
                    return temp;
                }
            }
            return F.NIL;
        }
        if (arg1.isFree(x, true)) {
            // Integrate[x_,y_Symbol] -> x*y /; FreeQ[x,y]
            return Times(arg1, x);
        }
        if (arg1.equals(x)) {
            // Integrate[x_,x_Symbol] -> x^2 / 2
            return Times(F.C1D2, Power(arg1, F.C2));
        }
        boolean showSteps = false;
        if (showSteps) {
            LOGGER.info(arg1);
            if (DEBUG_EXPR.contains(arg1)) {
            // System.exit(-1);
            }
            DEBUG_EXPR.add(arg1);
        }
        if (arg1.isAST()) {
            final IAST fx = (IAST) arg1;
            if (fx.topHead().equals(x)) {
                // issue #91
                return F.NIL;
            }
            int[] dim = fx.isPiecewise();
            if (dim != null) {
                return integratePiecewise(dim, fx, ast);
            }
            result = integrateAbs(fx, x);
            if (result.isPresent()) {
                if (result == S.Undefined) {
                    return F.NIL;
                }
                return result;
            }
            result = integrateByRubiRules(fx, x, ast, engine);
            if (result.isPresent()) {
                IExpr temp = result.replaceAll(f -> {
                    if (f.isAST(UtilityFunctionCtors.Unintegrable, 3)) {
                        IAST integrate = F.Integrate(f.first(), f.second());
                        integrate.addEvalFlags(IAST.BUILT_IN_EVALED);
                        return integrate;
                    } else if (f.isAST(F.$rubi("CannotIntegrate"), 3)) {
                        IAST integrate = F.Integrate(f.first(), f.second());
                        integrate.addEvalFlags(IAST.BUILT_IN_EVALED);
                        return integrate;
                    }
                    return F.NIL;
                });
                return temp.orElse(result);
            }
            if (fx.isTimes()) {
                IAST[] temp = fx.filter(arg -> arg.isFree(x));
                IExpr free = temp[0].oneIdentity1();
                if (!free.isOne()) {
                    IExpr rest = temp[1].oneIdentity1();
                    // Integrate[free_ * rest_,x_Symbol] -> free*Integrate[rest, x] /; FreeQ[free,x]
                    return Times(free, Integrate(rest, x));
                }
            }
            if (fx.isPower()) {
                // base ^ exponent
                IExpr base = fx.base();
                IExpr exponent = fx.exponent();
                if (base.equals(x) && exponent.isFree(x)) {
                    if (exponent.isMinusOne()) {
                        // Integrate[ 1 / x_ , x_ ] -> Log[x]
                        return Log(x);
                    }
                    // Integrate[ x_ ^n_ , x_ ] -> x^(n+1)/(n+1) /; FreeQ[n, x]
                    IExpr temp = Plus(F.C1, exponent);
                    return Divide(Power(x, temp), temp);
                }
                if (exponent.equals(x) && base.isFree(x)) {
                    if (base.isE()) {
                        // E^x
                        return fx;
                    }
                    // a^x / Log(a)
                    return F.Divide(fx, F.Log(base));
                }
            }
            result = callRestIntegrate(fx, x, engine);
            if (result.isPresent()) {
                return result;
            }
        }
        return evaled ? ast : F.NIL;
    } finally {
        engine.setAssumptions(oldAssumptions);
        engine.setNumericMode(numericMode);
    }
}
Also used : IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) IAssumptions(org.matheclipse.core.eval.util.IAssumptions) ASTSeriesData(org.matheclipse.core.expression.ASTSeriesData) OptionArgs(org.matheclipse.core.eval.util.OptionArgs) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST)

Example 5 with ASTSeriesData

use of org.matheclipse.core.expression.ASTSeriesData in project symja_android_library by axkr.

the class D method evaluate.

@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
    if (ast.isAST1()) {
        return ast.arg1();
    }
    try {
        final IExpr fx = ast.arg1();
        if (fx.isIndeterminate()) {
            return S.Indeterminate;
        }
        if (ast.size() > 3) {
            // reduce arguments by folding D[fxy, x, y] to D[ D[fxy, x], y] ...
            return ast.foldLeft((x, y) -> engine.evaluateNIL(F.D(x, y)), fx, 2);
        }
        IExpr x = ast.arg2();
        if (!(x.isVariable() || x.isList())) {
            // `1` is not a valid variable.
            return IOFunctions.printMessage(ast.topHead(), "ivar", F.list(x), engine);
        }
        if (fx.isList()) {
            IAST list = (IAST) fx;
            // thread over first list
            return list.mapThreadEvaled(engine, F.ListAlloc(list.size()), ast, 1);
        }
        if (x.isList()) {
            // D[fx_, {...}]
            IAST xList = (IAST) x;
            if (xList.isAST1() && xList.arg1().isListOfLists()) {
                IAST subList = (IAST) xList.arg1();
                IASTAppendable result = F.ListAlloc(subList.size());
                result.appendArgs(subList.size(), i -> F.D(fx, F.list(subList.get(i))));
                return result;
            } else if (xList.isAST1() && xList.arg1().isList()) {
                IAST subList = (IAST) xList.arg1();
                return subList.mapLeft(F.ListAlloc(), (a, b) -> engine.evaluateNIL(F.D(a, b)), fx);
            } else if (xList.isAST2()) {
                if (ast.isEvalFlagOn(IAST.IS_DERIVATIVE_EVALED)) {
                    return F.NIL;
                }
                if (xList.arg1().isList()) {
                    x = F.list(xList.arg1());
                } else {
                    x = xList.arg1();
                }
                IExpr arg2 = xList.arg2();
                int n = arg2.toIntDefault();
                if (n >= 0) {
                    IExpr temp = fx;
                    for (int i = 0; i < n; i++) {
                        temp = S.D.ofNIL(engine, temp, x);
                        if (!temp.isPresent()) {
                            return F.NIL;
                        }
                    }
                    return temp;
                }
                if (arg2.isFree(num -> num.isNumber(), false)) {
                    if (fx instanceof ASTSeriesData) {
                        return F.NIL;
                    }
                    if (fx.isFree(x, true)) {
                        // Piecewise({{fx, arg2 == 0}}, 0)
                        return F.Piecewise(F.list(F.list(fx, F.Equal(arg2, F.C0))), F.C0);
                    }
                    if (fx.equals(x)) {
                        // Piecewise({{fx, arg2 == 0}, {1, arg2 == 1}}, 0)
                        return F.Piecewise(F.list(F.list(fx, F.Equal(arg2, F.C0)), F.list(F.C1, F.Equal(arg2, F.C1))), F.C0);
                    }
                    if (fx.isAST()) {
                        final IAST function = (IAST) fx;
                        // final IExpr header = function.head();
                        if (function.isPlus()) {
                            // D(a_+b_+c_,x_) -> D(a,x)+D(b,x)+D(c,x)
                            return function.mapThread(F.D(F.Slot1, xList), 1);
                        }
                    }
                    return F.NIL;
                }
                if (!x.isVariable()) {
                    // `1` is not a valid variable.
                    return IOFunctions.printMessage(ast.topHead(), "ivar", F.list(x), engine);
                }
                if (arg2.isAST()) {
                    return F.NIL;
                }
                // symbolic expression or a non-negative integer.
                return IOFunctions.printMessage(ast.topHead(), "dvar", F.list(xList), engine);
            }
            return F.NIL;
        }
        if (!x.isVariable()) {
            // `1` is not a valid variable.
            return IOFunctions.printMessage(ast.topHead(), "ivar", F.list(x), engine);
        }
        return binaryD(fx, x, ast, engine);
    } catch (final ValidateException ve) {
        // int number validation
        LOGGER.log(engine.getLogLevel(), ve.getMessage(ast.topHead()), ve);
        return F.NIL;
    }
}
Also used : EvalEngine(org.matheclipse.core.eval.EvalEngine) IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) F(org.matheclipse.core.expression.F) IAST(org.matheclipse.core.interfaces.IAST) IInteger(org.matheclipse.core.interfaces.IInteger) ISymbol(org.matheclipse.core.interfaces.ISymbol) IOFunctions(org.matheclipse.core.builtin.IOFunctions) ASTSeriesData(org.matheclipse.core.expression.ASTSeriesData) S(org.matheclipse.core.expression.S) IASTMutable(org.matheclipse.core.interfaces.IASTMutable) Logger(org.apache.logging.log4j.Logger) IRational(org.matheclipse.core.interfaces.IRational) AbstractFunctionEvaluator(org.matheclipse.core.eval.interfaces.AbstractFunctionEvaluator) DRules(org.matheclipse.core.reflection.system.rules.DRules) IExpr(org.matheclipse.core.interfaces.IExpr) LogManager(org.apache.logging.log4j.LogManager) ValidateException(org.matheclipse.core.eval.exception.ValidateException) BinaryBindIth1st(org.matheclipse.core.generic.BinaryBindIth1st) ValidateException(org.matheclipse.core.eval.exception.ValidateException) IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) ASTSeriesData(org.matheclipse.core.expression.ASTSeriesData) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST)

Aggregations

ASTSeriesData (org.matheclipse.core.expression.ASTSeriesData)8 IExpr (org.matheclipse.core.interfaces.IExpr)8 IAST (org.matheclipse.core.interfaces.IAST)7 IInteger (org.matheclipse.core.interfaces.IInteger)5 ISymbol (org.matheclipse.core.interfaces.ISymbol)4 IASTAppendable (org.matheclipse.core.interfaces.IASTAppendable)3 EvalEngine (org.matheclipse.core.eval.EvalEngine)2 ValidateException (org.matheclipse.core.eval.exception.ValidateException)2 ASTRealMatrix (org.matheclipse.core.expression.ASTRealMatrix)2 ASTRealVector (org.matheclipse.core.expression.ASTRealVector)2 BinaryBindIth1st (org.matheclipse.core.generic.BinaryBindIth1st)2 IRational (org.matheclipse.core.interfaces.IRational)2 IQuantity (org.matheclipse.core.tensor.qty.IQuantity)2 PostfixOperator (org.matheclipse.parser.client.operator.PostfixOperator)2 ExpVector (edu.jas.poly.ExpVector)1 GenPolynomial (edu.jas.poly.GenPolynomial)1 IntList (it.unimi.dsi.fastutil.ints.IntList)1 Entry (java.util.Map.Entry)1 LogManager (org.apache.logging.log4j.LogManager)1 Logger (org.apache.logging.log4j.Logger)1