/// <summary> /// Simpson's integration method. /// <para> /// Note that the Commons implementation fails if the lower bound is larger than the upper - /// in this case, the bounds are reversed and the result negated. /// /// </para> /// </summary> /// <param name="f"> The function to integrate, not null </param> /// <param name="lower"> The lower bound, not null </param> /// <param name="upper"> The upper bound, not null </param> /// <returns> The result of the integration </returns> public virtual double?integrate(System.Func <double, double> f, double?lower, double?upper) { ArgChecker.notNull(f, "function"); ArgChecker.notNull(lower, "lower bound"); ArgChecker.notNull(upper, "upper bound"); try { if (lower < upper) { return(integrator.integrate(MAX_EVAL, CommonsMathWrapper.wrapUnivariate(f), lower, upper)); } log.info("Upper bound was less than lower bound; swapping bounds and negating result"); return(-integrator.integrate(MAX_EVAL, CommonsMathWrapper.wrapUnivariate(f), upper, lower)); } catch (Exception e) when(e is NumberIsTooSmallException || e is NumberIsTooLargeException) { throw new MathException(e); } }
/// <summary> /// Trapezoid integration method. Note that the Commons implementation fails if the lower bound is larger than the upper - /// in this case, the bounds are reversed and the result negated. /// {@inheritDoc} /// </summary> public virtual double?integrate(System.Func <double, double> f, double?lower, double?upper) { ArgChecker.notNull(f, "f"); ArgChecker.notNull(lower, "lower"); ArgChecker.notNull(upper, "upper"); try { if (lower < upper) { return(INTEGRATOR.integrate(MAX_EVAL, CommonsMathWrapper.wrapUnivariate(f), lower, upper)); } log.info("Upper bound was less than lower bound; swapping bounds and negating result"); return(-INTEGRATOR.integrate(MAX_EVAL, CommonsMathWrapper.wrapUnivariate(f), upper, lower)); } catch (Exception e) when(e is MaxCountExceededException || e is MathIllegalArgumentException) { throw new MathException(e); } }