use of org.matheclipse.core.generic.interfaces.IIterator in project symja_android_library by axkr.
the class Sum method iteratorStep.
/**
* See <a href="http://en.wikibooks.org/wiki/LaTeX/Mathematics">Wikibooks -
* LaTeX/Mathematics</a>
*
* @param buf
* @param mathSymbol
* the symbol for Sum or Product expressions
* @param f
* @param i
*
* @return <code>true</code> if the expression could be transformed to LaTeX
*/
public boolean iteratorStep(final StringBuffer buf, final String mathSymbol, final IAST f, int i) {
if (i >= f.size()) {
buf.append(" ");
fFactory.convertSubExpr(buf, f.arg1(), 0);
return true;
}
if (f.get(i).isList()) {
IIterator iterator = Iterator.create((IAST) f.get(i), EvalEngine.get());
if (iterator.isValidVariable() && iterator.getStep().isOne()) {
buf.append(mathSymbol);
buf.append("_{");
fFactory.convertSubExpr(buf, iterator.getVariable(), 0);
buf.append(" = ");
fFactory.convertSubExpr(buf, iterator.getLowerLimit(), 0);
buf.append("}^{");
fFactory.convert(buf, iterator.getUpperLimit(), 0);
buf.append('}');
if (!iteratorStep(buf, mathSymbol, f, i + 1)) {
return false;
}
return true;
}
} else if (f.get(i).isSymbol()) {
ISymbol symbol = (ISymbol) f.get(i);
buf.append(mathSymbol);
buf.append("_{");
fFactory.convertSymbol(buf, symbol);
buf.append("}");
if (!iteratorStep(buf, mathSymbol, f, i + 1)) {
return false;
}
return true;
}
return false;
}
use of org.matheclipse.core.generic.interfaces.IIterator in project symja_android_library by axkr.
the class Product method evaluate.
@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
Validate.checkRange(ast, 3);
IExpr arg1 = ast.arg1();
if (arg1.isAST()) {
arg1 = F.expand(arg1, false, false);
if (!arg1.isPresent()) {
arg1 = ast.arg1();
}
}
if (arg1.isTimes()) {
IAST prod = ast.setAtCopy(1, null);
return ((IAST) arg1).mapThread(prod, 1);
}
IExpr temp = evaluateTableThrow(ast, Times(), Times(), engine);
if (temp.isPresent()) {
return temp;
}
if (arg1.isPower()) {
IExpr powArg2 = arg1.getAt(2);
boolean flag = true;
// Prod( i^a, {i,from,to},... )
for (int i = 2; i < ast.size(); i++) {
IIterator iterator = Iterator.create((IAST) ast.get(i), engine);
if (iterator.isValidVariable() && powArg2.isFree(iterator.getVariable())) {
continue;
}
flag = false;
break;
}
if (flag) {
IAST prod = ast.copy();
prod.set(1, arg1.getAt(1));
return F.Power(prod, powArg2);
}
}
IExpr argN = ast.get(ast.size() - 1);
if (ast.size() >= 3 && argN.isList()) {
IIterator iterator = Iterator.create((IAST) argN, engine);
if (iterator.isValidVariable()) {
if (iterator.getLowerLimit().isInteger() && iterator.getUpperLimit().isSymbol() && iterator.getStep().isOne()) {
final ISymbol var = iterator.getVariable();
final IInteger from = (IInteger) iterator.getLowerLimit();
final ISymbol to = (ISymbol) iterator.getUpperLimit();
if (arg1.isPower()) {
IExpr powArg1 = arg1.getAt(1);
IExpr powArg2 = arg1.getAt(2);
if (powArg1.isFree(var)) {
if (iterator.getLowerLimit().isOne()) {
if (powArg2.equals(var)) {
// Prod( a^i, ..., {i,from,to} )
if (ast.isAST2()) {
return F.Power(powArg1, Times(C1D2, to, Plus(C1, to)));
}
IAST result = ast.clone();
result.remove(ast.size() - 1);
result.set(1, F.Power(powArg1, Times(C1D2, to, Plus(C1, to))));
return result;
}
}
}
}
if (arg1.isFree(var)) {
if (ast.isAST2()) {
if (from.isOne()) {
return F.Power(ast.arg1(), to);
}
if (from.isZero()) {
return F.Power(ast.arg1(), Plus(to, C1));
}
} else {
IAST result = ast.clone();
result.remove(ast.size() - 1);
if (from.isOne()) {
result.set(1, F.Power(ast.arg1(), to));
return result;
}
if (from.isZero()) {
result.set(1, F.Power(ast.arg1(), Plus(to, C1)));
return result;
}
}
}
}
}
IAST resultList = Times();
temp = evaluateLast(ast.arg1(), iterator, resultList, C1);
if (!temp.isPresent() || temp.equals(resultList)) {
return F.NIL;
}
if (ast.isAST2()) {
return temp;
} else {
IAST result = ast.clone();
result.remove(ast.size() - 1);
result.set(1, temp);
return result;
}
}
return F.NIL;
}
use of org.matheclipse.core.generic.interfaces.IIterator in project symja_android_library by axkr.
the class Sum method iteratorStep.
public boolean iteratorStep(final StringBuffer buf, final String mathSymbol, final IAST f, int i) {
if (i >= f.size()) {
fFactory.convert(buf, f.arg1(), 0);
return true;
}
fFactory.tagStart(buf, "mrow");
if (f.get(i).isList()) {
IIterator iterator = Iterator.create((IAST) f.get(i), EvalEngine.get());
if (iterator.isValidVariable() && iterator.getStep().isOne()) {
fFactory.tagStart(buf, "munderover");
fFactory.tag(buf, "mo", mathSymbol);
fFactory.tagStart(buf, "mrow");
fFactory.convertSymbol(buf, iterator.getVariable());
fFactory.tag(buf, "mo", "=");
fFactory.convert(buf, iterator.getLowerLimit(), 0);
fFactory.tagEnd(buf, "mrow");
fFactory.convert(buf, iterator.getUpperLimit(), 0);
fFactory.tagEnd(buf, "munderover");
if (!iteratorStep(buf, mathSymbol, f, i + 1)) {
return false;
}
fFactory.tagEnd(buf, "mrow");
return true;
}
} else if (f.get(i).isSymbol()) {
ISymbol symbol = (ISymbol) f.get(i);
fFactory.tagStart(buf, "munderover");
fFactory.tag(buf, "mo", mathSymbol);
fFactory.tagStart(buf, "mrow");
fFactory.convertSymbol(buf, symbol);
fFactory.tagEnd(buf, "mrow");
// empty <mi> </mi>
fFactory.tagStart(buf, "mi");
fFactory.tagEnd(buf, "mi");
fFactory.tagEnd(buf, "munderover");
if (!iteratorStep(buf, mathSymbol, f, i + 1)) {
return false;
}
fFactory.tagEnd(buf, "mrow");
return true;
}
return false;
}