diff --git a/src/Numerics/Integrate.cs b/src/Numerics/Integrate.cs index 84398dbe..01400e98 100644 --- a/src/Numerics/Integrate.cs +++ b/src/Numerics/Integrate.cs @@ -62,5 +62,33 @@ namespace MathNet.Numerics { return DoubleExponentialTransformation.Integrate(f, intervalBegin, intervalEnd, 1e-8); } + + /// + /// Approximates a definite integral using an Nth order Gauss-Legendre rule. + /// + /// The analytic smooth function to integrate. + /// Where the interval starts, exclusive and finite. + /// Where the interval ends, exclusive and finite. + /// Defines an Nth order Gauss-Legendre rule. The order also defines the number of abscissas and weights for the rule. Precomputed Gauss-Legendre abscissas/weights for orders 2,. . ., 20, 32, 64, 96, 100, 128, 256, 512, 1024 are used, otherwise they're calulcated on the fly. + /// Approximation of the finite integral in the given interval. + public static double GaussLegendre(Func f, double invervalBegin, double invervalEnd, int order) + { + return GaussLegendreRule.Integrate(f, invervalBegin, invervalEnd, order); + } + + /// + /// Approximates a 2-dimensional definite integral using an Nth order Gauss-Legendre rule over the rectangle [a,b] x [c,d]. + /// + /// The 2-dimensional analytic smooth function to integrate. + /// Where the interval starts for the first (inside) integral, exclusive and finite. + /// Where the interval ends for the first (inside) integral, exclusive and finite. + /// Where the interval starts for the second (outside) integral, exclusive and finite. + /// /// Where the interval ends for the second (outside) integral, exclusive and finite. + /// Defines an Nth order Gauss-Legendre rule. The order also defines the number of abscissas and weights for the rule. Precomputed Gauss-Legendre abscissas/weights for orders 2,. . ., 20, 32, 64, 96, 100, 128, 256, 512, 1024 are used, otherwise they're calulcated on the fly. + /// Approximation of the finite integral in the given interval. + public static double GaussLegendre(Func f, double invervalBeginA, double invervalEndA, double invervalBeginB, double invervalEndB, int order) + { + return GaussLegendreRule.Integrate(f, invervalBeginA, invervalEndA, invervalBeginB, invervalEndB, order); + } } } diff --git a/src/Numerics/Integration/GaussRule/GaussLegendrePointFactory.cs b/src/Numerics/Integration/GaussRule/GaussLegendrePointFactory.cs index 78967b4b..3dbd961f 100644 --- a/src/Numerics/Integration/GaussRule/GaussLegendrePointFactory.cs +++ b/src/Numerics/Integration/GaussRule/GaussLegendrePointFactory.cs @@ -44,7 +44,7 @@ namespace MathNet.Numerics.Integration.GaussRule /// /// Getter for the GaussPoint. /// - /// Defines an Nth order Gauss-Legendre rule. Precomputed Gauss-Legendre abscissas/weights for orders 2,. . ., 20, 32, 64, 96, 100, 128, 256, 512, 1024 are used, otherwise they're calulcated on the fly. Furthermore, caching in a private static field is used to speed up the algorithm. + /// Defines an Nth order Gauss-Legendre rule. Precomputed Gauss-Legendre abscissas/weights for orders 2,. . ., 20, 32, 64, 96, 100, 128, 256, 512, 1024 are used, otherwise they're calulcated on the fly. /// Object containing the non-negative abscissas/weights, order, and intervalBegin/intervalEnd. The non-negative abscissas/weights are generated over the interval [-1,1] for the given order. public static GaussPoint GetGaussPoint(int order) { diff --git a/src/UnitTests/IntegrationTests/IntegrationTest.cs b/src/UnitTests/IntegrationTests/IntegrationTest.cs index 26c6851c..2a6f3fe0 100644 --- a/src/UnitTests/IntegrationTests/IntegrationTest.cs +++ b/src/UnitTests/IntegrationTests/IntegrationTest.cs @@ -220,6 +220,21 @@ namespace MathNet.Numerics.UnitTests.IntegrationTests Assert.Less(relativeError, 5e-16); } + /// + /// Gauss-Legendre rule supports integration. + /// + /// Defines an Nth order Gauss-Legendre rule. The order also defines the number of abscissas and weights for the rule. + [TestCase(19)] + [TestCase(20)] + [TestCase(21)] + [TestCase(22)] + public void TestIntegrateGaussLegendre(int order) + { + double appoximateArea = Integrate.GaussLegendre(TargetFunctionA, StartA, StopA, order); + double relativeError = Math.Abs(TargetAreaA - appoximateArea) / TargetAreaA; + Assert.Less(relativeError, 5e-16); + } + /// /// Gauss-Legendre rule supports 2-dimensional integration over the rectangle. /// @@ -235,6 +250,21 @@ namespace MathNet.Numerics.UnitTests.IntegrationTests Assert.Less(relativeError, 1e-15); } + /// + /// Gauss-Legendre rule supports 2-dimensional integration over the rectangle. + /// + /// Defines an Nth order Gauss-Legendre rule. The order also defines the number of abscissas and weights for the rule. + [TestCase(19)] + [TestCase(20)] + [TestCase(21)] + [TestCase(22)] + public void TestIntegrateGaussLegendre2D(int order) + { + double appoximateArea = Integrate.GaussLegendre(TargetFunctionB, StartA, StopA, StartB, StopB, order); + double relativeError = Math.Abs(TargetAreaB - appoximateArea) / TargetAreaB; + Assert.Less(relativeError, 1e-15); + } + /// /// Gauss-Legendre rule supports obtaining the abscissas/weights. In this case, they're used for integration. ///