Search in sources :

Example 26 with MathException

use of org.matheclipse.parser.client.math.MathException 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 27 with MathException

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

the class Programming method rememberVariables.

/**
	 * Remember which local variable names (appended with the module counter) we use in the given
	 * <code>variablesMap</code>.
	 * 
	 * @param variablesList
	 *            initializer variables list from the <code>Module</code> function
	 * @param varAppend
	 *            the module counter string which aer appended to the variable names.
	 * @param variablesMap
	 *            the resulting module variables map
	 * @param engine
	 *            the evaluation engine
	 */
private static void rememberVariables(IAST variablesList, final String varAppend, final java.util.Map<ISymbol, ISymbol> variablesMap, final EvalEngine engine) {
    ISymbol oldSymbol;
    ISymbol newSymbol;
    for (int i = 1; i < variablesList.size(); i++) {
        if (variablesList.get(i).isSymbol()) {
            oldSymbol = (ISymbol) variablesList.get(i);
            newSymbol = F.userSymbol(oldSymbol.toString() + varAppend, engine);
            variablesMap.put(oldSymbol, newSymbol);
            newSymbol.pushLocalVariable();
        } else {
            if (variablesList.get(i).isAST(F.Set, 3)) {
                final IAST setFun = (IAST) variablesList.get(i);
                if (setFun.arg1().isSymbol()) {
                    oldSymbol = (ISymbol) setFun.arg1();
                    newSymbol = F.userSymbol(oldSymbol.toString() + varAppend, engine);
                    variablesMap.put(oldSymbol, newSymbol);
                    IExpr rightHandSide = setFun.arg2();
                    try {
                        rightHandSide = engine.evaluate(rightHandSide);
                    } catch (MathException me) {
                        if (Config.DEBUG) {
                            me.printStackTrace();
                        }
                    }
                    newSymbol.pushLocalVariable(rightHandSide);
                }
            }
        }
    }
}
Also used : ISymbol(org.matheclipse.core.interfaces.ISymbol) MathException(org.matheclipse.parser.client.math.MathException) IAST(org.matheclipse.core.interfaces.IAST) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 28 with MathException

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

the class CalculusExample method main.

public static void main(String[] args) {
    try {
        // don't distinguish between lower- and uppercase identifiers
        Config.PARSER_USE_LOWERCASE_SYMBOLS = true;
        ExprEvaluator util = new ExprEvaluator(false, 100);
        // Show an expression in the Java form:
        // Note: single character identifiers are case sensistive
        // (the "D()" function input must be written as upper case
        // character)
        String javaForm = util.toJavaForm("D(sin(x)*cos(x),x)");
        // prints: D(Times(Sin(x),Cos(x)),x)
        System.out.println(javaForm.toString());
        // Use the Java form to create an expression with F.* static
        // methods:
        IAST function = D(Times(Sin(x), Cos(x)), x);
        IExpr result = util.evaluate(function);
        // print: Cos(x)^2-Sin(x)^2
        System.out.println(result.toString());
        // Note "diff" is an alias for the "D" function
        result = util.evaluate("diff(sin(x)*cos(x),x)");
        // print: Cos(x)^2-Sin(x)^2
        System.out.println(result.toString());
        // evaluate the last result ($ans contains "last answer")
        result = util.evaluate("$ans+cos(x)^2");
        // print: 2*Cos(x)^2-Sin(x)^2
        System.out.println(result.toString());
        // evaluate an Integrate[] expression
        result = util.evaluate("integrate(sin(x)^5,x)");
        // print: 2/3*Cos(x)^3-1/5*Cos(x)^5-Cos(x)
        System.out.println(result.toString());
        // set the value of a variable "a" to 10
        // Note: in server mode the variable name must have a preceding '$'
        // character
        result = util.evaluate("a=10");
        // print: 10
        System.out.println(result.toString());
        // do a calculation with variable "a"
        result = util.evaluate("a*3+b");
        // print: 30+b
        System.out.println(result.toString());
        // Do a calculation in "numeric mode" with the N() function
        // Note: single character identifiers are case sensistive
        // (the "N()" function input must be written as upper case
        // character)
        result = util.evaluate("N(sinh(5))");
        // print: 74.20321057778875
        System.out.println(result.toString());
        // define a function with a recursive factorial function definition.
        // Note: fac(0) is the stop condition.
        result = util.evaluate("fac(x_IntegerQ):=x*fac(x-1);fac(0)=1");
        // now calculate factorial of 10:
        result = util.evaluate("fac(10)");
        // print: 3628800
        System.out.println(result.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 (final Exception ex) {
        System.out.println(ex.getMessage());
    } 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) SyntaxError(org.matheclipse.parser.client.SyntaxError) MathException(org.matheclipse.parser.client.math.MathException) IAST(org.matheclipse.core.interfaces.IAST) IExpr(org.matheclipse.core.interfaces.IExpr) MathException(org.matheclipse.parser.client.math.MathException)

Example 29 with MathException

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

the class EvalDoubleTestCase 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 30 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)

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