CodePlexProject Hosting for Open Source Software

The Integrate function does numerical integration a user-supplied function. The following C# code snippet illustrates its use.

// If the integrand is a defined method, you can simply name the method. // Here we integrate over the "hump" of the cosine function. double I1 = FunctionMath.Integrate( Math.Cos, Interval.FromEndpoints(-Math.PI/2.0, Math.PI/2.0) ); // You can also use anonymous delegate notation to define the integrand inline. double I2 = FunctionMath.Integrate( x => Math.Sqrt(Math.Sin(x)), Interval.FromEndpoints(0.0, Math.PI) ); // Integrals can have infinite endpoints. double I3 = FunctionMath.Integrate( x => Math.Exp(-x * x / 2.0), Interval.FromEndpoints(Double.NegativeInfinity, Double.PositiveInfinity) ); // Integrable endpoint singularities are also allowed. (Log goes to negative infinity at zero.) double I4 = FunctionMath.Integrate(Math.Log, Interval.FromEndpoints(0.0, 1.0)); // By default, we target nearly full precision and allow many thousands of function evaluations to achieve it. // If your accuracy requirements are lower, or you want to change the number of allowed function // evaluations, you can do so using EvaluationSettings. EvaluationSettings integrationSettings = new EvaluationSettings() { RelativePrecision = 1.0E-5, EvaluationBudget = 500 }; double I5 = FunctionMath.Integrate( AdvancedMath.LogGamma, Interval.FromEndpoints(0.0, 1.0), integrationSettings );

Last edited Sep 22, 2012 at 1:57 AM by ichbin, version 4

Explicitly declare the namespace of Interval as "Meta.Numerics.Interval" to clear compiler errors,

so change Interval.FromEndpoints(0.0, 1.0) to Meta.Numerics.Interval.FromEndpoints(0.0, 1.0),

and do all cases when calling method 'Interval'.

so change Interval.FromEndpoints(0.0, 1.0) to Meta.Numerics.Interval.FromEnd

and do all cases when calling method 'Interval'.

Need to change the last line to ".Value", to obtain a double from FunctionMath.Integrate.

double I5 = FunctionMath.Integrate(

AdvancedMath.LogGamma,

Meta.Numerics.Interval.FromEndpoints(0.0, 1.0),

integrationSettings

).Value;

double I5 = FunctionMath.Integrate(

AdvancedMath.LogGamma,

Meta.Numerics.Interval.FromEnd

integrationSettings

).Value;