Browse Source

Integration: align Integrate-facade functions with use cases

netstandard
Christoph Ruegg 11 years ago
parent
commit
d63d47e6ff
  1. 21
      src/Numerics/Integrate.cs
  2. 22
      src/Numerics/Integration/GaussLegendreRule.cs
  3. 18
      src/Numerics/Integration/GaussRule/GaussLegendrePointFactory.cs
  4. 48
      src/UnitTests/IntegrationTests/IntegrationTest.cs

21
src/Numerics/Integrate.cs

@ -4,7 +4,7 @@
// http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com
//
// Copyright (c) 2009-2010 Math.NET
// Copyright (c) 2009-2016 Math.NET
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
@ -64,16 +64,18 @@ namespace MathNet.Numerics
}
/// <summary>
/// Approximates a definite integral using an Nth order Gauss-Legendre rule.
/// Approximates a 2-dimensional definite integral using an Nth order Gauss-Legendre rule over the rectangle [a,b] x [c,d].
/// </summary>
/// <param name="f">The analytic smooth function to integrate.</param>
/// <param name="invervalBegin">Where the interval starts, exclusive and finite.</param>
/// <param name="invervalEnd">Where the interval ends, exclusive and finite.</param>
/// <param name="f">The 2-dimensional analytic smooth function to integrate.</param>
/// <param name="invervalBeginA">Where the interval starts for the first (inside) integral, exclusive and finite.</param>
/// <param name="invervalEndA">Where the interval ends for the first (inside) integral, exclusive and finite.</param>
/// <param name="invervalBeginB">Where the interval starts for the second (outside) integral, exclusive and finite.</param>
/// /// <param name="invervalEndB">Where the interval ends for the second (outside) integral, exclusive and finite.</param>
/// <param name="order">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.</param>
/// <returns>Approximation of the finite integral in the given interval.</returns>
public static double GaussLegendre(Func<double, double> f, double invervalBegin, double invervalEnd, int order)
public static double OnRectangle(Func<double, double, double> f, double invervalBeginA, double invervalEndA, double invervalBeginB, double invervalEndB, int order)
{
return GaussLegendreRule.Integrate(f, invervalBegin, invervalEnd, order);
return GaussLegendreRule.Integrate(f, invervalBeginA, invervalEndA, invervalBeginB, invervalEndB, order);
}
/// <summary>
@ -84,11 +86,10 @@ namespace MathNet.Numerics
/// <param name="invervalEndA">Where the interval ends for the first (inside) integral, exclusive and finite.</param>
/// <param name="invervalBeginB">Where the interval starts for the second (outside) integral, exclusive and finite.</param>
/// /// <param name="invervalEndB">Where the interval ends for the second (outside) integral, exclusive and finite.</param>
/// <param name="order">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.</param>
/// <returns>Approximation of the finite integral in the given interval.</returns>
public static double GaussLegendre(Func<double, double, double> f, double invervalBeginA, double invervalEndA, double invervalBeginB, double invervalEndB, int order)
public static double OnRectangle(Func<double, double, double> f, double invervalBeginA, double invervalEndA, double invervalBeginB, double invervalEndB)
{
return GaussLegendreRule.Integrate(f, invervalBeginA, invervalEndA, invervalBeginB, invervalEndB, order);
return GaussLegendreRule.Integrate(f, invervalBeginA, invervalEndA, invervalBeginB, invervalEndB, 32);
}
}
}

22
src/Numerics/Integration/GaussLegendreRule.cs

@ -38,8 +38,8 @@ namespace MathNet.Numerics.Integration
/// </summary>
public class GaussLegendreRule
{
private readonly GaussPoint gaussLegendrePoint;
private readonly GaussPoint _gaussLegendrePoint;
/// <summary>
/// Initializes a new instance of the <see cref="GaussLegendreRule"/> class.
/// </summary>
@ -48,7 +48,7 @@ namespace MathNet.Numerics.Integration
/// <param name="order">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.</param>
public GaussLegendreRule(double intervalBegin, double intervalEnd, int order)
{
gaussLegendrePoint = Map(GaussLegendrePointFactory.GetGaussPoint(order), intervalBegin, intervalEnd);
_gaussLegendrePoint = Map(GaussLegendrePointFactory.GetGaussPoint(order), intervalBegin, intervalEnd);
}
/// <summary>
@ -58,7 +58,7 @@ namespace MathNet.Numerics.Integration
/// <returns>The ith abscissa.</returns>
public double GetAbscissa(int index)
{
return gaussLegendrePoint.Abscissas[index];
return _gaussLegendrePoint.Abscissas[index];
}
/// <summary>
@ -68,7 +68,7 @@ namespace MathNet.Numerics.Integration
/// <returns>The ith weight.</returns>
public double GetWeight(int index)
{
return gaussLegendrePoint.Weights[index];
return _gaussLegendrePoint.Weights[index];
}
/// <summary>
@ -78,29 +78,29 @@ namespace MathNet.Numerics.Integration
{
get
{
return gaussLegendrePoint.Order;
return _gaussLegendrePoint.Order;
}
}
/// <summary>
/// Getter for the InvervalBegin.
/// Getter for the InvervalBegin.
/// </summary>
public double IntervalBegin
{
get
{
return gaussLegendrePoint.IntervalBegin;
return _gaussLegendrePoint.IntervalBegin;
}
}
/// <summary>
/// Getter for the InvervalEnd.
/// Getter for the InvervalEnd.
/// </summary>
public double IntervalEnd
{
get
{
return gaussLegendrePoint.IntervalEnd;
return _gaussLegendrePoint.IntervalEnd;
}
}
@ -249,4 +249,4 @@ namespace MathNet.Numerics.Integration
return c*a*sum;
}
}
}
}

18
src/Numerics/Integration/GaussRule/GaussLegendrePointFactory.cs

File diff suppressed because one or more lines are too long

48
src/UnitTests/IntegrationTests/IntegrationTest.cs

@ -3,7 +3,9 @@
// http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com
// Copyright (c) 2009-2010 Math.NET
//
// Copyright (c) 2009-2016 Math.NET
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
@ -12,8 +14,10 @@
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
@ -88,22 +92,34 @@ namespace MathNet.Numerics.UnitTests.IntegrationTests
private const double TargetAreaB = 11.7078776759298776163;
/// <summary>
/// Test integrate portal.
/// Test Integrate facade for simple use cases.
/// </summary>
[Test]
public void TestIntegratePortal()
public void TestIntegrateFacade()
{
Assert.AreEqual(
TargetAreaA,
Integrate.OnClosedInterval(TargetFunctionA, StartA, StopA),
1e-5,
"Basic");
"Interval");
Assert.AreEqual(
TargetAreaA,
Integrate.OnClosedInterval(TargetFunctionA, StartA, StopA, 1e-10),
1e-10,
"Basic Target 1e-10");
"Interval, Target 1e-10");
Assert.AreEqual(
Integrate.OnRectangle(TargetFunctionB, StartA, StopA, StartB, StopB),
TargetAreaB,
1e-12,
"Rectangle");
Assert.AreEqual(
Integrate.OnRectangle(TargetFunctionB, StartA, StopA, StartB, StopB, 22),
TargetAreaB,
1e-10,
"Rectangle, Gauss-Legendre Order 22");
}
/// <summary>
@ -220,21 +236,6 @@ namespace MathNet.Numerics.UnitTests.IntegrationTests
Assert.Less(relativeError, 5e-16);
}
/// <summary>
/// Gauss-Legendre rule supports integration.
/// </summary>
/// <param name="order">Defines an Nth order Gauss-Legendre rule. The order also defines the number of abscissas and weights for the rule.</param>
[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);
}
/// <summary>
/// Gauss-Legendre rule supports 2-dimensional integration over the rectangle.
/// </summary>
@ -260,13 +261,10 @@ namespace MathNet.Numerics.UnitTests.IntegrationTests
[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);
}
/// <summary>
/// Gauss-Legendre rule supports obtaining the abscissas/weights. In this case, they're used for integration.
/// Gauss-Legendre rule supports obtaining the abscissas/weights. In this case, they're used for integration.
/// </summary>
/// <param name="order">Defines an Nth order Gauss-Legendre rule. The order also defines the number of abscissas and weights for the rule.</param>
[TestCase(19)]
@ -309,4 +307,4 @@ namespace MathNet.Numerics.UnitTests.IntegrationTests
Assert.AreEqual(gaussLegendre.IntervalEnd, StopA);
}
}
}
}

Loading…
Cancel
Save