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