Example usage for org.apache.commons.math3.analysis.integration BaseAbstractUnivariateIntegrator integrate

List of usage examples for org.apache.commons.math3.analysis.integration BaseAbstractUnivariateIntegrator integrate

Introduction

In this page you can find the example usage for org.apache.commons.math3.analysis.integration BaseAbstractUnivariateIntegrator integrate.

Prototype

public double integrate(final int maxEval, final UnivariateFunction f, final double lower, final double upper)
        throws TooManyEvaluationsException, MaxCountExceededException, MathIllegalArgumentException,
        NullArgumentException 

Source Link

Usage

From source file:com.colofabrix.mathparser.operators.IntegralOperator.java

@Override
public Operand executeOperation(Stack<Expression> operands) throws ExpressionException {
    if (operands.size() < this.getCurrentOperands()) {
        throw new ExpressionException("Wrong number of given parameters");
    }//  w w w . j a v  a2 s  . c o  m

    this.parser = new MathParser(this.getContext());

    // Interval start
    Apfloat lower = Operand.extractNumber(this.parser.executePostfix(operands.pop()));
    // Interval end
    Apfloat upper = Operand.extractNumber(this.parser.executePostfix(operands.pop()));
    // Expression to evaluate
    Expression expression = this.parser.minimise(operands.pop());
    // Integration variable
    Operand variable = (Operand) operands.pop();

    Expression2AMLAdapter function = new Expression2AMLAdapter(this.parser, expression, variable);
    BaseAbstractUnivariateIntegrator integrator = new SimpsonIntegrator();
    function.finalize();

    Apfloat result = null;
    int maxEval = 10000, eval = 10, exp = 5;

    // It starts with a low number of evaluation and exponentially increase to the set maximum
    while (result == null && eval <= maxEval) {
        try {
            result = new Apfloat(
                    integrator.integrate(eval, function, lower.doubleValue(), upper.doubleValue()));
        } catch (org.apache.commons.math3.exception.TooManyEvaluationsException e) {
            eval *= exp;
        }
    }

    return new Operand(result);
}