Search in sources :

Example 16 with ValidateException

use of org.matheclipse.core.eval.exception.ValidateException in project symja_android_library by axkr.

the class Sum method evaluateSum.

private static IExpr evaluateSum(final IAST preevaledSum, EvalEngine engine) {
    if (preevaledSum.size() > 2) {
        try {
            IAST list;
            if (preevaledSum.last().isList()) {
                list = (IAST) preevaledSum.last();
            } else {
                list = F.list(preevaledSum.last());
            }
            if (list.isAST1()) {
                // indefinite sum case
                IExpr variable = list.arg1();
                if (preevaledSum.arg1().isFree(variable) && variable.isVariable()) {
                    return indefiniteSum(preevaledSum, variable);
                }
            }
            if (preevaledSum.size() == 3) {
                IExpr result = matcher1().apply(preevaledSum);
                if (result.isPresent()) {
                    return result;
                }
            }
            IExpr argN = preevaledSum.last();
            // try {
            IExpr temp = evaluateTableThrow(preevaledSum, Plus(), Plus(), engine);
            if (temp.isPresent()) {
                return temp;
            }
            VariablesSet variablesSet = determineIteratorExprVariables(preevaledSum);
            IAST varList = variablesSet.getVarList();
            IIterator<IExpr> iterator = null;
            if (argN.isList()) {
                argN = evalBlockWithoutReap(argN, varList);
                if (argN.isList()) {
                    iterator = Iterator.create((IAST) argN, preevaledSum.argSize(), engine);
                } else {
                    if (argN.isReal()) {
                        iterator = Iterator.create(F.list(argN), preevaledSum.argSize(), engine);
                    } else {
                        // Non-list iterator `1` at position `2` does not evaluate to a real numeric value.
                        return IOFunctions.printMessage(preevaledSum.topHead(), "nliter", F.list(argN, F.ZZ(preevaledSum.size() - 1)), engine);
                    }
                }
            }
            IExpr arg1 = preevaledSum.arg1();
            if (arg1.isTimes()) {
                if (variablesSet.size() > 0) {
                    temp = collectConstantFactors(preevaledSum, (IAST) arg1, variablesSet);
                    if (temp.isPresent()) {
                        return temp;
                    }
                }
            }
            if (iterator != null) {
                if (arg1.isZero()) {
                    // Sum(0, {k, n, m})
                    return F.C0;
                }
                if (iterator.isValidVariable() && iterator.getUpperLimit().isInfinity()) {
                    if (arg1.isPositiveResult() && arg1.isIntegerResult()) {
                        // Sum(n, {k, a, Infinity}) ;n is positive integer
                        return F.CInfinity;
                    }
                    if (arg1.isNegativeResult() && arg1.isIntegerResult()) {
                        // Sum(n, {k, a, Infinity}) ;n is negative integer
                        return F.CNInfinity;
                    }
                }
                if (iterator.isValidVariable() && iterator.isNumericFunction()) {
                    IAST resultList = Plus();
                    temp = evaluateLast(preevaledSum.arg1(), iterator, resultList, F.C0);
                    if (!temp.isPresent() || temp.equals(resultList)) {
                        return F.NIL;
                    }
                    if (preevaledSum.isAST2()) {
                        return temp;
                    } else {
                        IASTAppendable result = preevaledSum.removeAtClone(preevaledSum.argSize());
                        result.set(1, temp);
                        return result;
                    }
                }
                if (iterator.isValidVariable() && !iterator.isNumericFunction()) {
                    if (iterator.getStep().isOne()) {
                        if (iterator.getUpperLimit().isDirectedInfinity()) {
                            temp = definiteSumInfinity(arg1, iterator, (IAST) argN, engine);
                        } else {
                            temp = definiteSum(arg1, iterator, (IAST) argN, engine);
                        }
                        if (temp.isPresent()) {
                            if (preevaledSum.isAST2()) {
                                return temp;
                            }
                            IASTAppendable result = preevaledSum.removeAtClone(preevaledSum.argSize());
                            result.set(1, temp);
                            return result;
                        }
                    }
                }
            } else if (argN.isSymbol()) {
                temp = indefiniteSum(arg1, (ISymbol) argN);
                if (temp.isPresent()) {
                    if (preevaledSum.isAST2()) {
                        return temp;
                    } else {
                        IASTAppendable result = preevaledSum.removeAtClone(preevaledSum.argSize());
                        result.set(1, temp);
                        return result;
                    }
                }
            }
        } catch (ValidateException ve) {
            return IOFunctions.printMessage(preevaledSum.topHead(), ve, engine);
        }
    }
    return F.NIL;
}
Also used : ValidateException(org.matheclipse.core.eval.exception.ValidateException) IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) IAST(org.matheclipse.core.interfaces.IAST) IExpr(org.matheclipse.core.interfaces.IExpr) VariablesSet(org.matheclipse.core.convert.VariablesSet)

Aggregations

ValidateException (org.matheclipse.core.eval.exception.ValidateException)16 IAST (org.matheclipse.core.interfaces.IAST)12 IExpr (org.matheclipse.core.interfaces.IExpr)12 IASTMutable (org.matheclipse.core.interfaces.IASTMutable)5 ISymbol (org.matheclipse.core.interfaces.ISymbol)5 FlowControlException (org.matheclipse.core.eval.exception.FlowControlException)4 IASTAppendable (org.matheclipse.core.interfaces.IASTAppendable)4 Complex (org.hipparchus.complex.Complex)3 EvalEngine (org.matheclipse.core.eval.EvalEngine)3 SymjaMathException (org.matheclipse.core.eval.exception.SymjaMathException)3 VariablesSet (org.matheclipse.core.convert.VariablesSet)2 ExprEvaluator (org.matheclipse.core.eval.ExprEvaluator)2 LimitException (org.matheclipse.core.eval.exception.LimitException)2 ASTSeriesData (org.matheclipse.core.expression.ASTSeriesData)2 IBuiltInSymbol (org.matheclipse.core.interfaces.IBuiltInSymbol)2 IEvaluator (org.matheclipse.core.interfaces.IEvaluator)2 SyntaxError (org.matheclipse.parser.client.SyntaxError)2 MathException (org.matheclipse.parser.client.math.MathException)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1