Extreme Optimization™: Complexity made simple.

Math and Statistics
Libraries for .NET

  • Home
  • Features
    • Math Library
    • Vector and Matrix Library
    • Statistics Library
    • Performance
    • Usability
  • Documentation
    • Introduction
    • Math Library User's Guide
    • Vector and Matrix Library User's Guide
    • Data Analysis Library User's Guide
    • Statistics Library User's Guide
    • Reference
  • Resources
    • Downloads
    • QuickStart Samples
    • Sample Applications
    • Frequently Asked Questions
    • Technical Support
  • Blog
  • Order
  • Company
    • About us
    • Testimonials
    • Customers
    • Press Releases
    • Careers
    • Partners
    • Contact us
Introduction
Deployment Guide
Nuget packages
Configuration
Using Parallelism
Expand Mathematics Library User's GuideMathematics Library User's Guide
Expand Vector and Matrix Library User's GuideVector and Matrix Library User's Guide
Expand Data Analysis Library User's GuideData Analysis Library User's Guide
Expand Statistics Library User's GuideStatistics Library User's Guide
Expand Data Access Library User's GuideData Access Library User's Guide
Expand ReferenceReference
  • Extreme Optimization
    • Features
    • Solutions
    • Documentation
    • QuickStart Samples
    • Sample Applications
    • Downloads
    • Technical Support
    • Download trial
    • How to buy
    • Blog
    • Company
    • Resources
  • Documentation
    • Introduction
    • Deployment Guide
    • Nuget packages
    • Configuration
    • Using Parallelism
    • Mathematics Library User's Guide
    • Vector and Matrix Library User's Guide
    • Data Analysis Library User's Guide
    • Statistics Library User's Guide
    • Data Access Library User's Guide
    • Reference
  • Mathematics Library User's Guide
    • General Classes
    • Mathematical Functions
    • Complex Numbers
    • Arbitrary Precision Arithmetic
    • Automatic Differentiation
    • Curves and Interpolation
    • Curve Fitting
    • Solving Equations
    • Optimization
    • Calculus
    • Fast Fourier Transforms
    • Random Numbers
    • Generic Arithmetic
    • Appendices
  • Calculus
    • Numerical Integration
    • Numerical Integration in Two Dimensions
    • Numerical Integration in more than Two Dimensions
    • Numerical Differentiation
    • Ordinary Differential Equations
  • Numerical Integration
    • The NumericalIntegrator Class
    • Fixed interval methods
    • Gauss-Kronrod Integration Rules
    • Adaptive Numerical Integration
  • Adaptive Numerical Integration

Adaptive Numerical Integration

Extreme Optimization Numerical Libraries for .NET Professional

The AdaptiveIntegrator class provides the most robust, reliable, and in most cases the fastest numerical integration algorithm. It uses one of the GaussKronrodIntegrator derived integration rules to approximate the integral over subintervals. In each step, the interval with the largest estimated error is divided in two, and new approximations for these two subintervals are calculated. This process continues until the total estimated error over all intervals is within the desired tolerance.

The IntegrationRule property lets you specify which integration rule to use to approximate integrals on a subinterval. The default is the 21-point Gauss-Kronrod rule for normal integrands, and the 15-point Gauss-Kronrod rule when singularities are expected. For oscillating target functions, the higher order rules will tend to give better results. This value is not limited to Gauss-Kronrod integration rules. Any integration rule that provides an estimate for the error can be used.

C#
Copy
AdaptiveIntegrator integrator = new AdaptiveIntegrator();
integrator.RelativeTolerance = 1e-5;
integrator.ConvergenceCriterion = ConvergenceCriterion.WithinRelativeTolerance;
double result = integrator.Integrate(Math.Sin, 0, 2);
Console.WriteLine("sin(x) on [0,2]");
Console.WriteLine("Romberg integrator:");
Console.WriteLine("  Value: {0}", result);
Console.WriteLine("  Status: {0}", integrator.Status);
Console.WriteLine("  Estimated error: {0}", integrator.EstimatedError);
Console.WriteLine("  Iterations: {0}", integrator.IterationsNeeded);
Console.WriteLine("  Function evaluations: {0}", integrator.EvaluationsNeeded);
Console.WriteLine("  Order: {0}", integrator.Order);

Several enhancements make this the integration algorithm of choice, especially for 'difficult' integrands.

An extra property, UseExtrapolation, lets you specify whether special precautions should be taken to deal with singularities. If UseExtrapolation is true, an extrapolation algorithm is used to approximate the integral near a singularity. Because this option requires extra resources, it is recommended only for target functions that actually have singularities inside or at the boundaries of the integration interval.

The SetSingularities method lets you specify the points inside the integration interval where the integrand has a singularity or a discontinuity. The value of this property is a Double array containing the problem points. Extrapolation is used once again to approximate the integral near the singularity or singularities. Specifying the problem points in the target function can lead to speed-ups of several orders of magnitude.

C#
Copy
// Function with singularities inside the integration interval:
Func<double, double> f = x => x * x * x
  * Math.Log(Math.Abs((x * x - 1) * (x * x - 2)));
integrator.AbsoluteTolerance = 1e-8;
integrator.ConvergenceCriterion = ConvergenceCriterion.WithinAbsoluteTolerance;
integrator.SetSingularities(1, Math.Sqrt(2));
result = integrator.Integrate(f, 0, 3);
Console.WriteLine("Singularities inside the integration interval");
Console.WriteLine("  Value: {0}", integrator.Result);
Console.WriteLine("  Exact value: 52.740748383471444998");
Console.WriteLine("  Status: {0}", integrator.Status);
Console.WriteLine("  Estimated error: {0}", integrator.EstimatedError);
Console.WriteLine("  Iterations: {0}", integrator.IterationsNeeded);
Console.WriteLine("  Function evaluations: {0}", integrator.EvaluationsNeeded);

If the only discontinuities are on a boundary of the integration interval, you do not need to specify the singularities. You have to make sure, however, that UseExtrapolation is set to true.

C#
Copy
// Function with singularities on a boundary:
Func<double, double> f2 = x => Math.Pow(x, -0.9) * Math.Log(1 / x);
integrator.AbsoluteTolerance = 1e-8;
integrator.ConvergenceCriterion = ConvergenceCriterion.WithinAbsoluteTolerance;
integrator.UseExtrapolation = true;
result = integrator.Integrate(f2, 0, 1);
Console.WriteLine("Integrand with a singularity on a boundary:");
Console.WriteLine("  Value: {0}", integrator.Result);
Console.WriteLine("  Exact value: 100");
Console.WriteLine("  Status: {0}", integrator.Status);
Console.WriteLine("  Estimated error: {0}", integrator.EstimatedError);
Console.WriteLine("  Iterations: {0}", integrator.IterationsNeeded);
Console.WriteLine("  Function evaluations: {0}", integrator.EvaluationsNeeded);

Unlike all methods mentioned previously, this integrator can deal successfully with infinite integration intervals. Specify Double.NegativeInfinity or Double.PositiveInfinity for one or both bounds of the integration interval. Internally, the class transforms the infinite interval to a finite interval, and performs the integration of the transformed target function over the transformed interval.

C#
Copy
Func<double, double> f3 = x => Math.Exp(-x - x * x);
integrator.AbsoluteTolerance = 1e-8;
integrator.ConvergenceCriterion = ConvergenceCriterion.WithinAbsoluteTolerance;
result = integrator.Integrate(f3,
    Double.NegativeInfinity, Double.PositiveInfinity);
Console.WriteLine("Exp(-x^2-x) on [-inf,inf]");
Console.WriteLine("  Value: {0}", integrator.Result);
Console.WriteLine("  Status: {0}", integrator.Status);
Console.WriteLine("  Estimated error: {0}", integrator.EstimatedError);
Console.WriteLine("  Iterations: {0}", integrator.IterationsNeeded);
Console.WriteLine("  Function evaluations: {0}", integrator.EvaluationsNeeded);
References

R. Piessens, E. de Doncker, C. Uberhuber and D. Kahaner, QUADPACK, A Subroutine Package for Automatic Integration , Springer-Verlag, New York, 1983.

S.D. Conte and Carl de Boor, Elementary Numerical Analysis: An Algorithmic Approach, McGraw-Hill, 1972.

Copyright (c) 2004-2021 ExoAnalytics Inc.

Send comments on this topic to support@extremeoptimization.com

Copyright © 2004-2021, Extreme Optimization. All rights reserved.
Extreme Optimization, Complexity made simple, M#, and M Sharp are trademarks of ExoAnalytics Inc.
Microsoft, Visual C#, Visual Basic, Visual Studio, Visual Studio.NET, and the Optimized for Visual Studio logo
are registered trademarks of Microsoft Corporation.