Search in sources :

Example 1 with MathException

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

the class MMAConsole method interpreter.

/**
	 * Evaluates the given string-expression and returns the result in
	 * <code>OutputForm</code>
	 * 
	 * @param inputExpression
	 * @return
	 */
public String interpreter(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 "";
            }
            return result.toString();
        }
    } catch (final SyntaxError se) {
        String msg = se.getMessage();
        System.err.println(msg);
    } catch (final RuntimeException re) {
        Throwable me = re.getCause();
        if (me instanceof MathException) {
            Validate.printException(buf, me);
        } else {
            Validate.printException(buf, re);
        }
    } catch (final Exception e) {
        Validate.printException(buf, e);
    } catch (final OutOfMemoryError e) {
        Validate.printException(buf, e);
    } catch (final StackOverflowError e) {
        Validate.printException(buf, e);
    }
    return buf.toString();
}
Also used : 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 2 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 <code>OutputForm</code>
	 * 
	 * @param inputExpression
	 * @return
	 */
public String interpreter(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 "";
            }
            StringBuilder strBuffer = new StringBuilder();
            fOutputFactory.convert(strBuffer, result);
            return strBuffer.toString();
        }
    } catch (final SyntaxError se) {
        String msg = se.getMessage();
        System.err.println();
        System.err.println(msg);
        return "";
    } catch (final RuntimeException re) {
        Throwable me = re.getCause();
        if (me instanceof MathException) {
            Validate.printException(buf, me);
        } else {
            Validate.printException(buf, re);
        }
        return "";
    } catch (final Exception e) {
        Validate.printException(buf, e);
        return "";
    } catch (final OutOfMemoryError e) {
        Validate.printException(buf, e);
        return "";
    } catch (final StackOverflowError e) {
        Validate.printException(buf, e);
        return "";
    }
    return buf.toString();
}
Also used : 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 3 with MathException

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

the class TimeConstrained method evaluate.

@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
    Validate.checkRange(ast, 3, 4);
    IExpr arg2 = engine.evaluate(ast.arg2());
    long seconds = 0L;
    try {
        if (arg2.isSignedNumber()) {
            seconds = ((ISignedNumber) arg2).toLong();
        } else {
            engine.printMessage("TimeConstrained: " + ast.arg2().toString() + " is not a Java long value.");
            return F.NIL;
        }
    } catch (ArithmeticException ae) {
        engine.printMessage("TimeConstrained: " + ast.arg2().toString() + " is not a Java long value.");
        return F.NIL;
    }
    if (Config.JAS_NO_THREADS) {
        // no thread can be spawned
        try {
            return engine.evaluate(ast.arg1());
        } catch (final MathException e) {
            throw e;
        } catch (final Throwable th) {
            if (ast.isAST3()) {
                return ast.arg3();
            }
        }
        return F.Aborted;
    } else {
        TimeLimiter timeLimiter = new SimpleTimeLimiter();
        Callable<IExpr> work = new EvalCallable(ast.arg1(), engine);
        try {
            return timeLimiter.callWithTimeout(work, seconds, TimeUnit.SECONDS, true);
        } catch (java.util.concurrent.TimeoutException e) {
            if (ast.isAST3()) {
                return ast.arg3();
            }
            return F.Aborted;
        } catch (com.google.common.util.concurrent.UncheckedTimeoutException e) {
            if (ast.isAST3()) {
                return ast.arg3();
            }
            return F.Aborted;
        } catch (Exception e) {
            if (Config.DEBUG) {
                e.printStackTrace();
            }
            return F.Null;
        }
    }
}
Also used : SimpleTimeLimiter(com.google.common.util.concurrent.SimpleTimeLimiter) TimeLimiter(com.google.common.util.concurrent.TimeLimiter) MathException(org.matheclipse.parser.client.math.MathException) MathException(org.matheclipse.parser.client.math.MathException) IExpr(org.matheclipse.core.interfaces.IExpr) SimpleTimeLimiter(com.google.common.util.concurrent.SimpleTimeLimiter)

Example 4 with MathException

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

the class EvalDoubleTestCase method testInterpolatingFunction.

public void testInterpolatingFunction() {
    try {
        DoubleEvaluator engine = new DoubleEvaluator();
        engine.setCallbackFunction(CoreCallbackFunction.CONST);
        engine.defineVariable("x", new DoubleVariable(3.0));
        double d = engine.evaluate("InterpolatingFunction[{{0, 0}, {1, 1}, {2, 3}, {3, 4}, {4, 3}, {5, 0}}][x]");
        assertEquals(Double.toString(d), "4.0");
    } catch (MathException e) {
        e.printStackTrace();
        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) DoubleVariable(org.matheclipse.parser.client.eval.DoubleVariable)

Example 5 with MathException

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

the class MathUtils method integrate.

/**
 * Integrate a function numerically.
 *
 * @param method the following methods are possible: LegendreGauss, Simpson, Romberg, Trapezoid
 * @param fun the function which should be integrated
 * @param v the variable
 * @param aS lower bound double value string for integration
 * @param bS upper bound double value string for integration
 * @return
 * @throws MathException
 */
public static double integrate(String method, String fun, String v, String aS, String bS) throws MathException {
    ExprEvaluator parser = new ExprEvaluator();
    double a, b;
    try {
        a = parser.evalf(aS);
    } catch (MathException e) {
        // e.getMessage(), e.context);
        throw e;
    }
    try {
        // b = parser.parse(bS).getVal();
        b = parser.evalf(bS);
    } catch (MathException e) {
        // e.getMessage(), e.context);
        throw e;
    }
    IExpr function = parse(fun, null);
    IExpr var = parse(v, null);
    IAST list = F.list(var, F.num(a), F.num(b));
    return NIntegrate.integrate("LegendreGauss", list, a, b, function, NIntegrate.DEFAULT_MAX_POINTS, NIntegrate.DEFAULT_MAX_ITERATIONS);
}
Also used : SymjaMathException(org.matheclipse.core.eval.exception.SymjaMathException) MathException(org.matheclipse.parser.client.math.MathException) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST)

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