Search in sources :

Example 1 with SimpsonIntegrator

use of org.hipparchus.analysis.integration.SimpsonIntegrator in project symja_android_library by axkr.

the class NIntegrate method integrate.

// public final static ISymbol LegendreGauss = F
// .initFinalSymbol(Config.PARSER_USE_LOWERCASE_SYMBOLS ? "legendregauss" : "LegendreGauss");
/**
 * 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();
    if (!list.arg1().isSymbol()) {
        // `1` is not a valid variable.
        String str = IOFunctions.getMessage("ivar", F.list(list.arg1()), EvalEngine.get());
        throw new ArgumentTypeException(str);
    }
    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 {
        if (maxPoints > 1000) {
            // see also https://github.com/Hipparchus-Math/hipparchus/issues/61
            throw new MathIllegalArgumentException(LocalizedCoreFormats.NUMBER_TOO_LARGE, maxPoints, 1000);
        }
        // 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) GaussIntegratorFactory(org.hipparchus.analysis.integration.gauss.GaussIntegratorFactory) GaussIntegrator(org.hipparchus.analysis.integration.gauss.GaussIntegrator) MathIllegalArgumentException(org.hipparchus.exception.MathIllegalArgumentException) EvalEngine(org.matheclipse.core.eval.EvalEngine) IExpr(org.matheclipse.core.interfaces.IExpr) ArgumentTypeException(org.matheclipse.core.eval.exception.ArgumentTypeException) TrapezoidIntegrator(org.hipparchus.analysis.integration.TrapezoidIntegrator)

Aggregations

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 UnivariateIntegrator (org.hipparchus.analysis.integration.UnivariateIntegrator)1 GaussIntegrator (org.hipparchus.analysis.integration.gauss.GaussIntegrator)1 GaussIntegratorFactory (org.hipparchus.analysis.integration.gauss.GaussIntegratorFactory)1 MathIllegalArgumentException (org.hipparchus.exception.MathIllegalArgumentException)1 EvalEngine (org.matheclipse.core.eval.EvalEngine)1 ArgumentTypeException (org.matheclipse.core.eval.exception.ArgumentTypeException)1 UnaryNumerical (org.matheclipse.core.generic.UnaryNumerical)1 IExpr (org.matheclipse.core.interfaces.IExpr)1 ISymbol (org.matheclipse.core.interfaces.ISymbol)1