Search in sources :

Example 21 with MathException

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

the class CoreCallbackFunction method evaluate.

@Override
public double evaluate(DoubleEvaluator doubleEngine, FunctionNode functionNode, double[] args) {
    ASTNode node = functionNode.getNode(0);
    if (node instanceof SymbolNode) {
        AST2Expr ast2Expr = new AST2Expr();
        IExpr head = ast2Expr.convert(node);
        IAST fun = F.ast(head);
        for (int i = 0; i < args.length; i++) {
            fun.append(F.num(args[i]));
        }
        final IExpr result = F.evaln(fun);
        if (result.isSignedNumber()) {
            return ((ISignedNumber) result).doubleValue();
        }
    } else if (node instanceof FunctionNode) {
        AST2Expr ast2Expr = new AST2Expr();
        IExpr head = ast2Expr.convert(node);
        IAST fun = F.ast(head);
        for (int i = 0; i < args.length; i++) {
            fun.append(F.num(args[i]));
        }
        final IExpr result = F.evaln(fun);
        if (result.isSignedNumber()) {
            return ((ISignedNumber) result).doubleValue();
        }
    }
    throw new MathException("CoreCallbackFunction#evaluate() not possible for: " + functionNode.toString());
}
Also used : SymbolNode(org.matheclipse.parser.client.ast.SymbolNode) ISignedNumber(org.matheclipse.core.interfaces.ISignedNumber) MathException(org.matheclipse.parser.client.math.MathException) ASTNode(org.matheclipse.parser.client.ast.ASTNode) FunctionNode(org.matheclipse.parser.client.ast.FunctionNode) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST) AST2Expr(org.matheclipse.core.convert.AST2Expr)

Example 22 with MathException

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

the class Console method prettyPrinter3Lines.

private String[] prettyPrinter3Lines(final String inputExpression) {
    IExpr result;
    final StringWriter buf = new StringWriter();
    try {
        if (fSeconds <= 0) {
            result = fEvaluator.evaluate(inputExpression);
        } else {
            result = fEvaluator.evaluateWithTimeout(inputExpression, fSeconds, TimeUnit.SECONDS, true);
        }
        if (result != null) {
            if (result.equals(F.Null)) {
                return null;
            }
            ASCIIPrettyPrinter3 strBuffer = new ASCIIPrettyPrinter3();
            strBuffer.convert(result);
            return strBuffer.toStringBuilder();
        }
    } catch (final SyntaxError se) {
        String msg = se.getMessage();
        System.err.println();
        System.err.println(msg);
        return null;
    } catch (final RuntimeException re) {
        Throwable me = re.getCause();
        if (me instanceof MathException) {
            Validate.printException(buf, me);
        } else {
            Validate.printException(buf, re);
        }
        return null;
    } catch (final Exception e) {
        Validate.printException(buf, e);
        return null;
    } catch (final OutOfMemoryError e) {
        Validate.printException(buf, e);
        return null;
    } catch (final StackOverflowError e) {
        Validate.printException(buf, e);
        return null;
    }
    String[] strArray = new String[3];
    strArray[0] = "";
    strArray[1] = buf.toString();
    strArray[3] = "";
    return strArray;
}
Also used : ASCIIPrettyPrinter3(org.matheclipse.core.form.output.ASCIIPrettyPrinter3) 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) IOException(java.io.IOException)

Example 23 with MathException

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

the class MathScriptEngine method eval.

public Object eval(final String script, final ScriptContext context) throws ScriptException {
    final ArrayList<ISymbol> list = new ArrayList<ISymbol>();
    try {
        // first assign the EvalEngine to the current thread:
        fUtility.startRequest();
        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);
        }
        boolean relaxedSyntax = false;
        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, 100);
        }
        // evaluate an expression
        final Object stepwise = get("STEPWISE");
        IExpr result;
        if (Boolean.TRUE.equals(stepwise)) {
            result = fUtility.evalTrace(script, null, F.List());
        } 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) {
                if (result.equals(F.Null)) {
                    return "";
                }
                final StringWriter buf = new StringWriter();
                OutputFormFactory.get(relaxedSyntax).convert(buf, result);
                // print the result in the console
                return buf.toString();
            }
            return "";
        }
    } catch (final MathException e) {
        if (Config.SHOW_STACKTRACE) {
            e.printStackTrace();
        }
        // catch parser errors here
        return e.getMessage();
    } catch (final Exception e) {
        // }
        if (Config.SHOW_STACKTRACE) {
            e.printStackTrace();
        }
        return e.getMessage();
    } catch (final OutOfMemoryError e) {
        if (Config.DEBUG) {
            e.printStackTrace();
        }
        return "OutOfMemoryError";
    } catch (final StackOverflowError e) {
        if (Config.DEBUG) {
            e.printStackTrace();
        }
        return "StackOverflowError";
    } finally {
        if (list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                list.get(i).popLocalVariable();
            }
        }
    }
}
Also used : ISymbol(org.matheclipse.core.interfaces.ISymbol) ArrayList(java.util.ArrayList) Bindings(javax.script.Bindings) MathException(org.matheclipse.parser.client.math.MathException) IOException(java.io.IOException) ScriptException(javax.script.ScriptException) StringWriter(java.io.StringWriter) MathException(org.matheclipse.parser.client.math.MathException) IExpr(org.matheclipse.core.interfaces.IExpr) Map(java.util.Map)

Aggregations

MathException (org.matheclipse.parser.client.math.MathException)23 IExpr (org.matheclipse.core.interfaces.IExpr)16 SyntaxError (org.matheclipse.parser.client.SyntaxError)11 ExprEvaluator (org.matheclipse.core.eval.ExprEvaluator)6 IAST (org.matheclipse.core.interfaces.IAST)6 ASTNode (org.matheclipse.parser.client.ast.ASTNode)6 IOException (java.io.IOException)4 StringWriter (java.io.StringWriter)4 ISymbol (org.matheclipse.core.interfaces.ISymbol)2 ExprParser (org.matheclipse.core.parser.ExprParser)2 DoubleEvaluator (org.matheclipse.parser.client.eval.DoubleEvaluator)2 SimpleTimeLimiter (com.google.common.util.concurrent.SimpleTimeLimiter)1 TimeLimiter (com.google.common.util.concurrent.TimeLimiter)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 Bindings (javax.script.Bindings)1 ScriptException (javax.script.ScriptException)1 AST2Expr (org.matheclipse.core.convert.AST2Expr)1 ASCIIPrettyPrinter3 (org.matheclipse.core.form.output.ASCIIPrettyPrinter3)1 ISignedNumber (org.matheclipse.core.interfaces.ISignedNumber)1