Advanced Integration in IronPython QuickStart Sample

Illustrates more advanced numerical integration using the AdaptiveIntegrator class in IronPython.

View this sample in: C# Visual Basic F#

```Python
import numerics

from math import *

# The numerical integration classes reside in the
# Extreme.Mathematics.Calculus namespace.
from Extreme.Mathematics.Calculus import *
# Function delegates reside in the Extreme.Mathematics
# namespace.
from Extreme.Mathematics import *

# AdaptiveGaussKronrodIntegrator numerical integrator class
# classes in the Extreme.Mathematics.Calculus namespace of the Extreme
# Optimization Numerical Libraries for .NET.
# </summary>

# Numerical integration algorithms fall into two
# main categories: adaptive and non-adaptive.
# This QuickStart Sample illustrates the use of
# the adaptive numerical integrator implemented by
# the AdaptiveIntegrator class. This class is the
# most advanced of the numerical integration 
# classes.
#
# All numerical integration classes derive from
# NumericalIntegrator. This abstract base class
# defines properties and methods that are shared
# by all numerical integration classes.

#
# The integrand
#

# The function we are integrating must be
# provided as a Func<double, double>. For more
# information about this delegate, see the
# FunctionDelegates QuickStart sample.
#
# Construct an instance of the integrator class:
integrator = AdaptiveIntegrator()

#
# Adaptive integrator basics
#

# All the properties and methods defined by the
# NumericalIntegrator base class are available.
# See the BasicIntegration QuickStart Sample 
# for details. The AdaptiveIntegrator class defines
# the following additional properties:
#
# The IntegrationRule property gets or sets the
# integration rule that is to be used for
# integrating subintervals. It can be any
# object derived from IntegrationRule.
#
# For convenience, a series of Gauss-Kronrod
# integration rules of order 15, 21, 31, 41, 51, # and 61 have been provided.
integrator.IntegrationRule = IntegrationRule.CreateGaussKronrod15PointRule()
# The UseAcceleration property specifies whether
# precautions should be taken for singularities
# in the integration interval.
integrator.UseExtrapolation = False
# Finally, the Singularities property allows you
# to specify singularities or discontinuities
# inside the integration interval. See the
# sample below for details.

#
# Integration over infinite intervals
# 

integrator.AbsoluteTolerance = 1e-8
integrator.ConvergenceCriterion = ConvergenceCriterion.WithinAbsoluteTolerance
# The Integrate method performs the actual 
# integration. To integrate over an infinite
# interval, simply use either or both of
# float.PositiveInfinity and 
# float.NegativeInfinity as bounds:
result = integrator.Integrate(lambda x: exp(-x - x*x), float.NegativeInfinity, float.PositiveInfinity)

print "Exp(-x^2-x) on [-inf,inf]"
print "  Value:      ", integrator.Result
print "  Exact value:", exp(0.25) * Constants.SqrtPi
# To see whether the algorithm ended normally, # inspect the Status property:
print "  Status:", integrator.Status
print "  Estimated error:", integrator.EstimatedError
print "  Iterations:", integrator.IterationsNeeded
print "  Function evaluations:", integrator.EvaluationsNeeded

# If you just want the result, you can also call the Integrate
# extension method directly on the integrand:
integrand = lambda(x): exp(-x - x ** 2)
result = FunctionMath.Integrate(integrand, float.NegativeInfinity, float.PositiveInfinity)
print "  Value:      ", result

#
# Functions with singularities at the end points
# of the integration interval.
#

# Thanks to the adaptive nature of the algorithm, # special measures can be taken to accelerate 
# convergence near singularities. To enable this
# acceleration, set the Singularities property
# to True.
integrator.UseExtrapolation = True
# We'll use the function that gives the Romberg
# integrator in the BasicIntegration QuickStart
# sample trouble.
result = integrator.Integrate(lambda x: x ** -0.9 * log(1/x), 0.0, 1.0)
print "Singularities on boundary:"
print "  Value:      ", integrator.Result
print "  Exact value: 100"
print "  Status:", integrator.Status
print "  Estimated error:", integrator.EstimatedError
# Where Romberg integration failed after 1,000,000
# function evaluations, we find the correct answer 
# to within tolerance using only 135 function
# evaluations!
print "  Iterations:", integrator.IterationsNeeded
print "  Function evaluations:", integrator.EvaluationsNeeded

#
# Functions with singularities or discontinuities
# inside the interval.
#
integrator.UseExtrapolation = True
# We will pass an array containing the interior
# singularities to the integrator through the
# Singularities property:
integrator.SetSingularities(1, sqrt(2))
integrator.Integrate(lambda x: x**3 * log(abs((x**2-1) * (x**2 - 2))), 0.0, 3.0)
print "Singularities inside the interval:"
print "  Value:      ", integrator.Result
print "  Exact value: 52.740748383471444998"
print "  Status:", integrator.Status
print "  Estimated error:", integrator.EstimatedError
print "  Iterations:", integrator.IterationsNeeded
print "  Function evaluations:", integrator.EvaluationsNeeded

```