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.evaluateDoube(aS);
} catch (MathException e) {
// e.getMessage(), e.context);
throw e;
}
try {
// b = parser.parse(bS).getVal();
b = parser.evaluateDoube(bS);
} catch (MathException e) {
// e.getMessage(), e.context);
throw e;
}
IExpr function = parse(fun, null);
IExpr var = parse(v, null);
IAST list = F.List();
list.append(var);
list.append(F.num(a));
list.append(F.num(b));
return NIntegrate.integrate("LegendreGauss", list, a, b, function, NIntegrate.DEFAULT_MAX_POINTS, NIntegrate.DEFAULT_MAX_ITERATIONS);
}
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 PolynomialExample method main.
public static void main(String[] args) {
try {
ExprEvaluator util = new ExprEvaluator();
IExpr expr = util.evaluate("x^2+y+a*x+b*y+c");
System.out.println(expr.toString());
final IAST variables = F.List(F.x, F.y);
ExprPolynomialRing ring = new ExprPolynomialRing(ExprRingFactory.CONST, variables, variables.size() - 1, ExprTermOrderByName.Lexicographic, false);
ExprPolynomial poly = ring.create(expr);
System.out.println(poly.toString());
// x degree
System.out.println(poly.degree(0));
// y degree
System.out.println(poly.degree(1));
// show internal structure:
System.out.println(poly.coefficientRules());
System.out.println();
for (ExprMonomial monomial : poly) {
System.out.println(monomial.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 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());
}
}
Aggregations