Search in sources :

Example 61 with IASTMutable

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

the class IntervalSym method normalize.

/**
 * The list of intervals are sorted and overlapping intervals are merged.
 *
 * @param intervalList
 * @param engine
 * @return <code>F.NIL</code> if the interval could not be normalized
 */
public static IAST normalize(final IAST intervalList, EvalEngine engine) {
    try {
        IASTAppendable result = intervalList.copyAppendable();
        boolean evaled = false;
        for (int i = 1; i < intervalList.size(); i++) {
            IAST temp = normalizeArgument(intervalList.get(i), engine);
            if (temp.isPresent()) {
                evaled = true;
                result.set(i, temp);
            }
        }
        if (EvalAttributes.sort(result, INTERVAL_COMPARATOR)) {
            evaled = true;
        }
        result.addEvalFlags(IAST.BUILT_IN_EVALED);
        if (result.size() > 2) {
            int j = 1;
            IAST list1 = (IAST) result.arg1();
            IExpr min1 = list1.arg1();
            IExpr max1 = list1.arg2();
            int i = 2;
            while (i < result.size()) {
                IAST list2 = (IAST) result.get(i);
                IExpr min2 = list2.arg1();
                IExpr max2 = list2.arg2();
                if (min2.lessEqual(max1).isTrue()) {
                    if (max2.lessEqual(max1).isTrue()) {
                        evaled = true;
                        result.remove(i);
                    } else {
                        evaled = true;
                        result.remove(i);
                        list1 = F.list(min1, max2);
                        max1 = max2;
                    }
                    continue;
                }
                result.set(j++, list1);
                list1 = list2;
                min1 = min2;
                max1 = max2;
                i++;
            }
            result.set(j, list1);
        }
        if (evaled) {
            return result;
        }
        if (intervalList instanceof IASTMutable) {
            intervalList.addEvalFlags(IAST.BUILT_IN_EVALED);
            if (EvalAttributes.sort((IASTMutable) intervalList, INTERVAL_COMPARATOR)) {
                return intervalList;
            }
        }
        return F.NIL;
    // return intervalList;
    } catch (RuntimeException rex) {
        LOGGER.log(engine.getLogLevel(), "Interval", rex);
    }
    return F.NIL;
}
Also used : IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) IAST(org.matheclipse.core.interfaces.IAST) IExpr(org.matheclipse.core.interfaces.IExpr) IASTMutable(org.matheclipse.core.interfaces.IASTMutable)

Example 62 with IASTMutable

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

the class AbstractAST method setPart.

@Override
public IExpr setPart(IExpr value, final int... positions) {
    IExpr expr = this;
    int size = positions.length;
    for (int i = 0; i < size; i++) {
        if (!expr.isAST()) {
            break;
        }
        IASTMutable ast = (IASTMutable) expr;
        expr = ast.get(positions[i]);
        if (i == (size - 1)) {
            ast.set(positions[i], value);
            return expr;
        }
    }
    return null;
}
Also used : IExpr(org.matheclipse.core.interfaces.IExpr) IASTMutable(org.matheclipse.core.interfaces.IASTMutable)

Example 63 with IASTMutable

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

the class ASTAssociation method values.

protected IASTMutable values(IBuiltInSymbol symbol) {
    IASTMutable list = copyAST();
    list.set(0, symbol);
    return list;
}
Also used : IASTMutable(org.matheclipse.core.interfaces.IASTMutable)

Example 64 with IASTMutable

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

the class B3 method setAtCopy.

@Override
public IASTMutable setAtCopy(int i, IExpr expr) {
    if (i == 0) {
        return new AST3(expr, arg1(), arg2(), arg3());
    }
    IASTMutable ast = copy();
    ast.set(i, expr);
    return ast;
}
Also used : IASTMutable(org.matheclipse.core.interfaces.IASTMutable)

Example 65 with IASTMutable

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

the class SparseArrayExpr method patternPositionsList.

/**
 * Fill the sparse array by evaluating the rules left-hand-side as a pattern-matching expression.
 *
 * @param trie the internal trie structure of the sparse array
 * @param ruleLHS
 * @param ruleRHS
 * @param dimension the dimension of the sparse array
 * @param arrayRulesList parameter for error message handling
 * @param engine
 * @return
 */
private static boolean patternPositionsList(final Trie<int[], IExpr> trie, IExpr ruleLHS, IExpr ruleRHS, int[] dimension, IAST arrayRulesList, EvalEngine engine) {
    if (dimension == null) {
        return false;
    }
    final int depth = dimension.length;
    PatternMatcherAndEvaluator matcher = new PatternMatcherAndEvaluator(ruleLHS, ruleRHS);
    if (matcher.isRuleWithoutPatterns()) {
        // The left hand side of `2` in `1` doesn't match an int-array of depth `3`.
        IOFunctions.printMessage(S.SparseArray, "posr", F.list(arrayRulesList, ruleLHS, F.ZZ(depth)), EvalEngine.get());
        return false;
    }
    IASTMutable positionList = F.constantArray(F.C1, depth);
    int[] positionsKey = new int[depth];
    IPatternMap patternMap = matcher.getPatternMap();
    IExpr[] patternValuesArray = patternMap.copyPattern();
    patternPositionsRecursive(trie, dimension, engine, matcher, positionList, 0, positionsKey, patternMap, patternValuesArray);
    return true;
}
Also used : IPatternMap(org.matheclipse.core.patternmatching.IPatternMap) IASTMutable(org.matheclipse.core.interfaces.IASTMutable) IExpr(org.matheclipse.core.interfaces.IExpr) PatternMatcherAndEvaluator(org.matheclipse.core.patternmatching.PatternMatcherAndEvaluator)

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