Search in sources :

Example 26 with EvalEngine

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);
}
Also used : SimpsonIntegrator(org.hipparchus.analysis.integration.SimpsonIntegrator) UnaryNumerical(org.matheclipse.core.generic.UnaryNumerical) ISymbol(org.matheclipse.core.interfaces.ISymbol) UnivariateFunction(org.hipparchus.analysis.UnivariateFunction) UnivariateIntegrator(org.hipparchus.analysis.integration.UnivariateIntegrator) RombergIntegrator(org.hipparchus.analysis.integration.RombergIntegrator) EvalEngine(org.matheclipse.core.eval.EvalEngine) IExpr(org.matheclipse.core.interfaces.IExpr) GaussIntegratorFactory(org.hipparchus.analysis.integration.gauss.GaussIntegratorFactory) TrapezoidIntegrator(org.hipparchus.analysis.integration.TrapezoidIntegrator) GaussIntegrator(org.hipparchus.analysis.integration.gauss.GaussIntegrator)

Example 27 with EvalEngine

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);
    }
}
Also used : EvalEngine(org.matheclipse.core.eval.EvalEngine)

Example 28 with EvalEngine

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;
}
Also used : RecursionLimitExceeded(org.matheclipse.core.eval.exception.RecursionLimitExceeded) ISymbol(org.matheclipse.core.interfaces.ISymbol) EvalEngine(org.matheclipse.core.eval.EvalEngine) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 29 with EvalEngine

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;
}
Also used : RecursionLimitExceeded(org.matheclipse.core.eval.exception.RecursionLimitExceeded) EvalEngine(org.matheclipse.core.eval.EvalEngine) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 30 with EvalEngine

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");
}
Also used : EvalEngine(org.matheclipse.core.eval.EvalEngine) IExpr(org.matheclipse.core.interfaces.IExpr)

Aggregations

EvalEngine (org.matheclipse.core.eval.EvalEngine)32 IExpr (org.matheclipse.core.interfaces.IExpr)15 IAST (org.matheclipse.core.interfaces.IAST)5 ISymbol (org.matheclipse.core.interfaces.ISymbol)5 IRational (org.matheclipse.core.interfaces.IRational)3 IOException (java.io.IOException)2 RecursionLimitExceeded (org.matheclipse.core.eval.exception.RecursionLimitExceeded)2 Num (org.matheclipse.core.expression.Num)2 IInteger (org.matheclipse.core.interfaces.IInteger)2 INum (org.matheclipse.core.interfaces.INum)2 Parser (org.matheclipse.parser.client.Parser)2 File (java.io.File)1 InputStream (java.io.InputStream)1 ObjectInputStream (java.io.ObjectInputStream)1 PrintWriter (java.io.PrintWriter)1 Apint (org.apfloat.Apint)1 UnivariateFunction (org.hipparchus.analysis.UnivariateFunction)1 RombergIntegrator (org.hipparchus.analysis.integration.RombergIntegrator)1 SimpsonIntegrator (org.hipparchus.analysis.integration.SimpsonIntegrator)1 TrapezoidIntegrator (org.hipparchus.analysis.integration.TrapezoidIntegrator)1