Search in sources :

Example 46 with IAST

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

the class UnaryBindIth method apply.

/**
	 * Clone the given AST and set the i-th argument of the new AST to {@code arg}
	 * .
	 * 
	 * @param index
	 *          the i-th index should be used
	 * @param arg
	 *          the i-th argument in the new AST
	 */
public IExpr apply(int index, final IExpr arg) {
    final IAST ast = fConstant.copy();
    ast.set(index, arg);
    return ast;
}
Also used : IAST(org.matheclipse.core.interfaces.IAST)

Example 47 with IAST

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

the class HashedOrderlessMatcher method evaluateRepeated.

/**
	 * Evaluate an <code>Orderless</code> AST with the defined
	 * <code>HashedPatternRules</code> as long as the header of the given
	 * expression equals the evaluated expression.
	 * 
	 * @param orderlessAST
	 * @return
	 * @see HashedPatternRules
	 */
public IAST evaluateRepeated(final IAST orderlessAST) {
    if (orderlessAST.isEvalFlagOn(IAST.IS_HASH_EVALED)) {
        return F.NIL;
    }
    IAST temp = orderlessAST;
    if (checkMinimmumASTs(temp)) {
        boolean evaled = false;
        int[] hashValues;
        if (!fPatternHashRuleMap.isEmpty()) {
            IExpr head = orderlessAST.head();
            IAST result = F.NIL;
            while (temp.isPresent()) {
                hashValues = new int[(temp.size() - 1)];
                HashValueVisitor v = new HashValueVisitor();
                for (int i = 0; i < hashValues.length; i++) {
                    hashValues[i] = temp.get(i + 1).accept(v);
                    v.setUp();
                }
                result = evaluateHashedValues(temp, fPatternHashRuleMap, hashValues);
                if (result.isPresent()) {
                    temp = result;
                    evaled = true;
                    if (!temp.head().equals(head)) {
                        temp.setEvalFlags(IAST.IS_HASH_EVALED);
                        return temp;
                    }
                } else {
                    break;
                }
            }
        }
        if (!fHashRuleMap.isEmpty()) {
            hashValues = new int[(temp.size() - 1)];
            for (int i = 0; i < hashValues.length; i++) {
                hashValues[i] = temp.get(i + 1).head().hashCode();
            }
            IAST result = evaluateHashedValues(temp, fHashRuleMap, hashValues);
            if (result.isPresent()) {
                result.setEvalFlags(IAST.IS_HASH_EVALED);
                return result;
            }
        }
        if (evaled) {
            temp.setEvalFlags(IAST.IS_HASH_EVALED);
            return temp;
        }
    }
    orderlessAST.setEvalFlags(IAST.IS_HASH_EVALED);
    return F.NIL;
}
Also used : HashValueVisitor(org.matheclipse.core.visit.HashValueVisitor) IAST(org.matheclipse.core.interfaces.IAST) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 48 with IAST

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

the class HashedOrderlessMatcher method evaluateHashedValues.

private static IAST evaluateHashedValues(final IAST orderlessAST, OpenIntToList<AbstractHashedPatternRules> hashRuleMap, int[] hashValues) {
    boolean evaled = false;
    IAST result = orderlessAST.copyHead();
    for (int i = 0; i < hashValues.length - 1; i++) {
        if (hashValues[i] == 0) {
            // already used entry
            continue;
        }
        evaled: for (int j = i + 1; j < hashValues.length; j++) {
            if (hashValues[j] == 0) {
                // already used entry
                continue;
            }
            final List<AbstractHashedPatternRules> hashRuleList = hashRuleMap.get(AbstractHashedPatternRules.calculateHashcode(hashValues[i], hashValues[j]));
            if (hashRuleList != null) {
                for (AbstractHashedPatternRules hashRule : hashRuleList) {
                    if (!hashRule.isPattern1() && !hashRule.isPattern2()) {
                        if (hashValues[i] != hashRule.getHash1() || hashValues[j] != hashRule.getHash2()) {
                            if (hashValues[i] != hashRule.getHash2() || hashValues[j] != hashRule.getHash1()) {
                                // hash code of both entries aren't matching
                                continue;
                            }
                            if (updateHashValues(result, orderlessAST, hashRule, hashValues, j, i)) {
                                evaled = true;
                                break evaled;
                            }
                            continue;
                        }
                        if (updateHashValues(result, orderlessAST, hashRule, hashValues, i, j)) {
                            evaled = true;
                            break evaled;
                        }
                        if (hashValues[i] != hashRule.getHash2() || hashValues[j] != hashRule.getHash1()) {
                            // hash code of both entries aren't matching
                            continue;
                        }
                        if (updateHashValues(result, orderlessAST, hashRule, hashValues, j, i)) {
                            evaled = true;
                            break evaled;
                        }
                        continue;
                    }
                    if (updateHashValues(result, orderlessAST, hashRule, hashValues, i, j)) {
                        evaled = true;
                        break evaled;
                    }
                    if (updateHashValues(result, orderlessAST, hashRule, hashValues, j, i)) {
                        evaled = true;
                        break evaled;
                    }
                }
            }
        }
    }
    if (evaled) {
        // append the rest of the unevaluated arguments
        for (int i = 0; i < hashValues.length; i++) {
            if (hashValues[i] != 0) {
                result.append(orderlessAST.get(i + 1));
            }
        }
        return result;
    }
    return F.NIL;
}
Also used : List(java.util.List) OpenIntToList(org.matheclipse.core.eval.util.OpenIntToList) IAST(org.matheclipse.core.interfaces.IAST)

Example 49 with IAST

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

the class PatternMap method isPatternTest.

public boolean isPatternTest(IExpr expr, IExpr patternTest) {
    IExpr temp = substitutePatternOrSymbols(expr);
    if (temp == null) {
        temp = expr;
    }
    IAST test = F.unaryAST1(patternTest, null);
    EvalEngine engine = EvalEngine.get();
    if (temp.isSequence()) {
        for (int i = 1; i < ((IAST) temp).size(); i++) {
            test.set(1, ((IAST) temp).get(i));
            if (!engine.evalTrue(test)) {
                return false;
            }
        }
        return true;
    }
    test.set(1, temp);
    if (!engine.evalTrue(test)) {
        return false;
    }
    return true;
}
Also used : EvalEngine(org.matheclipse.core.eval.EvalEngine) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST)

Example 50 with IAST

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

the class PatternMap method determinePatternsRecursive.

/**
	 * Determine all patterns (i.e. all objects of instance IPattern) in the
	 * given expression
	 * 
	 * Increments this classes pattern counter.
	 * 
	 * @param patternIndexMap
	 * @param lhsPatternExpr
	 *            the (left-hand-side) expression which could contain pattern
	 *            objects.
	 * @param treeLevel
	 *            TODO
	 */
private int determinePatternsRecursive(Map<IExpr, Integer> patternIndexMap, final IAST lhsPatternExpr, int treeLevel) {
    final IAST ast = lhsPatternExpr;
    if (lhsPatternExpr.isAST(F.Except, 2, 3)) {
        fRuleWithoutPattern = false;
    }
    int listEvalFlags = IAST.NO_FLAG;
    for (int i = 0; i < ast.size(); i++) {
        IExpr temp = ast.get(i);
        if (temp.isAST()) {
            listEvalFlags |= determinePatternsRecursive(patternIndexMap, (IAST) temp, treeLevel + 1);
            fPriority -= 11;
        } else if (temp instanceof IPatternObject) {
            int[] result = ((IPatternObject) temp).addPattern(this, patternIndexMap);
            listEvalFlags |= result[0];
            fPriority -= result[1];
        } else {
            fPriority -= (50 - treeLevel);
        }
    }
    ast.setEvalFlags(listEvalFlags);
    //		listEvalFlags &= IAST.CONTAINS_NO_DEFAULT_PATTERN_MASK;
    return listEvalFlags;
}
Also used : IPatternObject(org.matheclipse.core.interfaces.IPatternObject) IAST(org.matheclipse.core.interfaces.IAST) IExpr(org.matheclipse.core.interfaces.IExpr)

Aggregations

IAST (org.matheclipse.core.interfaces.IAST)413 IExpr (org.matheclipse.core.interfaces.IExpr)248 ISymbol (org.matheclipse.core.interfaces.ISymbol)76 IInteger (org.matheclipse.core.interfaces.IInteger)34 WrongArgumentType (org.matheclipse.core.eval.exception.WrongArgumentType)30 ISignedNumber (org.matheclipse.core.interfaces.ISignedNumber)22 ExpVector (edu.jas.poly.ExpVector)15 ArrayList (java.util.ArrayList)14 BigRational (edu.jas.arith.BigRational)13 JASIExpr (org.matheclipse.core.convert.JASIExpr)13 VariablesSet (org.matheclipse.core.convert.VariablesSet)13 INum (org.matheclipse.core.interfaces.INum)13 ExprPolynomial (org.matheclipse.core.polynomials.ExprPolynomial)12 GenPolynomial (edu.jas.poly.GenPolynomial)11 JASConversionException (org.matheclipse.core.eval.exception.JASConversionException)11 IFraction (org.matheclipse.core.interfaces.IFraction)11 INumber (org.matheclipse.core.interfaces.INumber)11 IComplex (org.matheclipse.core.interfaces.IComplex)10 ModLong (edu.jas.arith.ModLong)9 ExprPolynomialRing (org.matheclipse.core.polynomials.ExprPolynomialRing)9