Search in sources :

Example 36 with MathException

use of org.matheclipse.parser.client.math.MathException in project symja_android_library by axkr.

the class ExprEvaluatorTest method testStringEval005.

/**
 * See: https://github.com/axkr/symja_android_library/issues/48 why the toString() method output
 * of numeric values is different from OutputFormFactory#convert() method.
 */
public void testStringEval005() {
    try {
        ExprEvaluator util = new ExprEvaluator();
        IExpr expr = util.eval("1.2 * 1.5");
        assertEquals("1.8", expr.toString());
        StringWriter buf = new StringWriter();
        OutputFormFactory.get(util.getEvalEngine().isRelaxedSyntax()).convert(buf, expr);
        assertEquals("1.7999999999999998", buf.toString());
        buf = new StringWriter();
        // DecimalFormatSymbols usSymbols = new DecimalFormatSymbols(Locale.US);
        // DecimalFormat decimalFormat = new DecimalFormat("0.0####", usSymbols);
        OutputFormFactory.get(true, false, 5, 7).convert(buf, expr);
        assertEquals("1.8", buf.toString());
        expr = util.eval("10.0^-15");
        assertEquals("1.*10^-15", expr.toString());
        buf = new StringWriter();
        OutputFormFactory.get(util.getEvalEngine().isRelaxedSyntax()).convert(buf, expr);
        assertEquals("1.0E-15", buf.toString());
    } catch (SyntaxError e) {
        // catch Symja parser errors here
        System.out.println(e.getMessage());
    } catch (MathException me) {
        // catch Symja math errors here
        System.out.println(me.getMessage());
    } catch (Exception e) {
        e.printStackTrace();
    } catch (final StackOverflowError soe) {
        System.out.println(soe.getMessage());
    } catch (final OutOfMemoryError oome) {
        System.out.println(oome.getMessage());
    }
}
Also used : ExprEvaluator(org.matheclipse.core.eval.ExprEvaluator) StringWriter(java.io.StringWriter) SyntaxError(org.matheclipse.parser.client.SyntaxError) MathException(org.matheclipse.parser.client.math.MathException) IExpr(org.matheclipse.core.interfaces.IExpr) MathException(org.matheclipse.parser.client.math.MathException)

Example 37 with MathException

use of org.matheclipse.parser.client.math.MathException in project symja_android_library by axkr.

the class SymjaBot method interpreter.

private static String interpreter(final String trimmedInput) {
    ExprEvaluator evaluator = new ExprEvaluator(false, (short) 100);
    IExpr result;
    final StringWriter buf = new StringWriter();
    try {
        if (trimmedInput.length() > 1 && trimmedInput.charAt(0) == '?') {
            StringBuilder docBuf = new StringBuilder();
            Documentation.getMarkdown(docBuf, trimmedInput.substring(1));
            String docString = docBuf.toString();
            if (docString.length() > 0) {
                int indx = docString.indexOf("### Github");
                if (indx > 0) {
                    docString = docString.substring(0, indx);
                }
                return docString;
            }
            Documentation.usageDocumentation(docBuf, trimmedInput.substring(1));
            docString = docBuf.toString();
            if (docString.length() > 0) {
                int indx = docString.indexOf("### Github");
                if (indx > 0) {
                    docString = docString.substring(0, indx);
                }
                return docString;
            }
            return "No help page found";
        }
        System.out.println(trimmedInput);
        result = evaluator.evaluateWithTimeout(trimmedInput, 30, TimeUnit.SECONDS, true, new EvalControlledCallable(evaluator.getEvalEngine()));
        if (result != null) {
            return printResultShortened(trimmedInput, result);
        }
    } catch (final AbortException re) {
        // try {
        return printResultShortened(trimmedInput, S.$Aborted);
    // } catch (IOException e) {
    // Validate.printException(buf, e);
    // stderr.println(buf.toString());
    // stderr.flush();
    // return "";
    // }
    } catch (final FailedException re) {
        // try {
        return printResultShortened(trimmedInput, S.$Failed);
    // } catch (IOException e) {
    // Validate.printException(buf, e);
    // stderr.println(buf.toString());
    // stderr.flush();
    // return "";
    // }
    } catch (final SyntaxError se) {
        String msg = se.getMessage();
        // stderr.flush();
        return msg;
    } catch (final RuntimeException re) {
        Throwable me = re.getCause();
        if (me instanceof MathException) {
            Validate.printException(buf, me);
        } else {
            Validate.printException(buf, re);
        }
        // stderr.flush();
        return "";
    } catch (final Exception e) {
        Validate.printException(buf, e);
        // stderr.flush();
        return "";
    } catch (final OutOfMemoryError e) {
        Validate.printException(buf, e);
        // stderr.flush();
        return "";
    } catch (final StackOverflowError e) {
        Validate.printException(buf, e);
        // stderr.flush();
        return "";
    }
    return buf.toString();
}
Also used : ExprEvaluator(org.matheclipse.core.eval.ExprEvaluator) 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) EvalControlledCallable(org.matheclipse.core.eval.EvalControlledCallable) IExpr(org.matheclipse.core.interfaces.IExpr) AbortException(org.matheclipse.core.eval.exception.AbortException)

Example 38 with MathException

use of org.matheclipse.parser.client.math.MathException in project symja_android_library by axkr.

the class MathScriptEngine method eval.

@Override
public Object eval(final String script, final ScriptContext context) {
    boolean relaxedSyntax = false;
    final Object enableStackTraceBoolean = get("PRINT_STACKTRACE");
    Level stackLogLevel = Boolean.TRUE.equals(enableStackTraceBoolean) ? Level.ERROR : Level.DEBUG;
    try {
        // first assign the EvalEngine to the current thread:
        EvalEngine.setReset(fEngine);
        // final Bindings bindings = context.getBindings(ScriptContext.ENGINE_SCOPE);
        // ISymbol symbol;
        // for (Map.Entry<String, Object> currEntry : bindings.entrySet()) {
        // symbol = F.userSymbol(currEntry.getKey(), fEngine);
        // symbol.pushLocalVariable(Object2Expr.convert(currEntry.getValue()));
        // list.add(symbol);
        // }
        final Object decimalFormat = get("DECIMAL_FORMAT");
        if (decimalFormat instanceof String) {
            fDecimalFormat = (String) decimalFormat;
        }
        final Object relaxedSyntaxBoolean = get("RELAXED_SYNTAX");
        if (Boolean.TRUE.equals(relaxedSyntaxBoolean)) {
            relaxedSyntax = true;
            fEngine.setRelaxedSyntax(relaxedSyntax);
        }
        boolean disableHistory = true;
        final Object enableHistoryBoolean = get("ENABLE_HISTORY");
        if (Boolean.TRUE.equals(enableHistoryBoolean)) {
            disableHistory = false;
            fEngine.setOutListDisabled(disableHistory, (short) 100);
        }
        // evaluate an expression
        final Object stepwise = get("STEPWISE");
        IExpr result;
        String trimmedScript = script.trim();
        if (trimmedScript.length() == 0) {
            return "";
        }
        if (Boolean.TRUE.equals(stepwise)) {
            result = fUtility.evalTrace(script, null);
        } else {
            result = fUtility.evaluate(script);
        }
        final Object returnType = context.getAttribute("RETURN_OBJECT");
        if ((returnType != null) && returnType.equals(Boolean.TRUE)) {
            // return the object "as is"
            return result;
        } else {
            // return the object as String representation
            if (result != null) {
                return printResult(result, relaxedSyntax);
            }
            return "";
        }
    } catch (final AbortException e) {
        LOGGER.log(stackLogLevel, "Aborted", e);
        return printResult(S.$Aborted, relaxedSyntax);
    } catch (final FailedException e) {
        LOGGER.log(stackLogLevel, "Failed", e);
        return printResult(S.$Failed, relaxedSyntax);
    } catch (final MathException e) {
        // catches parser errors as well
        LOGGER.log(stackLogLevel, "evaluation failed", e);
        return e.getMessage();
    } catch (final ApfloatRuntimeException e) {
        LOGGER.log(stackLogLevel, "ApFloat error", e);
        // catch parser errors here
        return "Apfloat: " + e.getMessage();
    } catch (final Exception e) {
        LOGGER.log(stackLogLevel, "Exception", e);
        return "Exception: " + e.getMessage();
    } catch (final OutOfMemoryError e) {
        LOGGER.log(stackLogLevel, "Out of memory", e);
        return "OutOfMemoryError";
    } catch (final StackOverflowError e) {
        LOGGER.log(stackLogLevel, "Stack overflow", e);
        return "StackOverflowError";
    } finally {
        // if (list.size() > 0) {
        // for (int i = 0; i < list.size(); i++) {
        // list.get(i).popLocalVariable();
        // }
        // }
        EvalEngine.remove();
    }
}
Also used : ApfloatRuntimeException(org.apfloat.ApfloatRuntimeException) FailedException(org.matheclipse.core.eval.exception.FailedException) MathException(org.matheclipse.parser.client.math.MathException) Level(org.apache.logging.log4j.Level) IExpr(org.matheclipse.core.interfaces.IExpr) ApfloatRuntimeException(org.apfloat.ApfloatRuntimeException) MathException(org.matheclipse.parser.client.math.MathException) IOException(java.io.IOException) AbortException(org.matheclipse.core.eval.exception.AbortException) FailedException(org.matheclipse.core.eval.exception.FailedException) ScriptException(javax.script.ScriptException) AbortException(org.matheclipse.core.eval.exception.AbortException)

Example 39 with MathException

use of org.matheclipse.parser.client.math.MathException in project symja_android_library by axkr.

the class EvalDoubleCallbackTestCase method testMissingFunction009.

public void testMissingFunction009() {
    try {
        DoubleEvaluator engine = new DoubleEvaluator();
        double d = engine.evaluate("aTest[1.0]");
        assertEquals(Double.toString(d), "");
    } catch (MathException e) {
        assertEquals("EvalDouble#evaluateFunction(FunctionNode) not possible for: aTest(1.0)", e.getMessage());
    }
}
Also used : DoubleEvaluator(org.matheclipse.parser.client.eval.DoubleEvaluator) MathException(org.matheclipse.parser.client.math.MathException)

Example 40 with MathException

use of org.matheclipse.parser.client.math.MathException in project symja_android_library by axkr.

the class Console method interpreter.

/**
 * Evaluates the given string-expression and returns the result in string form.
 *
 * @param strEval
 * @return the result in string for
 */
public String interpreter(final String strEval) {
    try {
        DoubleEvaluator engine = new DoubleEvaluator(true);
        double d = engine.evaluate(strEval);
        return Double.toString(d);
    } catch (MathException e) {
        System.err.println();
        System.err.println(e.getMessage());
    } catch (RuntimeException e) {
        e.printStackTrace();
    }
    return "";
}
Also used : DoubleEvaluator(org.matheclipse.parser.client.eval.DoubleEvaluator) MathException(org.matheclipse.parser.client.math.MathException)

Aggregations

MathException (org.matheclipse.parser.client.math.MathException)51 IExpr (org.matheclipse.core.interfaces.IExpr)41 SyntaxError (org.matheclipse.parser.client.SyntaxError)37 ExprEvaluator (org.matheclipse.core.eval.ExprEvaluator)24 IAST (org.matheclipse.core.interfaces.IAST)15 StringWriter (java.io.StringWriter)12 SymjaMathException (org.matheclipse.core.eval.exception.SymjaMathException)8 IOException (java.io.IOException)7 AbortException (org.matheclipse.core.eval.exception.AbortException)7 FailedException (org.matheclipse.core.eval.exception.FailedException)7 ASTNode (org.matheclipse.parser.client.ast.ASTNode)7 EvalControlledCallable (org.matheclipse.core.eval.EvalControlledCallable)5 EvalEngine (org.matheclipse.core.eval.EvalEngine)5 DoubleEvaluator (org.matheclipse.parser.client.eval.DoubleEvaluator)5 ISymbol (org.matheclipse.core.interfaces.ISymbol)4 FlowControlException (org.matheclipse.core.eval.exception.FlowControlException)3 OutputFormFactory (org.matheclipse.core.form.output.OutputFormFactory)3 ExprParser (org.matheclipse.core.parser.ExprParser)3 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)2 AST2Expr (org.matheclipse.core.convert.AST2Expr)2