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;
}
Aggregations