use of org.matheclipse.parser.client.SyntaxError in project symja_android_library by axkr.
the class SolveExample method main.
public static void main(String[] args) {
try {
ExprEvaluator util = new ExprEvaluator();
IExpr result = util.evaluate("Solve(2*x==5 + 4*x,x)");
// print: {{x->-5/2}}
System.out.println(result.toString());
result = util.evaluate("Roots(2*x==5+4*x, x)");
// print: x==-5/2
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.SyntaxError in project symja_android_library by axkr.
the class SolveSO39753012 method main.
public static void main(String[] args) {
try {
ExprEvaluator util = new ExprEvaluator();
IExpr eq = F.Equal(F.Plus(F.a, F.b), F.c);
IExpr eq1 = eq.replaceAll(F.Rule(F.a, F.integer(1)));
eq1 = eq1.replaceAll(F.Rule(F.b, F.integer(2)));
// Solve(1+2==c, c)
IExpr result = util.evaluate(F.Solve(eq1, F.c));
// print: {{c->3}}
System.out.println(result.toString());
IExpr eq2 = eq.replaceAll(F.Rule(F.a, F.integer(1)));
eq2 = eq2.replaceAll(F.Rule(F.c, F.integer(3)));
// Solve(1+b==3, b)
result = util.evaluate(F.Solve(eq2, F.b));
// print: {{b->2}}
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.SyntaxError in project symja_android_library by axkr.
the class SolveSO43024172 method main.
public static void main(String[] args) {
try {
ExprEvaluator util = new ExprEvaluator();
IExpr expr = util.evaluate("(x1)^2+4*(x2)^2-2*x1-4*x2");
System.out.println(expr.toString());
// determine the variables used in the expression
IAST variableList = VariablesSet.getVariables(expr);
System.out.println(variableList.toString());
IExpr a = util.evaluate("a");
IExpr x1 = util.evaluate("x1");
IExpr x2 = util.evaluate("x2");
IExpr x1Substitute = util.evaluate("5+a");
IExpr x2Substitute = util.evaluate("2");
IAST astRules = F.List(F.Rule(x1, x1Substitute), F.Rule(x2, x2Substitute));
// {x1->5+a,x2->2}
System.out.println(astRules.toString());
IExpr replacedExpr = expr.replaceAll(astRules);
// -2*(5+a)+(5+a)^2+(-4)*2+4*2^2
System.out.println(replacedExpr.toString());
// 8+(5+a)^2-2*(5+a)
replacedExpr = util.evaluate(replacedExpr);
System.out.println(replacedExpr.toString());
// replacedExpr = util.evaluate(F.ExpandAll(replacedExpr));
// 23+8*a+a^2
// System.out.println(replacedExpr.toString());
IExpr derivedExpr = util.evaluate(F.D(replacedExpr, a));
// -2+2*(5+a)
System.out.println(derivedExpr.toString());
IExpr equation = F.Equal(derivedExpr, F.C0);
// -2+2*(5+a)==0
System.out.println(equation.toString());
IExpr solvedEquation = util.evaluate(F.Solve(equation, a));
// {{a->-4}}
System.out.println(solvedEquation.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.SyntaxError in project symja_android_library by axkr.
the class EvalUtilities method eval.
/**
* Evaluate the <code>inputExpression</code> and return the resulting
* expression. <br/>
* The parser first tries (independently from the settings in the
* <code>evalEngine</code>) to parse the expression in the "relaxed
* mode" (i.e. "common math expression syntax" with
* parentheses for function arguments) and if that results in a
* <code>SyntaxError</code> exception it tries to parse in the
* "stronger mode" (i.e. with square brackets for function
* arguments). <br />
* <b>Note</B> that after the second parser step this method may also throw
* a <code>SyntaxError</code> exception.
*
* @param inputExpression
* the expression which should be evaluated.
* @param evalEngine
* the evaluation engine which should be used
* @return <code>F.NIL</code>, if the inputExpression is <code>null</code>
* @throw org.matheclipse.parser.client.SyntaxError
* @throw org.matheclipse.parser.client.math.MathException
*/
public static IExpr eval(final String inputExpression, final EvalEngine evalEngine) throws MathException {
if (inputExpression != null) {
EvalEngine.set(evalEngine);
boolean SIMPLE_SYNTAX = true;
IExpr parsedExpression;
try {
ExprParser parser = new ExprParser(evalEngine, SIMPLE_SYNTAX);
parsedExpression = parser.parse(inputExpression);
} catch (SyntaxError se1) {
try {
SIMPLE_SYNTAX = false;
ExprParser parser = new ExprParser(evalEngine, SIMPLE_SYNTAX);
parsedExpression = parser.parse(inputExpression);
} catch (SyntaxError se2) {
throw se1;
}
}
if (parsedExpression != null) {
F.join();
evalEngine.reset();
IExpr temp = evalEngine.evaluate(parsedExpression);
evalEngine.addOut(temp);
return temp;
}
}
return F.NIL;
}
use of org.matheclipse.parser.client.SyntaxError in project symja_android_library by axkr.
the class MMAConsole method main.
public static void main(final String[] args) {
// distinguish between lower- and uppercase identifiers
Config.PARSER_USE_LOWERCASE_SYMBOLS = false;
// console.getDefaultSystemRulesFilename(),
F.initSymbols(null, null, true);
// null, false);
printUsage();
MMAConsole console;
try {
console = new MMAConsole();
} catch (final SyntaxError e1) {
e1.printStackTrace();
return;
}
String inputExpression = null;
String trimmedInput = null;
String outputExpression = null;
console.setArgs(args);
final File file = console.getFile();
if (file != null) {
try {
final BufferedReader f = new BufferedReader(new FileReader(file));
final StringBuffer buff = new StringBuffer(1024);
String line;
while ((line = f.readLine()) != null) {
buff.append(line);
buff.append('\n');
}
f.close();
inputExpression = buff.toString();
outputExpression = console.interpreter(inputExpression);
System.out.println("In [" + COUNTER + "]: " + inputExpression);
if (outputExpression.length() > 0) {
System.out.println("Out[" + COUNTER + "]: " + outputExpression);
}
COUNTER++;
} catch (final IOException ioe) {
final String msg = "Cannot read from the specified file. " + "Make sure the path exists and you have read permission.";
System.out.println(msg);
return;
}
}
while (true) {
try {
inputExpression = console.readString(System.out, ">>> ");
if (inputExpression != null) {
trimmedInput = inputExpression.trim();
if ((inputExpression.length() >= 4) && inputExpression.toLowerCase(Locale.ENGLISH).substring(0, 4).equals("exit")) {
System.out.println("Closing Symja console... bye.");
System.exit(0);
} else if ((inputExpression.length() >= 10) && inputExpression.toLowerCase(Locale.ENGLISH).substring(0, 10).equals("timeoutoff")) {
System.out.println("Disabling timeout for evaluation");
console.fSeconds = -1;
continue;
} else if ((inputExpression.length() >= 9) && inputExpression.toLowerCase(Locale.ENGLISH).substring(0, 9).equals("timeouton")) {
System.out.println("Enabling timeout for evaluation to 60 seconds.");
console.fSeconds = 60;
continue;
} else if (trimmedInput.length() > 1 && trimmedInput.charAt(0) == '?') {
IAST list = Names.getNamesByPrefix(trimmedInput.substring(1));
for (int i = 1; i < list.size(); i++) {
System.out.print(list.get(i).toString());
if (i != list.size() - 1) {
System.out.print(", ");
}
}
System.out.println();
if (list.size() == 2) {
printDocumentation(list.get(1).toString());
}
continue;
}
outputExpression = console.interpreter(inputExpression);
System.out.println("In [" + COUNTER + "]: " + inputExpression);
if (outputExpression.length() > 0) {
System.out.println("Out[" + COUNTER + "]: " + outputExpression);
}
COUNTER++;
}
// } catch (final MathRuntimeException mre) {
// Throwable me = mre.getCause();
// System.out.println(me.getMessage());
} catch (final Exception e) {
System.out.println(e.getMessage());
}
}
}
Aggregations