use of org.matheclipse.parser.client.math.MathException 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.math.MathException 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.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 Programming method rememberVariables.
/**
* Remember which local variable names (appended with the module counter) we use in the given
* <code>variablesMap</code>.
*
* @param variablesList
* initializer variables list from the <code>Module</code> function
* @param varAppend
* the module counter string which aer appended to the variable names.
* @param variablesMap
* the resulting module variables map
* @param engine
* the evaluation engine
*/
private static void rememberVariables(IAST variablesList, final String varAppend, final java.util.Map<ISymbol, ISymbol> variablesMap, final EvalEngine engine) {
ISymbol oldSymbol;
ISymbol newSymbol;
for (int i = 1; i < variablesList.size(); i++) {
if (variablesList.get(i).isSymbol()) {
oldSymbol = (ISymbol) variablesList.get(i);
newSymbol = F.userSymbol(oldSymbol.toString() + varAppend, engine);
variablesMap.put(oldSymbol, newSymbol);
newSymbol.pushLocalVariable();
} else {
if (variablesList.get(i).isAST(F.Set, 3)) {
final IAST setFun = (IAST) variablesList.get(i);
if (setFun.arg1().isSymbol()) {
oldSymbol = (ISymbol) setFun.arg1();
newSymbol = F.userSymbol(oldSymbol.toString() + varAppend, engine);
variablesMap.put(oldSymbol, newSymbol);
IExpr rightHandSide = setFun.arg2();
try {
rightHandSide = engine.evaluate(rightHandSide);
} catch (MathException me) {
if (Config.DEBUG) {
me.printStackTrace();
}
}
newSymbol.pushLocalVariable(rightHandSide);
}
}
}
}
}
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();
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(F.Null)) {
return buf.toString();
}
OutputFormFactory.get(true).convert(buf, result);
}
return buf.toString();
}
} 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();
}
Aggregations