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();
}
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();
}
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;
}
}
}
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());
}
}
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);
}
Aggregations