Search in sources :

Example 41 with EvalEngine

use of org.matheclipse.core.eval.EvalEngine in project symja_android_library by axkr.

the class Functors method rules.

/**
 * Create a functor from the given rules. All strings in <code>strRules</code> are parsed in
 * internal rules form.
 *
 * @param strRules array of rules of the form &quot;<code>x-&gt;y</code>&quot;
 * @return
 */
public static Function<IExpr, IExpr> rules(String[] strRules) {
    IASTAppendable astRules = F.ListAlloc(strRules.length);
    final EvalEngine engine = EvalEngine.get();
    ExprParser parser = new ExprParser(engine);
    for (String str : strRules) {
        IExpr expr = parser.parse(str);
        expr = engine.evaluate(expr);
        astRules.append(expr);
    }
    return rules(astRules, engine);
}
Also used : IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) EvalEngine(org.matheclipse.core.eval.EvalEngine) IExpr(org.matheclipse.core.interfaces.IExpr) ExprParser(org.matheclipse.core.parser.ExprParser)

Example 42 with EvalEngine

use of org.matheclipse.core.eval.EvalEngine in project symja_android_library by axkr.

the class SerializableTest method testEvalEngine.

public void testEvalEngine() {
    try {
        EvalEngine engine = EvalEngine.get();
        engine.evaluate("x=10");
        Context context = engine.getContextPath().getGlobalContext();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(baos);
        oos.writeObject(context);
        byte[] bArray = baos.toByteArray();
        baos.close();
        oos.close();
        ByteArrayInputStream bais = new ByteArrayInputStream(bArray);
        ObjectInputStream ois = new ObjectInputStream(bais);
        Context copy = (Context) ois.readObject();
        bais.close();
        ois.close();
        engine.getContextPath().setGlobalContext(copy);
        IExpr result = engine.evaluate("x");
        assertEquals("10", result.toString());
    } catch (ClassNotFoundException cnfe) {
        cnfe.printStackTrace();
        assertEquals("", cnfe.toString());
    } catch (IOException ioe) {
        ioe.printStackTrace();
        assertEquals("", ioe.toString());
    }
}
Also used : Context(org.matheclipse.core.expression.Context) ByteArrayInputStream(java.io.ByteArrayInputStream) EvalEngine(org.matheclipse.core.eval.EvalEngine) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IExpr(org.matheclipse.core.interfaces.IExpr) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream)

Example 43 with EvalEngine

use of org.matheclipse.core.eval.EvalEngine in project symja_android_library by axkr.

the class PatternMatchingTestCase method checkPriority.

public void checkPriority(String patternString, int priority) {
    try {
        EvalEngine engine = EvalEngine.get();
        ASTNode node = fParser.parse(patternString);
        IExpr pat = new AST2Expr(false, engine).convert(node);
        PatternMatcher matcher = new PatternMatcher(pat);
        assertEquals(matcher.getLHSPriority(), priority);
    } catch (Exception e) {
        e.printStackTrace();
        assertEquals(0, priority);
    }
}
Also used : EvalEngine(org.matheclipse.core.eval.EvalEngine) ASTNode(org.matheclipse.parser.client.ast.ASTNode) IExpr(org.matheclipse.core.interfaces.IExpr) PatternMatcher(org.matheclipse.core.patternmatching.PatternMatcher) AST2Expr(org.matheclipse.core.convert.AST2Expr)

Example 44 with EvalEngine

use of org.matheclipse.core.eval.EvalEngine in project symja_android_library by axkr.

the class PatternMatchingTestCase method comparePriority.

public void comparePriority(String patternString1, String patternString2, int result) {
    try {
        EvalEngine engine = EvalEngine.get();
        ASTNode node = fParser.parse(patternString1);
        IExpr pat1 = new AST2Expr(false, engine).convert(node);
        node = fParser.parse(patternString1);
        IExpr pat2 = new AST2Expr(false, engine).convert(node);
        PatternMatcher matcher1 = new PatternMatcher(pat1);
        PatternMatcher matcher2 = new PatternMatcher(pat2);
        assertEquals(matcher1.equivalentTo(matcher2), result);
    } catch (Exception e) {
        e.printStackTrace();
        assertEquals(Integer.MAX_VALUE, result);
    }
}
Also used : EvalEngine(org.matheclipse.core.eval.EvalEngine) ASTNode(org.matheclipse.parser.client.ast.ASTNode) IExpr(org.matheclipse.core.interfaces.IExpr) PatternMatcher(org.matheclipse.core.patternmatching.PatternMatcher) AST2Expr(org.matheclipse.core.convert.AST2Expr)

Example 45 with EvalEngine

use of org.matheclipse.core.eval.EvalEngine in project symja_android_library by axkr.

the class PatternMatchingTestCase method setUp.

// public void testSlotPatternMatching() {
// checkPattern("b_.* #+c_.*#^2", "#-1*#^2", "");
// checkPattern("b_.* #+c_.*#^2", "#+#^2", "[1, 1]");
// checkPattern("a_. + b_.* #+c_.*#^2", "-1+#+#^2", "[-1, 1, 1]");
// }
/**
 * The JUnit setup method
 */
@Override
protected void setUp() {
    try {
        // setup the evaluation engine (and bind to current thread)
        // F.initSymbols();
        // EvalEngine.get();
        EvalEngine engine = new EvalEngine();
        EvalEngine.set(engine);
        engine.setSessionID("SpecialTestCase");
        engine.setRecursionLimit(256);
        engine.setIterationLimit(1024 * 1024);
        util = new EvalUtilities(engine, false, false);
        // setup a parser for the math expressions
        fParser = new Parser();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : EvalUtilities(org.matheclipse.core.eval.EvalUtilities) EvalEngine(org.matheclipse.core.eval.EvalEngine) Parser(org.matheclipse.parser.client.Parser)

Aggregations

EvalEngine (org.matheclipse.core.eval.EvalEngine)131 IExpr (org.matheclipse.core.interfaces.IExpr)71 IAST (org.matheclipse.core.interfaces.IAST)39 IASTAppendable (org.matheclipse.core.interfaces.IASTAppendable)20 ISymbol (org.matheclipse.core.interfaces.ISymbol)20 IOException (java.io.IOException)13 F (org.matheclipse.core.expression.F)12 ExprEvaluator (org.matheclipse.core.eval.ExprEvaluator)11 S (org.matheclipse.core.expression.S)11 IInteger (org.matheclipse.core.interfaces.IInteger)11 ASTNode (org.matheclipse.parser.client.ast.ASTNode)11 LogManager (org.apache.logging.log4j.LogManager)10 Logger (org.apache.logging.log4j.Logger)10 AST2Expr (org.matheclipse.core.convert.AST2Expr)9 ExprParser (org.matheclipse.core.parser.ExprParser)9 IBuiltInSymbol (org.matheclipse.core.interfaces.IBuiltInSymbol)8 MathException (org.matheclipse.parser.client.math.MathException)8 ArrayList (java.util.ArrayList)7 Config (org.matheclipse.core.basic.Config)7 IASTMutable (org.matheclipse.core.interfaces.IASTMutable)7