Search in sources :

Example 1 with PatternMatcherAndEvaluator

use of org.matheclipse.core.patternmatching.PatternMatcherAndEvaluator in project symja_android_library by axkr.

the class Functors method rules.

/**
	 * Create a functor from the given rules. If <code>astRules</code> is a
	 * <code>List[]</code> object, the elements of the list are taken as the
	 * rules of the form <code>Rule[lhs, rhs]</code>, otherwise the
	 * <code>astRules</code> itself is taken as the <code>Rule[lhs, rhs]</code>.
	 * 
	 * @param astRules
	 * @return
	 * @throws WrongArgumentType
	 */
public static Function<IExpr, IExpr> rules(@Nonnull IAST astRules) throws WrongArgumentType {
    final Map<IExpr, IExpr> equalRules;
    List<PatternMatcherAndEvaluator> matchers = new ArrayList<PatternMatcherAndEvaluator>();
    if (astRules.isList()) {
        // assuming multiple rules in a list
        IAST rule;
        int size = astRules.size() - 1;
        if (size <= 5) {
            equalRules = new OpenFixedSizeMap<IExpr, IExpr>(size * 3 - 1);
        } else {
            equalRules = new HashMap<IExpr, IExpr>();
        }
        for (final IExpr expr : astRules) {
            if (expr.isRuleAST()) {
                rule = (IAST) expr;
                addRuleToCollection(equalRules, matchers, rule);
            } else {
                throw new WrongArgumentType(astRules, astRules, -1, "Rule expression (x->y) expected: ");
            }
        }
    } else {
        if (astRules.isRuleAST()) {
            equalRules = new OpenFixedSizeMap<IExpr, IExpr>(3);
            addRuleToCollection(equalRules, matchers, astRules);
        } else {
            throw new WrongArgumentType(astRules, astRules, -1, "Rule expression (x->y) expected: ");
        }
    }
    if (matchers.size() > 0) {
        return new RulesPatternFunctor(equalRules, matchers);
    }
    return rules(equalRules);
}
Also used : WrongArgumentType(org.matheclipse.core.eval.exception.WrongArgumentType) ArrayList(java.util.ArrayList) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST) PatternMatcherAndEvaluator(org.matheclipse.core.patternmatching.PatternMatcherAndEvaluator)

Aggregations

ArrayList (java.util.ArrayList)1 WrongArgumentType (org.matheclipse.core.eval.exception.WrongArgumentType)1 IAST (org.matheclipse.core.interfaces.IAST)1 IExpr (org.matheclipse.core.interfaces.IExpr)1 PatternMatcherAndEvaluator (org.matheclipse.core.patternmatching.PatternMatcherAndEvaluator)1