Search in sources :

Example 56 with ISymbol

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

the class RulesData method removeRule.

public void removeRule(final ISymbol.RuleType setSymbol, final boolean equalRule, final IExpr leftHandSide) {
    if (equalRule) {
        if (fEqualDownRules != null) {
            fEqualDownRules.remove(leftHandSide);
            return;
        }
    }
    final PatternMatcherAndEvaluator pmEvaluator = new PatternMatcherAndEvaluator(setSymbol, leftHandSide, null);
    if (pmEvaluator.isRuleWithoutPatterns()) {
        if (fEqualDownRules != null) {
            fEqualDownRules.remove(leftHandSide);
            return;
        }
    }
    Set<ISymbol> headerSymbols = new HashSet<ISymbol>();
    if (!isComplicatedPatternRule(leftHandSide, headerSymbols)) {
        if (fSimplePatternDownRules != null) {
            final int hash = ((IAST) leftHandSide).patternHashCode();
            if (fSimplePatternDownRules.containsEntry(hash, pmEvaluator)) {
                fSimplePatternDownRules.remove(hash, pmEvaluator);
            }
        }
        return;
    } else {
        if (headerSymbols.size() > 0) {
            if (fSimpleOrderlesPatternDownRules != null) {
                for (ISymbol head : headerSymbols) {
                    final int hash = head.hashCode();
                    if (fSimpleOrderlesPatternDownRules.containsEntry(hash, pmEvaluator)) {
                        fSimpleOrderlesPatternDownRules.remove(hash, pmEvaluator);
                    }
                }
            }
            return;
        }
        if (fPatternDownRules != null) {
            fPatternDownRules.remove(pmEvaluator);
            return;
        }
    }
}
Also used : ISymbol(org.matheclipse.core.interfaces.ISymbol) IAST(org.matheclipse.core.interfaces.IAST) HashSet(java.util.HashSet)

Example 57 with ISymbol

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

the class PatternMatching method setDelayedDownRule.

public static Object[] setDelayedDownRule(IExpr leftHandSide, IExpr rightHandSide, boolean packageMode) {
    final Object[] result = new Object[] { null, rightHandSide };
    if (leftHandSide.isAST()) {
        final ISymbol lhsSymbol = ((IAST) leftHandSide).topHead();
        result[0] = lhsSymbol.putDownRule(ISymbol.RuleType.SET_DELAYED, false, leftHandSide, rightHandSide, packageMode);
        return result;
    }
    if (leftHandSide.isSymbol()) {
        final ISymbol lhsSymbol = (ISymbol) leftHandSide;
        if (lhsSymbol.hasLocalVariableStack()) {
            lhsSymbol.set(rightHandSide);
            return result;
        }
        result[0] = lhsSymbol.putDownRule(ISymbol.RuleType.SET_DELAYED, true, leftHandSide, rightHandSide, packageMode);
        return result;
    }
    throw new RuleCreationError(leftHandSide);
}
Also used : ISymbol(org.matheclipse.core.interfaces.ISymbol) IPatternObject(org.matheclipse.core.interfaces.IPatternObject) IAST(org.matheclipse.core.interfaces.IAST) RuleCreationError(org.matheclipse.core.eval.exception.RuleCreationError)

Example 58 with ISymbol

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

the class Programming method checkModuleCondition.

/**
	 * Check the (possible nested) module condition in pattern matcher without evaluating a result.
	 * 
	 * @param arg1
	 * @param arg2
	 * @param engine
	 * @return
	 */
public static boolean checkModuleCondition(IExpr arg1, IExpr arg2, final EvalEngine engine) {
    if (arg1.isList()) {
        IAST intializerList = (IAST) arg1;
        final int moduleCounter = engine.incModuleCounter();
        final String varAppend = "$" + moduleCounter;
        final java.util.Map<ISymbol, ISymbol> moduleVariables = new IdentityHashMap<ISymbol, ISymbol>();
        try {
            rememberVariables(intializerList, varAppend, moduleVariables, engine);
            IExpr result = F.subst(arg2, Functors.rules(moduleVariables));
            if (result.isCondition()) {
                return checkCondition(result.getAt(1), result.getAt(2), engine);
            } else if (result.isModule()) {
                return checkModuleCondition(result.getAt(1), result.getAt(2), engine);
            }
        } finally {
            removeUserVariables(moduleVariables);
        }
    }
    return true;
}
Also used : ISymbol(org.matheclipse.core.interfaces.ISymbol) IdentityHashMap(java.util.IdentityHashMap) IAST(org.matheclipse.core.interfaces.IAST) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 59 with ISymbol

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

the class Get method loadPackage.

/**
	 * Load a package from the given reader
	 * 
	 * @param engine
	 * @param is
	 * @return the last evaluated expression result
	 */
public static IExpr loadPackage(final EvalEngine engine, final Reader is) {
    final BufferedReader r = new BufferedReader(is);
    Context packageContext = null;
    try {
        final List<ASTNode> node = parseReader(r, engine);
        IExpr temp;
        int i = 0;
        AST2Expr ast2Expr = AST2Expr.CONST;
        if (engine.isRelaxedSyntax()) {
            ast2Expr = AST2Expr.CONST_LC;
        }
        IExpr result = F.Null;
        while (i < node.size()) {
            temp = ast2Expr.convert(node.get(i++), engine);
            if (temp.isAST()) {
                IAST ast = (IAST) temp;
                IExpr head = temp.head();
                if (head.equals(F.BeginPackage) && ast.size() >= 2) {
                    String contextName = Validate.checkContextName(ast, 1);
                    packageContext = new Context(contextName);
                    ISymbol endSymbol = F.EndPackage;
                    for (int j = 2; j < ast.size(); j++) {
                        FileReader reader = new FileReader(ast.get(j).toString());
                        Get.loadPackage(engine, reader);
                        reader.close();
                    }
                    i = addContextToPath(new ContextPath(packageContext), node, i, engine, endSymbol);
                    continue;
                } else if (head.equals(F.Begin) && ast.size() >= 2) {
                    String contextName = Validate.checkContextName(ast, 1);
                    ISymbol endSymbol = F.End;
                    i = addContextToPath(new ContextPath(contextName), node, i, engine, endSymbol);
                    continue;
                }
            }
            result = engine.evaluate(temp);
        }
        return result;
    } catch (final Exception e) {
        e.printStackTrace();
    } finally {
        if (packageContext != null) {
            engine.getContextPath().add(packageContext);
        }
        try {
            r.close();
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return F.Null;
}
Also used : Context(org.matheclipse.core.expression.Context) ISymbol(org.matheclipse.core.interfaces.ISymbol) IOException(java.io.IOException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) AST2Expr(org.matheclipse.core.convert.AST2Expr) ContextPath(org.matheclipse.core.expression.ContextPath) BufferedReader(java.io.BufferedReader) ASTNode(org.matheclipse.parser.client.ast.ASTNode) FileReader(java.io.FileReader) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST)

Example 60 with ISymbol

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

the class Package method evalPackage.

public static void evalPackage(IAST publicSymbols, IAST list, EvalEngine engine) {
    HashMap<String, ISymbol> convertedSymbolMap = new HashMap<String, ISymbol>();
    HashSet<ISymbol> publicSymbolSet = new HashSet<ISymbol>();
    ISymbol toSymbol;
    for (int i = 1; i < publicSymbols.size(); i++) {
        IExpr expr = publicSymbols.get(i);
        if (expr.isSymbol()) {
            publicSymbolSet.add((ISymbol) expr);
            toSymbol = F.predefinedSymbol(((ISymbol) expr).toString());
            convertedSymbolMap.put(expr.toString(), toSymbol);
        } else if (expr instanceof IStringX) {
            String symbolStr = ((IStringX) expr).toString();
            toSymbol = F.predefinedSymbol(symbolStr);
            publicSymbolSet.add(toSymbol);
            if (Config.PARSER_USE_LOWERCASE_SYMBOLS) {
                symbolStr = symbolStr.toLowerCase(Locale.ENGLISH);
            }
            convertedSymbolMap.put(symbolStr, toSymbol);
        }
    }
    // determine "private package rule headers" in convertedSymbolMap
    for (int i = 1; i < list.size(); i++) {
        if (list.get(i).isAST()) {
            determineRuleHead((IAST) list.get(i), publicSymbolSet, convertedSymbolMap);
        }
    }
    // convert the rules into a new list:
    IAST resultList = F.List();
    for (int i = 1; i < list.size(); i++) {
        resultList.append(convertSymbolsInExpr(list.get(i), convertedSymbolMap));
    }
    try {
        engine.setPackageMode(true);
        // evaluate the new converted rules
        for (int i = 1; i < resultList.size(); i++) {
            engine.evaluate(resultList.get(i));
        }
    } finally {
        engine.setPackageMode(false);
    }
}
Also used : ISymbol(org.matheclipse.core.interfaces.ISymbol) HashMap(java.util.HashMap) IExpr(org.matheclipse.core.interfaces.IExpr) IStringX(org.matheclipse.core.interfaces.IStringX) IAST(org.matheclipse.core.interfaces.IAST) HashSet(java.util.HashSet)

Aggregations

ISymbol (org.matheclipse.core.interfaces.ISymbol)117 IExpr (org.matheclipse.core.interfaces.IExpr)79 IAST (org.matheclipse.core.interfaces.IAST)76 IInteger (org.matheclipse.core.interfaces.IInteger)18 WrongArgumentType (org.matheclipse.core.eval.exception.WrongArgumentType)12 INum (org.matheclipse.core.interfaces.INum)10 ISignedNumber (org.matheclipse.core.interfaces.ISignedNumber)10 IFraction (org.matheclipse.core.interfaces.IFraction)9 IBuiltInSymbol (org.matheclipse.core.interfaces.IBuiltInSymbol)8 IComplex (org.matheclipse.core.interfaces.IComplex)7 IComplexNum (org.matheclipse.core.interfaces.IComplexNum)7 ArrayList (java.util.ArrayList)6 IPatternObject (org.matheclipse.core.interfaces.IPatternObject)6 HashSet (java.util.HashSet)5 EvalEngine (org.matheclipse.core.eval.EvalEngine)5 Num (org.matheclipse.core.expression.Num)5 GenPolynomial (edu.jas.poly.GenPolynomial)4 Options (org.matheclipse.core.eval.util.Options)4 ExprPolynomialRing (org.matheclipse.core.polynomials.ExprPolynomialRing)4 ExpVector (edu.jas.poly.ExpVector)3