use of org.matheclipse.core.eval.EvalEngine in project symja_android_library by axkr.
the class NIntegrate method integrate.
/**
* Integrate a function numerically.
*
* @param method
* the following methods are possible: LegendreGauss, Simpson,
* Romberg, Trapezoid
* @param list
* a list of the form <code>{x, lowerBound, upperBound}</code>,
* where <code>lowerBound</code> and <code>upperBound</code> are
* numbers which could be converted to a Java double value.
* @param min
* Lower bound of the integration interval.
* @param max
* Upper bound of the integration interval.
* @param function
* the function which should be integrated.
* @param maxPoints
* maximum number of points
* @param maxIterations
* maximum number of iterations
* @return
* @throws MathIllegalStateException
*/
public static double integrate(String method, IAST list, double min, double max, IExpr function, int maxPoints, int maxIterations) throws MathIllegalStateException {
GaussIntegratorFactory factory = new GaussIntegratorFactory();
ISymbol xVar = (ISymbol) list.arg1();
final EvalEngine engine = EvalEngine.get();
IExpr tempFunction = F.eval(function);
UnivariateFunction f = new UnaryNumerical(tempFunction, xVar, engine);
UnivariateIntegrator integrator;
if ("Simpson".equalsIgnoreCase(method)) {
integrator = new SimpsonIntegrator();
} else if ("Romberg".equalsIgnoreCase(method)) {
integrator = new RombergIntegrator();
} else if ("Trapezoid".equalsIgnoreCase(method)) {
integrator = new TrapezoidIntegrator();
} else {
// default: LegendreGauss
GaussIntegrator integ = factory.legendre(maxPoints, min, max);
return integ.integrate(f);
}
return integrator.integrate(maxIterations, f, min, max);
}
use of org.matheclipse.core.eval.EvalEngine in project symja_android_library by axkr.
the class Limit method evalLimitQuiet.
private static IExpr evalLimitQuiet(final IExpr expr, LimitData data) {
EvalEngine engine = EvalEngine.get();
boolean quiet = engine.isQuietMode();
try {
engine.setQuietMode(true);
return evalLimit(expr, data, true);
} finally {
engine.setQuietMode(quiet);
}
}
use of org.matheclipse.core.eval.EvalEngine in project symja_android_library by axkr.
the class Limit method lHospitalesRule.
/**
* Try L'hospitales rule. See <a href="http://en.wikipedia.org/wiki/L%27H%C3%B4pital%27s_rule"> Wikipedia
* L'Hôpital's rule</a>
*
* @param numerator
* @param denominator
* @param data
* the limits data definition
* @return
*/
private static IExpr lHospitalesRule(IExpr numerator, IExpr denominator, LimitData data) {
EvalEngine engine = EvalEngine.get();
ISymbol x = data.getSymbol();
int recursionLimit = engine.getRecursionLimit();
if (recursionLimit > 0) {
IExpr expr = F.evalQuiet(F.Times(F.D(numerator, x), F.Power(F.D(denominator, x), F.CN1)));
return evalLimit(expr, data, false);
}
try {
if (recursionLimit <= 0) {
// set recursion limit for using l'Hospitales rule
engine.setRecursionLimit(128);
}
IExpr expr = F.evalQuiet(F.Times(F.D(numerator, x), F.Power(F.D(denominator, x), F.CN1)));
return evalLimit(expr, data, false);
} catch (RecursionLimitExceeded rle) {
engine.setRecursionLimit(recursionLimit);
} finally {
engine.setRecursionLimit(recursionLimit);
}
return F.NIL;
}
use of org.matheclipse.core.eval.EvalEngine in project symja_android_library by axkr.
the class Integrate method integrateByParts.
/**
* <p>
* Integrate by parts rule:
* <code>Integrate(f'(x) * g(x), x) = f(x) * g(x) - Integrate(f(x) * g'(x),x )</code>
* .
* </p>
*
* See
* <a href="http://en.wikipedia.org/wiki/Integration_by_parts">Wikipedia-
* Integration by parts</a>
*
* @param f
* <code>f(x)</code>
* @param g
* <code>g(x)</code>
* @param x
* @return <code>f(x) * g(x) - Integrate(f(x) * g'(x),x )</code>
*/
private static IExpr integrateByParts(IExpr f, IExpr g, ISymbol x) {
EvalEngine engine = EvalEngine.get();
int limit = engine.getRecursionLimit();
try {
if (limit <= 0) {
// set recursion limit
engine.setRecursionLimit(128);
}
IExpr firstIntegrate = F.eval(F.Integrate(f, x));
if (!firstIntegrate.isFreeAST(Integrate)) {
return F.NIL;
}
IExpr gDerived = F.eval(F.D(g, x));
IExpr second2Integrate = F.eval(F.Integrate(F.Times(gDerived, firstIntegrate), x));
if (!second2Integrate.isFreeAST(Integrate)) {
return F.NIL;
}
return F.eval(F.Subtract(F.Times(g, firstIntegrate), second2Integrate));
} catch (RecursionLimitExceeded rle) {
engine.setRecursionLimit(limit);
} finally {
engine.setRecursionLimit(limit);
}
return F.NIL;
}
use of org.matheclipse.core.eval.EvalEngine in project symja_android_library by axkr.
the class MainTestCase method testSystem400.
public void testSystem400() {
EvalEngine engine = EvalEngine.get();
IExpr exprNumerator = engine.parse("8+12*x+20*x^2+12*x^3+8*x^4+3*x^5");
IExpr exprDenominator = engine.parse("8*x+12*x^3+6*x^5+x^7");
IExpr[] result = Algebra.cancelGCD(exprNumerator, exprDenominator);
assertEquals(result[0].toString(), "1");
assertEquals(result[1].toString(), "4+6*x+8*x^2+3*x^3");
}
Aggregations