use of org.matheclipse.core.interfaces.IASTMutable in project symja_android_library by axkr.
the class AbstractAST method mapReverse.
@Override
public IAST mapReverse(final Function<IExpr, IExpr> function) {
IASTMutable result = copy();
final int size = size();
for (int i = 1; i < size; i++) {
final IExpr arg = get(i);
final IExpr value = function.apply(arg);
result.set(size - i, value.orElse(arg));
}
return result;
}
use of org.matheclipse.core.interfaces.IASTMutable in project symja_android_library by axkr.
the class AbstractAST method variables2Slots.
/**
* Replace all elements determined by the unary <code>from</code> predicate, with the element
* generated by the unary <code>to</code> function. If the unary function returns null replaceAll
* returns null.
*
* @return <code>F.NIL</code> if no replacement occurs.
*/
private static IExpr variables2Slots(final IExpr expr, final Predicate<IExpr> from, final Function<IExpr, ? extends IExpr> to) {
if (from.test(expr)) {
return to.apply(expr);
}
if (expr.isAST()) {
IAST nestedList = (IAST) expr;
IASTMutable result;
final IExpr head = nestedList.head();
IExpr temp = variables2Slots(head, from, to);
if (temp.isPresent()) {
result = nestedList.apply(temp);
} else {
return F.NIL;
}
final int size = nestedList.size();
for (int i = 1; i < size; i++) {
temp = variables2Slots(nestedList.get(i), from, to);
if (temp.isPresent()) {
result.set(i, temp);
} else {
return F.NIL;
}
}
return result;
}
return expr;
}
use of org.matheclipse.core.interfaces.IASTMutable in project symja_android_library by axkr.
the class AbstractAST method extractConditionalExpression.
/**
* {@inheritDoc}
*/
@Override
public IExpr extractConditionalExpression(boolean isUnaryConditionalExpression) {
if (isUnaryConditionalExpression) {
// mergeConditionalExpression
IAST conditionalExpr = (IAST) arg1();
IASTMutable copy = copy();
copy.set(1, conditionalExpr.arg1());
return conditionalExpr.setAtCopy(1, copy);
}
int indx = indexOf(x -> x.isConditionalExpression());
if (indx > 0) {
IAST conditionalExpr = (IAST) get(indx);
IASTAppendable andExpr = F.And();
IASTMutable copy = copy();
copy.set(indx, conditionalExpr.arg1());
andExpr.append(conditionalExpr.arg2());
indx++;
for (int i = indx; i < copy.size(); i++) {
IExpr x = copy.get(i);
if (x.isConditionalExpression()) {
conditionalExpr = (IAST) x;
copy.set(i, conditionalExpr.arg1());
andExpr.append(conditionalExpr.arg2());
}
}
IASTMutable mergedResult = conditionalExpr.setAtCopy(1, copy);
mergedResult.set(2, andExpr);
return mergedResult;
}
return F.NIL;
}
use of org.matheclipse.core.interfaces.IASTMutable in project symja_android_library by axkr.
the class AbstractAST method map.
/**
* {@inheritDoc}
*/
@Override
public IAST map(final Function<IExpr, IExpr> function, final int startOffset) {
IExpr temp;
IASTMutable result = F.NIL;
int i = startOffset;
int size = size();
while (i < size) {
temp = function.apply(get(i));
if (temp.isPresent()) {
// something was evaluated - return a new IAST:
result = copyAppendable();
result.set(i++, temp);
break;
}
i++;
}
if (result.isPresent()) {
while (i < size) {
temp = function.apply(get(i));
if (temp.isPresent()) {
result.set(i, temp);
}
i++;
}
}
return result.orElse(this);
}
use of org.matheclipse.core.interfaces.IASTMutable in project symja_android_library by axkr.
the class B2 method setAtCopy.
@Override
public IASTMutable setAtCopy(int i, IExpr expr) {
if (i == 0) {
return new AST2(expr, arg1(), arg2());
}
IASTMutable ast = copy();
ast.set(i, expr);
return ast;
}
Aggregations