Search in sources :

Example 61 with EvalEngine

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

the class ExprEvaluatorTests method nonBuiltinFunctionFuzz.

public static void nonBuiltinFunctionFuzz() {
    Config.MAX_AST_SIZE = 10000;
    Config.MAX_OUTPUT_SIZE = 10000;
    Config.MAX_INPUT_LEAVES = 100L;
    Config.MAX_MATRIX_DIMENSION_SIZE = 100;
    Config.MAX_PRECISION_APFLOAT = 100;
    Config.MAX_BIT_LENGTH = 200000;
    Config.MAX_POLYNOMIAL_DEGREE = 100;
    Config.FILESYSTEM_ENABLED = false;
    EvalEngine engine = new EvalEngine(true);
    engine.setRecursionLimit(256);
    engine.setIterationLimit(1000);
    ExprEvaluator eval = new ExprEvaluator(engine, true, (short) 20);
    byte[] bArray = new byte[0];
    ByteArrayExpr ba = ByteArrayExpr.newInstance(bArray);
    byte[] b0Array = new byte[] { 0 };
    ByteArrayExpr b0a = ByteArrayExpr.newInstance(b0Array);
    F.x.setAttributes(ISymbol.PROTECTED);
    F.y.setAttributes(ISymbol.PROTECTED);
    IAST seedList = // 
    F.List(// 
    ba, // 
    b0a, // 
    F.complex(-0.5, 0.5), // 
    F.complex(0.0, 0.5), // 
    F.complex(0.0, -1.0), // 
    F.complex(0.0, 1.0), // 
    F.num(0.5), // 
    F.num(-0.5), // 
    F.num(Math.PI * (-0.5)), // 
    F.num(Math.PI * 0.5), // 
    F.num(-Math.PI), // 
    F.num(Math.PI), // 
    F.num(-Math.E), // 
    F.num(Math.E), // 
    F.C0, // 
    F.C1, // 
    F.CN1, // 
    F.CN1D2, // 
    F.C1D2, // 
    F.CNI, // 
    F.CI, // 
    F.CInfinity, // 
    F.CNInfinity, // 
    F.Null, // 
    F.Power(F.x, F.C2), // 
    F.Indeterminate, // 
    F.ComplexInfinity, // 
    F.x_, // 
    F.y_, // 
    F.CEmptyList, // 
    F.assoc(F.List(F.Rule(F.a, F.C0), F.RuleDelayed(F.b, F.C1))), // 
    F.assoc(F.List()), // 
    F.assoc(F.List(F.Rule(F.stringx("s1"), F.C0), F.RuleDelayed(F.stringx("s2"), F.C1))), // 
    SparseArrayExpr.newDenseList(F.List(F.C0, F.C0), F.C0), // 
    SparseArrayExpr.newDenseList(F.List(F.C0, F.C1, F.C0, F.C2), F.C0), // 
    SparseArrayExpr.newDenseList(F.List(F.List(F.C0, F.C0), F.List(F.C0, F.C0)), F.C0), // 
    SparseArrayExpr.newDenseList(F.List(F.List(F.C1, F.C0), F.List(F.C0, F.C1)), F.C0), // 
    F.List(F.List(F.C0)), // 
    F.List(F.List(F.C1)), // 
    F.List(F.List(F.CN1)), // 
    F.List(F.List(F.C1, F.C0), F.List(F.C0, F.C1)), // 
    F.List(F.List(F.C0, F.C0), F.List(F.C0, F.C0)), // 
    F.List(F.List(F.C1, F.C0), F.List(F.C0, F.C1), F.C0), // 
    F.List(F.List(F.C0, F.C0), F.List(F.C0, F.C0), F.C0), // 
    F.List(F.CN1, F.CN2, F.C3), // 
    F.List(F.CN1D2, F.CN2, F.C3), // 
    F.List(F.x, F.CN2, F.C3), // 
    F.List(F.x, F.C5, F.CN3), // 
    F.List(F.x, F.CN3, F.CN1D2), // simulate level spec
    F.List(F.CN5), // simulate level spec
    F.List(F.C7), // 
    F.C1DSqrt5, // 
    F.C2Pi, // 
    F.CN3D2, // 
    F.C3D2, // 
    F.C3D4, // 
    F.Slot2, // 
    F.stringx(""), // 
    F.stringx("\\"), // 
    F.stringx("\r"), // 
    F.stringx("\t"), // 
    F.stringx("\n"), // 
    F.stringx("\r\n"), // 
    F.stringx("\n   "), // 
    F.stringx("\uffff"), F.Subtract(F.C1, F.C1));
    String[] functionStrs = AST2Expr.FUNCTION_STRINGS;
    ThreadLocalRandom random = ThreadLocalRandom.current();
    int[] counter = new int[] { 0 };
    for (int i = 0; i < functionStrs.length; i++) {
        IBuiltInSymbol sym = (IBuiltInSymbol) F.symbol(functionStrs[i]);
        IEvaluator evaluator = sym.getEvaluator();
        if (evaluator instanceof IFunctionEvaluator) {
            continue;
        }
        generateASTs(sym, 1, 5, seedList, random, counter, null, engine, false, false);
        generateASTs(sym, 1, 5, seedList, random, counter, null, engine, false, true);
        generateASTs(sym, 1, 5, seedList, random, counter, null, engine, true, false);
        generateASTs(sym, 1, 5, seedList, random, counter, null, engine, true, true);
    }
}
Also used : ExprEvaluator(org.matheclipse.core.eval.ExprEvaluator) ByteArrayExpr(org.matheclipse.core.expression.data.ByteArrayExpr) IBuiltInSymbol(org.matheclipse.core.interfaces.IBuiltInSymbol) IEvaluator(org.matheclipse.core.interfaces.IEvaluator) IFunctionEvaluator(org.matheclipse.core.eval.interfaces.IFunctionEvaluator) EvalEngine(org.matheclipse.core.eval.EvalEngine) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) IAST(org.matheclipse.core.interfaces.IAST)

Example 62 with EvalEngine

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

the class TeXTests method testSmartFuzz.

public void testSmartFuzz() {
    boolean quietMode = true;
    EvalEngine engine = EvalEngine.get();
    List<ASTNode> node = parseFileToList();
    IExpr temp;
    TeXFormFactory fTeXFactory = new TeXFormFactory();
    OutputFormFactory fInputFactory = OutputFormFactory.get(true, false, 5, 7);
    fInputFactory.setInputForm(true);
    AST2Expr ast2Expr = new AST2Expr(engine.isRelaxedSyntax(), engine);
    IAST seedList = // 
    F.List(// 
    F.complex(-0.5, 0.5), // 
    F.complex(0.0, 0.5), // 
    F.complex(0.0, -1.0), // 
    F.complex(0.0, 1.0), // 
    F.num(-0.5), // 
    F.num(0.5), // 
    F.num(Math.PI * (-0.5)), // 
    F.num(Math.PI * 0.5), // 
    F.num(-Math.PI), // 
    F.num(Math.PI), // 
    F.num(-Math.E), // 
    F.num(Math.E), // 
    S.True, // 
    S.False, // 
    F.assoc(F.CEmptyList), // 
    F.assoc(F.List(F.Rule(F.x, F.y))), // 
    F.CEmptyList, // 
    F.List(F.Rule(F.C1, F.C0)), // 
    F.List(F.Rule(F.x, F.CN1)), // 
    F.C0, // 
    F.C1, // 
    F.CN1, // 
    F.C2, // 
    F.CN2, // 
    F.CN10, // 
    F.CN1D2, // 
    F.C1D2, // 
    F.CNI, // 
    F.CI, // 
    F.CInfinity, // 
    F.CNInfinity, // 
    F.Null, // 
    F.Power(F.x, F.C2), // 
    F.ComplexInfinity, // 
    F.x_, // 
    F.y_, // 
    F.C1DSqrt5, // 
    F.Slot1, // 
    F.stringx(""), // 
    F.stringx("\uffff"), F.Subtract(F.C1, F.C1));
    int counter = 0;
    ThreadLocalRandom random = ThreadLocalRandom.current();
    for (int j = 1; j < 10000; j++) {
        int i = 0;
        while (i < node.size()) {
            temp = ast2Expr.convert(node.get(i++));
            if (temp.isAST() && temp.size() > 1) {
                final StringBuilder buf = new StringBuilder();
                int seedIndex = random.nextInt(1, seedList.size());
                IExpr seed = seedList.get(seedIndex);
                IASTMutable mutant = ((IAST) temp).copy();
                int randomIndex = random.nextInt(1, mutant.size());
                mutant.set(randomIndex, seed);
                for (int k = 0; k < 1; k++) {
                    seedIndex = random.nextInt(1, seedList.size());
                    seed = seedList.get(seedIndex);
                    randomIndex = random.nextInt(1, mutant.size());
                    mutant.set(randomIndex, seed);
                }
                engine.init();
                engine.setQuietMode(quietMode);
                engine.setRecursionLimit(256);
                engine.setIterationLimit(1000);
                final String mutantStr = fInputFactory.toString(mutant);
                try {
                    // System.out.print(".");
                    if (counter++ > 80) {
                        // System.out.println("");
                        counter = 0;
                        System.out.flush();
                        System.err.flush();
                    }
                    // eval.eval(mutantStr);
                    fTeXFactory.convert(buf, mutant, 0);
                    System.out.println(buf.toString());
                } catch (FlowControlException mex) {
                    if (!quietMode) {
                        System.err.println(mutantStr);
                        mex.printStackTrace();
                        System.err.println();
                    }
                } catch (SyntaxError se) {
                    if (!quietMode) {
                        System.err.println(mutantStr);
                        se.printStackTrace();
                        System.err.println();
                    }
                // fail();
                } catch (MathException mex) {
                    System.err.println(mutantStr);
                    mex.printStackTrace();
                    System.err.println();
                    fail();
                } catch (RuntimeException rex) {
                    System.err.println(mutantStr);
                    rex.printStackTrace();
                    fail();
                } catch (Error rex) {
                    System.err.println(mutantStr);
                    if (rex instanceof StackOverflowError) {
                        System.err.println("java.lang.StackOverflowError");
                        rex.printStackTrace();
                    } else {
                        rex.printStackTrace();
                        fail();
                    }
                }
            }
        }
    }
// return result;
}
Also used : SyntaxError(org.matheclipse.parser.client.SyntaxError) IASTMutable(org.matheclipse.core.interfaces.IASTMutable) OutputFormFactory(org.matheclipse.core.form.output.OutputFormFactory) AST2Expr(org.matheclipse.core.convert.AST2Expr) SyntaxError(org.matheclipse.parser.client.SyntaxError) MathException(org.matheclipse.parser.client.math.MathException) FlowControlException(org.matheclipse.core.eval.exception.FlowControlException) EvalEngine(org.matheclipse.core.eval.EvalEngine) ASTNode(org.matheclipse.parser.client.ast.ASTNode) TeXFormFactory(org.matheclipse.core.form.tex.TeXFormFactory) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST)

Example 63 with EvalEngine

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

the class AbstractRubiTestCase method interpreter.

/**
 * Evaluates the given string-expression and returns the result in <code>OutputForm</code>
 */
public String interpreter(final String inputExpression, final String expectedResult, String manuallyCheckedResult) {
    IExpr result;
    final StringWriter buf = new StringWriter();
    IExpr integral = fEvaluator.parse(inputExpression).first();
    try {
        if (fSeconds <= 0) {
            result = fEvaluator.eval(inputExpression);
        } else {
            EvalEngine engine = fEvaluator.getEvalEngine();
            engine.setSeconds(fSeconds);
            result = fEvaluator.evaluateWithTimeout(inputExpression, fSeconds, TimeUnit.SECONDS, true, new EvalControlledCallable(fEvaluator.getEvalEngine()));
        }
        if (result != null) {
            return printResult(integral, result, expectedResult, manuallyCheckedResult);
        }
    } catch (final AbortException re) {
        return printResult(integral, F.$Aborted, expectedResult, manuallyCheckedResult);
    } catch (final FailedException re) {
        return printResult(integral, F.$Failed, expectedResult, manuallyCheckedResult);
    } catch (final SyntaxError se) {
        String msg = se.getMessage();
        System.err.println(msg);
        System.err.println();
        System.err.flush();
        return "";
    } catch (final RuntimeException re) {
        Throwable me = re.getCause();
        if (me instanceof MathException) {
            Validate.printException(buf, me);
        } else {
            Validate.printException(buf, re);
        }
        System.err.println(buf.toString());
        System.err.flush();
        return "";
    } catch (final Exception e) {
        Validate.printException(buf, e);
        System.err.println(buf.toString());
        System.err.flush();
        return "";
    } catch (final OutOfMemoryError e) {
        Validate.printException(buf, e);
        System.err.println(buf.toString());
        System.err.flush();
        return "";
    } catch (final StackOverflowError e) {
        Validate.printException(buf, e);
        System.err.println(buf.toString());
        System.err.flush();
        return "";
    }
    return buf.toString();
}
Also used : MathException(org.matheclipse.parser.client.math.MathException) AbortException(org.matheclipse.core.eval.exception.AbortException) FailedException(org.matheclipse.core.eval.exception.FailedException) StringWriter(java.io.StringWriter) SyntaxError(org.matheclipse.parser.client.SyntaxError) FailedException(org.matheclipse.core.eval.exception.FailedException) MathException(org.matheclipse.parser.client.math.MathException) EvalEngine(org.matheclipse.core.eval.EvalEngine) EvalControlledCallable(org.matheclipse.core.eval.EvalControlledCallable) IExpr(org.matheclipse.core.interfaces.IExpr) AbortException(org.matheclipse.core.eval.exception.AbortException)

Example 64 with EvalEngine

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

the class AbstractRubiTestCase method interpreter.

/**
 * Evaluates the given string-expression and returns the result in <code>OutputForm</code>
 */
public String interpreter(final String inputExpression, final String expectedResult, String manuallyCheckedResult) {
    IExpr result;
    final StringWriter buf = new StringWriter();
    try {
        if (fSeconds <= 0) {
            result = fEvaluator.eval(inputExpression);
        } else {
            EvalEngine engine = fEvaluator.getEvalEngine();
            engine.setSeconds(fSeconds);
            result = fEvaluator.evaluateWithTimeout(inputExpression, fSeconds, TimeUnit.SECONDS, true, new EvalControlledCallable(fEvaluator.getEvalEngine()));
        }
        if (result != null) {
            return printResult(result, expectedResult, manuallyCheckedResult);
        }
    } catch (final AbortException re) {
        return printResult(F.$Aborted, expectedResult, manuallyCheckedResult);
    } catch (final FailedException re) {
        return printResult(F.$Failed, expectedResult, manuallyCheckedResult);
    } catch (final SyntaxError se) {
        String msg = se.getMessage();
        System.err.println(msg);
        System.err.println();
        System.err.flush();
        return "";
    } catch (final RuntimeException re) {
        Throwable me = re.getCause();
        if (me instanceof MathException) {
            Validate.printException(buf, me);
        } else {
            Validate.printException(buf, re);
        }
        System.err.println(buf.toString());
        System.err.flush();
        return "";
    } catch (final Exception e) {
        Validate.printException(buf, e);
        System.err.println(buf.toString());
        System.err.flush();
        return "";
    } catch (final OutOfMemoryError e) {
        Validate.printException(buf, e);
        System.err.println(buf.toString());
        System.err.flush();
        return "";
    } catch (final StackOverflowError e) {
        Validate.printException(buf, e);
        System.err.println(buf.toString());
        System.err.flush();
        return "";
    }
    return buf.toString();
}
Also used : MathException(org.matheclipse.parser.client.math.MathException) AbortException(org.matheclipse.core.eval.exception.AbortException) FailedException(org.matheclipse.core.eval.exception.FailedException) StringWriter(java.io.StringWriter) SyntaxError(org.matheclipse.parser.client.SyntaxError) FailedException(org.matheclipse.core.eval.exception.FailedException) MathException(org.matheclipse.parser.client.math.MathException) EvalEngine(org.matheclipse.core.eval.EvalEngine) EvalControlledCallable(org.matheclipse.core.eval.EvalControlledCallable) IExpr(org.matheclipse.core.interfaces.IExpr) AbortException(org.matheclipse.core.eval.exception.AbortException)

Example 65 with EvalEngine

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

the class Blank method isConditionMatched.

@Override
public boolean isConditionMatched(final IExpr expr, PatternMap patternMap) {
    if (fCondition == null || expr.head().equals(fCondition)) {
        patternMap.setValue(this, expr);
        return true;
    }
    EvalEngine engine = EvalEngine.get();
    boolean traceMode = false;
    try {
        traceMode = engine.isTraceMode();
        engine.setTraceMode(false);
        final Predicate<IExpr> matcher = Predicates.isTrue(engine, fCondition);
        if (matcher.test(expr)) {
            patternMap.setValue(this, expr);
            return true;
        }
    } finally {
        if (traceMode) {
            engine.setTraceMode(true);
        }
    }
    return false;
}
Also used : EvalEngine(org.matheclipse.core.eval.EvalEngine) IExpr(org.matheclipse.core.interfaces.IExpr)

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