Search in sources :

Example 1 with IASTMutable

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;
}
Also used : IASTMutable(org.matheclipse.core.interfaces.IASTMutable) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 2 with IASTMutable

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;
}
Also used : IAST(org.matheclipse.core.interfaces.IAST) IASTMutable(org.matheclipse.core.interfaces.IASTMutable) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 3 with IASTMutable

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;
}
Also used : IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) IAST(org.matheclipse.core.interfaces.IAST) IASTMutable(org.matheclipse.core.interfaces.IASTMutable) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 4 with IASTMutable

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);
}
Also used : IExpr(org.matheclipse.core.interfaces.IExpr) IASTMutable(org.matheclipse.core.interfaces.IASTMutable)

Example 5 with IASTMutable

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;
}
Also used : IASTMutable(org.matheclipse.core.interfaces.IASTMutable)

Aggregations

IASTMutable (org.matheclipse.core.interfaces.IASTMutable)92 IExpr (org.matheclipse.core.interfaces.IExpr)60 IAST (org.matheclipse.core.interfaces.IAST)34 IASTAppendable (org.matheclipse.core.interfaces.IASTAppendable)26 ISymbol (org.matheclipse.core.interfaces.ISymbol)12 IInteger (org.matheclipse.core.interfaces.IInteger)5 Map (java.util.Map)4 IComplex (org.matheclipse.core.interfaces.IComplex)4 IRational (org.matheclipse.core.interfaces.IRational)4 ArrayList (java.util.ArrayList)3 TreeMap (java.util.TreeMap)3 EvalEngine (org.matheclipse.core.eval.EvalEngine)3 JASConversionException (org.matheclipse.core.eval.exception.JASConversionException)3 ValidateException (org.matheclipse.core.eval.exception.ValidateException)3 INumber (org.matheclipse.core.interfaces.INumber)3 IPatternObject (org.matheclipse.core.interfaces.IPatternObject)3 ISparseArray (org.matheclipse.core.interfaces.ISparseArray)3 ASTNode (org.matheclipse.parser.client.ast.ASTNode)3 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)2 AST2Expr (org.matheclipse.core.convert.AST2Expr)2