Search in sources :

Example 21 with IASTAppendable

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

Example 22 with IASTAppendable

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

Example 23 with IASTAppendable

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

Example 24 with IASTAppendable

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

Example 25 with IASTAppendable

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

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