use of org.matheclipse.parser.client.math.MathException in project symja_android_library by axkr.
the class SolveIssue176 method main.
public static void main(String[] args) {
try {
ExprEvaluator util = new ExprEvaluator();
util.eval("x0=20.796855124168776");
IExpr result = util.eval("Solve(x0==(-1.0000000000000002)*Sqrt(y^2.0),y)");
// {}
System.out.println(result.toString());
result = util.eval("Solve(x1==(-1.0000000000000002)*Sqrt(y^2.0),y)");
// {{y->-x1},{y->x1}}
System.out.println(result.toString());
IExpr exp1 = util.eval("x2=985.3698808807793");
IExpr exp2 = util.eval("y=89.73311105248706");
IExpr exp3 = util.eval("z=30.358930164378133");
IExpr exp4 = util.eval("w=143.26674070021394");
IExpr exp5 = util.eval("q=923.282853878973");
IExpr exp6 = util.eval("j=955.7677262340256");
result = util.eval(" N(Solve(q/w==(m+2.0*y)/(m+z+x2+j),m))");
// {{m->-2300.6414589715228}}
System.out.println(result.toString());
result = util.eval("Solve(923.282853878973/143.26674070021394==(m+2.0*89.73311105248706)/(m+30.358930164378133+985.3698808807793+955.7677262340256),m)");
// {{m->-2300.6414589715228}}
System.out.println(result.toString());
result = util.eval("Solve(4.027433300199394==(110.70534+x3)/(1015.3739400000001+x3),x3)");
// {{x3->-1314.197567242396}}
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 (Exception e) {
e.printStackTrace();
} catch (final StackOverflowError soe) {
System.out.println(soe.getMessage());
} catch (final OutOfMemoryError oome) {
System.out.println(oome.getMessage());
}
}
use of org.matheclipse.parser.client.math.MathException in project symja_android_library by axkr.
the class MathUtils method getFunctionVal.
public static String getFunctionVal(String fun, String[] var, String resp, String[] vals) throws MathException {
// return evaluate(command.toString(), null);
try {
EvalDouble parParser = new EvalDouble(true);
double[] values = new double[vals.length];
for (int i = 0; i < vals.length; i++) {
values[i] = parParser.evaluate(vals[i]);
}
String respVar = null;
for (int i = 0; i < var.length; i++) {
if (var[i].equals(resp)) {
respVar = resp;
// parParser.add(respVar);
// respVar.setVal(values[i]);
parParser.defineVariable(respVar, values[i]);
} else {
String temp = var[i];
parParser.defineVariable(temp, values[i]);
}
}
if (respVar != null) {
try {
ASTNode f = parParser.parse(fun);
return parParser.evaluateNode(f) + "";
} catch (MathException e) {
// e.getMessage(), e.context);
throw e;
}
}
} catch (MathException e) {
// ParserContext(resp, 0, null));
throw e;
}
throw new SymjaMathException("MathUtils:getFunctionVal - cannot compute function values");
}
use of org.matheclipse.parser.client.math.MathException in project symja_android_library by axkr.
the class MathUtils method getFunctionVal.
public static double getFunctionVal(String f, String v, String x) {
// StringBuilder command = new StringBuilder();
// command.append("ReplaceAll(");
// command.append(f);
// command.append(",");
// command.append(v);
// command.append("-> (");
// command.append(x);
// command.append(")");
// command.append(")");
// String result = evaluate(command.toString(), "N");
//
// EvalDouble dEval = new EvalDouble(true);
// return dEval.evaluate(result);
// Variable var = new Variable(v);
// Expression fun,val;
// Parser parser = new Parser(Parser.STANDARD_FUNCTIONS |
// Parser.OPTIONAL_PARENS
// | Parser.OPTIONAL_STARS | Parser.OPTIONAL_SPACES
// | Parser.BRACES | Parser.BRACKETS| Parser.BOOLEANS);
// parser.add(var);
// setUpParser(parser);
EvalDouble parser = new EvalDouble(true);
String var = v;
ASTNode fun, val;
parser.defineVariable(var);
try {
fun = parser.parse(f);
} catch (MathException e) {
// + e.getMessage(), e.context);
throw e;
}
try {
val = parser.parse(x);
} catch (MathException e) {
// e.getMessage(), e.context);
throw e;
}
// var.setVal(val.getVal());
parser.defineVariable(var, parser.evaluateNode(val));
return parser.evaluateNode(fun);
}
use of org.matheclipse.parser.client.math.MathException in project symja_android_library by axkr.
the class SymjaInterpreter method interpreter.
/**
* Parse the <code>codeString</code> into an <code>IExpr</code> and if <code>function</code>
* unequals <code>null</code>, replace all occurences of slot <code>#</code> in the function with
* the parsed expression. After that evaluate the given expression.
*
* @param function
* @return
*/
public String interpreter(IAST function) {
String evalStr = codeString;
IExpr expr;
EvalEngine engine = EvalEngine.get();
EvalEngine.setReset(engine);
try {
ExprParser p = new ExprParser(engine, true);
// throws SyntaxError exception, if syntax isn't valid
expr = p.parse(evalStr);
} catch (SyntaxError e1) {
try {
ExprParser p = new ExprParser(engine);
// throws SyntaxError exception, if syntax isn't valid
expr = p.parse(evalStr);
} catch (Exception e2) {
outStream.println(e2.getMessage());
return "";
}
}
IExpr result;
final StringBuilder buf = new StringBuilder();
try {
if (function != null) {
expr = function.replaceAll(F.Rule(F.Slot1, expr));
}
if (expr.isPresent()) {
result = evaluate(expr);
if (result.isPresent()) {
if (result.equals(S.Null)) {
return buf.toString();
}
if (OutputFormFactory.get(engine.isRelaxedSyntax()).convert(buf, result)) {
return buf.toString();
}
}
return "ERROR-IN-OUTPUTFORM";
}
} 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);
}
return buf.toString();
}
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 trimmedInput a trimmed input string
* @return
*/
/* package private */
String interpreter(final String trimmedInput) {
IExpr result;
final StringWriter buf = new StringWriter();
try {
if (trimmedInput.length() > 1 && trimmedInput.charAt(0) == '?') {
IExpr doc = Documentation.findDocumentation(trimmedInput);
return printResult(doc);
}
if (fSeconds <= 0) {
result = fEvaluator.eval(trimmedInput);
} else {
result = fEvaluator.evaluateWithTimeout(trimmedInput, fSeconds, TimeUnit.SECONDS, true, new EvalControlledCallable(fEvaluator.getEvalEngine()));
}
if (result != null) {
return printResult(result);
}
} catch (final AbortException re) {
return printResult(S.$Aborted);
} catch (final FailedException re) {
return printResult(S.$Failed);
} catch (final SyntaxError se) {
String msg = se.getMessage();
stderr.println(msg);
// stderr.println();
stderr.flush();
return "";
} catch (final RuntimeException re) {
Throwable me = re.getCause();
if (me instanceof MathException) {
Validate.printException(buf, me);
} else {
Validate.printException(buf, re);
}
stderr.println(buf.toString());
stderr.flush();
return "";
} catch (final Exception | OutOfMemoryError | StackOverflowError e) {
Validate.printException(buf, e);
stderr.println(buf.toString());
stderr.flush();
return "";
}
return buf.toString();
}
Aggregations