use of org.matheclipse.core.interfaces.IASTAppendable in project symja_android_library by axkr.
the class PlusOp method addMerge.
/**
* Add or merge the <code>key, value</code> pair into the given <code>plusMap</code>.
*
* @param key the key expression
* @param value the value expression
*/
private boolean addMerge(final IExpr key, final IExpr value) {
final Map<IExpr, IExpr> map = getMap();
IExpr temp = map.get(key);
if (temp == null) {
map.put(key, value);
return false;
}
// merge both values
if (temp.isNumber() && value.isNumber()) {
temp = temp.plus(value);
if (temp.isZero()) {
map.remove(key);
return true;
}
} else if (temp.head().equals(S.Plus)) {
if (!(temp instanceof IASTAppendable)) {
temp = ((IAST) temp).copyAppendable();
}
((IASTAppendable) temp).append(value);
} else {
temp = F.Plus(temp, value);
}
map.put(key, temp);
return true;
}
use of org.matheclipse.core.interfaces.IASTAppendable in project symja_android_library by axkr.
the class VariablesSet method addAlgebraicVariables.
/**
* See the Variables() function in <code>Cos(x) + Sin(x)</code>, <code>Cos(x)</code> and <code>
* Sin(x)</code> are extracted as variables^.
*
* @param fVariablesSet
* @param expr
* @return
*/
public static IAST addAlgebraicVariables(Set<IExpr> fVariablesSet, IExpr expr) {
expr.accept(new AlgebraVariablesVisitor(fVariablesSet));
final Iterator<IExpr> iter = fVariablesSet.iterator();
final IASTAppendable list = F.ListAlloc(fVariablesSet.size());
while (iter.hasNext()) {
list.append(iter.next());
}
return list;
}
use of org.matheclipse.core.interfaces.IASTAppendable in project symja_android_library by axkr.
the class AbstractNonOrderlessArgMultiple method evaluate.
@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
int size = ast.size();
if (size == 3) {
return binaryOperator(ast, ast.arg1(), ast.arg2(), engine);
} else if (size == 2) {
return evaluateAST1(ast, engine);
} else if (size == 1) {
return evaluateAST0(ast, engine);
}
if (size > 3) {
final ISymbol sym = ast.topHead();
final IASTAppendable result = F.ast(sym);
IExpr tres;
IExpr temp = ast.arg1();
boolean evaled = false;
int i = 2;
while (i < size) {
tres = binaryOperator(ast, temp, ast.get(i), engine);
if (!tres.isPresent()) {
result.append(temp);
if (i == size - 1) {
result.append(ast.get(i));
} else {
temp = ast.get(i);
}
} else {
evaled = true;
temp = tres;
if (i == (size - 1)) {
result.append(temp);
}
}
i++;
}
if (evaled) {
if ((result.isAST1()) && sym.hasOneIdentityAttribute()) {
return result.arg1();
}
return result;
}
}
return F.NIL;
}
use of org.matheclipse.core.interfaces.IASTAppendable in project symja_android_library by axkr.
the class VariablesSet method getVarList.
/**
* Transform the set of variables into an <code>IAST</code> list of ordered variables.
*
* @return the ordered list of variables.
*/
public IASTAppendable getVarList() {
final Iterator<IExpr> iter = fVariablesSet.iterator();
final IASTAppendable list = F.ListAlloc(fVariablesSet.size());
while (iter.hasNext()) {
list.append(iter.next());
}
return list;
}
use of org.matheclipse.core.interfaces.IASTAppendable in project symja_android_library by axkr.
the class EvalAttributes method flattenDeep.
/**
* Flatten the list (i.e. the ASTs head element has the same head) example: suppose the head f
* should be flattened out:<br>
* <code>f[a,b,f[x,y,f[u,v]],z] ==> f[a,b,x,y,u,v,z]</code>
*
* @param head the head of the expression, which should be flattened.
* @param ast the <code>sublist</code> which should be added to the <code>result</code> list.
* @return the flattened ast expression if a sublist was flattened out, otherwise return <code>
* F#NIL</code>..
*/
public static IASTAppendable flattenDeep(final ISymbol head, final IAST ast) {
int[] newSize = new int[1];
newSize[0] = 0;
boolean[] flattened = new boolean[] { false };
ast.forEach(expr -> {
if (expr.isAST(head)) {
flattened[0] = true;
int temp = flattenAlloc(head, (IAST) expr);
newSize[0] += temp;
} else {
newSize[0]++;
}
});
if (flattened[0]) {
IASTAppendable result = F.ast(ast.head(), newSize[0]);
ast.forEach(expr -> {
if (expr.isAST(head)) {
result.appendArgs(flattenDeep(head, (IAST) expr).orElse((IAST) expr));
} else {
result.append(expr);
}
});
return result;
}
return F.NIL;
}
Aggregations