Search in sources :

Example 1 with IASTAppendable

use of org.matheclipse.core.interfaces.IASTAppendable in project symja_android_library by axkr.

the class AbstractAST method opposite.

/**
 * {@inheritDoc}
 */
@Override
public IExpr opposite() {
    final int lhsOrdinal = (head() instanceof IBuiltInSymbol) ? ((IBuiltInSymbol) head()).ordinal() : -1;
    if (lhsOrdinal > 0) {
        if (isTimes()) {
            IExpr arg1 = arg1();
            if (arg1.isNumber()) {
                if (arg1.isMinusOne()) {
                    if (size() == 3) {
                        return arg2();
                    }
                    return rest();
                }
                return setAtCopy(1, ((INumber) arg1).negate());
            }
            IASTAppendable timesAST = copyAppendable();
            timesAST.append(1, F.CN1);
            return timesAST;
        }
        if (isNegativeInfinity()) {
            return F.CInfinity;
        }
        if (isInfinity()) {
            return F.CNInfinity;
        }
        if (isPlus()) {
            return map(x -> x.negate(), 1);
        }
    }
    return F.Times(F.CN1, this);
// return F.eval(F.Times(F.CN1, this));
}
Also used : IBuiltInSymbol(org.matheclipse.core.interfaces.IBuiltInSymbol) IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 2 with IASTAppendable

use of org.matheclipse.core.interfaces.IASTAppendable in project symja_android_library by axkr.

the class AbstractAST method partition.

/**
 * {@inheritDoc}
 */
@Override
public final IAST partition(ISymbol operator, Predicate<? super IExpr> predicate, IExpr init1, IExpr init2, ISymbol combiner, ISymbol action) {
    if (head().equals(operator)) {
        IASTAppendable result = F.ast(action, 3);
        final int size = size();
        int newSize = size / 2;
        if (newSize <= 4) {
            newSize = 5;
        } else {
            newSize += 4;
        }
        IASTAppendable yesAST = F.ast(combiner, newSize);
        IASTAppendable noAST = F.ast(combiner, newSize);
        forEach(size, x -> {
            if (predicate.test(x)) {
                yesAST.append(x);
            } else {
                noAST.append(x);
            }
        });
        if (yesAST.size() > 1) {
            result.append(F.eval(yesAST));
        } else {
            result.append(init1);
        }
        if (noAST.size() > 1) {
            result.append(F.eval(noAST));
        } else {
            result.append(init2);
        }
        return result;
    }
    return F.NIL;
}
Also used : IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable)

Example 3 with IASTAppendable

use of org.matheclipse.core.interfaces.IASTAppendable in project symja_android_library by axkr.

the class AbstractAST method removeAtClone.

/**
 * {@inheritDoc}
 */
@Override
public final IASTAppendable removeAtClone(int position) {
    IASTAppendable ast = copyAppendable();
    ast.remove(position);
    return ast;
}
Also used : IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable)

Example 4 with IASTAppendable

use of org.matheclipse.core.interfaces.IASTAppendable 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 5 with IASTAppendable

use of org.matheclipse.core.interfaces.IASTAppendable in project symja_android_library by axkr.

the class AbstractAST method apply.

@Override
public IAST apply(final IExpr head, final int start, final int end) {
    final IASTAppendable ast = F.ast(head, end - start);
    ast.appendArgs(start, end, i -> get(i));
    // }
    return ast;
}
Also used : IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable)

Aggregations

IASTAppendable (org.matheclipse.core.interfaces.IASTAppendable)363 IExpr (org.matheclipse.core.interfaces.IExpr)219 IAST (org.matheclipse.core.interfaces.IAST)130 ISymbol (org.matheclipse.core.interfaces.ISymbol)36 IASTMutable (org.matheclipse.core.interfaces.IASTMutable)30 IInteger (org.matheclipse.core.interfaces.IInteger)29 Map (java.util.Map)28 EvalEngine (org.matheclipse.core.eval.EvalEngine)20 PrettyPrint (edu.jas.kern.PrettyPrint)13 SortedMap (java.util.SortedMap)13 ArrayList (java.util.ArrayList)12 F (org.matheclipse.core.expression.F)12 BigRational (edu.jas.arith.BigRational)10 LogManager (org.apache.logging.log4j.LogManager)10 Logger (org.apache.logging.log4j.Logger)10 ExpVector (edu.jas.poly.ExpVector)9 HashMap (java.util.HashMap)9 IBuiltInSymbol (org.matheclipse.core.interfaces.IBuiltInSymbol)8 IStringX (org.matheclipse.core.interfaces.IStringX)8 ASTNode (org.matheclipse.parser.client.ast.ASTNode)8