//
// Math.NET Numerics, part of the Math.NET Project
// http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics
//
// Copyright (c) 2009-2013 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
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// 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
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Collections.Generic;
using System.Numerics;
namespace IStation.Numerics.Integration
{
///
/// Approximation algorithm for definite integrals by the Trapezium rule of the Newton-Cotes family.
///
///
/// Wikipedia - Trapezium Rule
///
public static class NewtonCotesTrapeziumRule
{
///
/// Direct 2-point approximation of the definite integral in the provided interval by the trapezium rule.
///
/// The analytic smooth function to integrate.
/// Where the interval starts, inclusive and finite.
/// Where the interval stops, inclusive and finite.
/// Approximation of the finite integral in the given interval.
public static double IntegrateTwoPoint(Func f, double intervalBegin, double intervalEnd)
{
if (f == null)
{
throw new ArgumentNullException(nameof(f));
}
return (intervalEnd - intervalBegin)/2*(f(intervalBegin) + f(intervalEnd));
}
///
/// Direct 2-point approximation of the definite integral in the provided interval by the trapezium rule.
///
/// The analytic smooth complex function to integrate, defined on real domain.
/// Where the interval starts, inclusive and finite.
/// Where the interval stops, inclusive and finite.
/// Approximation of the finite integral in the given interval.
public static Complex ContourIntegrateTwoPoint(Func f, double intervalBegin, double intervalEnd)
{
if (f == null)
{
throw new ArgumentNullException(nameof(f));
}
return (intervalEnd - intervalBegin) / 2 * (f(intervalBegin) + f(intervalEnd));
}
///
/// Composite N-point approximation of the definite integral in the provided interval by the trapezium rule.
///
/// The analytic smooth function to integrate.
/// Where the interval starts, inclusive and finite.
/// Where the interval stops, inclusive and finite.
/// Number of composite subdivision partitions.
/// Approximation of the finite integral in the given interval.
public static double IntegrateComposite(Func f, double intervalBegin, double intervalEnd, int numberOfPartitions)
{
if (f == null)
{
throw new ArgumentNullException(nameof(f));
}
if (numberOfPartitions <= 0)
{
throw new ArgumentOutOfRangeException(nameof(numberOfPartitions), "Value must be positive (and not zero).");
}
double step = (intervalEnd - intervalBegin)/numberOfPartitions;
double offset = step;
double sum = 0.5*(f(intervalBegin) + f(intervalEnd));
for (int i = 0; i < numberOfPartitions - 1; i++)
{
// NOTE (ruegg, 2009-01-07): Do not combine intervalBegin and offset (numerical stability!)
sum += f(intervalBegin + offset);
offset += step;
}
return step*sum;
}
///
/// Composite N-point approximation of the definite integral in the provided interval by the trapezium rule.
///
/// The analytic smooth complex function to integrate, defined on real domain.
/// Where the interval starts, inclusive and finite.
/// Where the interval stops, inclusive and finite.
/// Number of composite subdivision partitions.
/// Approximation of the finite integral in the given interval.
public static Complex ContourIntegrateComposite(Func f, double intervalBegin, double intervalEnd, int numberOfPartitions)
{
if (f == null)
{
throw new ArgumentNullException(nameof(f));
}
if (numberOfPartitions <= 0)
{
throw new ArgumentOutOfRangeException(nameof(numberOfPartitions), "Value must be positive (and not zero).");
}
double step = (intervalEnd - intervalBegin) / numberOfPartitions;
double offset = step;
Complex sum = 0.5 * (f(intervalBegin) + f(intervalEnd));
for (int i = 0; i < numberOfPartitions - 1; i++)
{
// NOTE (ruegg, 2009-01-07): Do not combine intervalBegin and offset (numerical stability!)
sum += f(intervalBegin + offset);
offset += step;
}
return step * sum;
}
///
/// Adaptive approximation of the definite integral in the provided interval by the trapezium rule.
///
/// The analytic smooth function to integrate.
/// Where the interval starts, inclusive and finite.
/// Where the interval stops, inclusive and finite.
/// The expected accuracy of the approximation.
/// Approximation of the finite integral in the given interval.
public static double IntegrateAdaptive(Func f, double intervalBegin, double intervalEnd, double targetError)
{
if (f == null)
{
throw new ArgumentNullException(nameof(f));
}
int numberOfPartitions = 1;
double step = intervalEnd - intervalBegin;
double sum = 0.5*step*(f(intervalBegin) + f(intervalEnd));
for (int k = 0; k < 20; k++)
{
double midpointsum = 0;
for (int i = 0; i < numberOfPartitions; i++)
{
midpointsum += f(intervalBegin + ((i + 0.5)*step));
}
midpointsum *= step;
sum = 0.5*(sum + midpointsum);
step *= 0.5;
numberOfPartitions *= 2;
if (sum.AlmostEqualRelative(midpointsum, targetError))
{
break;
}
}
return sum;
}
///
/// Adaptive approximation of the definite integral in the provided interval by the trapezium rule.
///
/// The analytic smooth complex function to integrate, define don real domain.
/// Where the interval starts, inclusive and finite.
/// Where the interval stops, inclusive and finite.
/// The expected accuracy of the approximation.
/// Approximation of the finite integral in the given interval.
public static Complex ContourIntegrateAdaptive(Func f, double intervalBegin, double intervalEnd, double targetError)
{
if (f == null)
{
throw new ArgumentNullException(nameof(f));
}
int numberOfPartitions = 1;
double step = intervalEnd - intervalBegin;
Complex sum = 0.5 * step * (f(intervalBegin) + f(intervalEnd));
for (int k = 0; k < 20; k++)
{
Complex midpointsum = 0;
for (int i = 0; i < numberOfPartitions; i++)
{
midpointsum += f(intervalBegin + ((i + 0.5) * step));
}
midpointsum *= step;
sum = 0.5 * (sum + midpointsum);
step *= 0.5;
numberOfPartitions *= 2;
if (sum.AlmostEqualRelative(midpointsum, targetError))
{
break;
}
}
return sum;
}
///
/// Adaptive approximation of the definite integral by the trapezium rule.
///
/// The analytic smooth function to integrate.
/// Where the interval starts, inclusive and finite.
/// Where the interval stops, inclusive and finite.
/// Abscissa vector per level provider.
/// Weight vector per level provider.
/// First Level Step
/// The expected relative accuracy of the approximation.
/// Approximation of the finite integral in the given interval.
public static double IntegrateAdaptiveTransformedOdd(
Func f,
double intervalBegin, double intervalEnd,
IEnumerable levelAbscissas, IEnumerable levelWeights,
double levelOneStep, double targetRelativeError)
{
if (f == null)
{
throw new ArgumentNullException(nameof(f));
}
if (levelAbscissas == null)
{
throw new ArgumentNullException(nameof(levelAbscissas));
}
if (levelWeights == null)
{
throw new ArgumentNullException(nameof(levelWeights));
}
double linearSlope = 0.5*(intervalEnd - intervalBegin);
double linearOffset = 0.5*(intervalEnd + intervalBegin);
targetRelativeError /= 5*linearSlope;
using (var abcissasIterator = levelAbscissas.GetEnumerator())
using (var weightsIterator = levelWeights.GetEnumerator())
{
double step = levelOneStep;
// First Level
abcissasIterator.MoveNext();
weightsIterator.MoveNext();
double[] abcissasL1 = abcissasIterator.Current;
double[] weightsL1 = weightsIterator.Current;
double sum = f(linearOffset)*weightsL1[0];
for (int i = 1; i < abcissasL1.Length; i++)
{
sum += weightsL1[i]*(f((linearSlope*abcissasL1[i]) + linearOffset) + f(-(linearSlope*abcissasL1[i]) + linearOffset));
}
sum *= step;
// Additional Levels
double previousDelta = double.MaxValue;
for (int level = 1; abcissasIterator.MoveNext() && weightsIterator.MoveNext(); level++)
{
double[] abcissas = abcissasIterator.Current;
double[] weights = weightsIterator.Current;
double midpointsum = 0;
for (int i = 0; i < abcissas.Length; i++)
{
midpointsum += weights[i]*(f((linearSlope*abcissas[i]) + linearOffset) + f(-(linearSlope*abcissas[i]) + linearOffset));
}
midpointsum *= step;
sum = 0.5*(sum + midpointsum);
step *= 0.5;
double delta = Math.Abs(sum - midpointsum);
if (level == 1)
{
previousDelta = delta;
continue;
}
double r = Math.Log(delta)/Math.Log(previousDelta);
previousDelta = delta;
if (r > 1.9 && r < 2.1)
{
// convergence region
delta = Math.Sqrt(delta);
}
if (sum.AlmostEqualNormRelative(midpointsum, delta, targetRelativeError))
{
break;
}
}
return sum*linearSlope;
}
}
///
/// Adaptive approximation of the definite integral by the trapezium rule.
///
/// The analytic smooth complex function to integrate, defined on the real domain.
/// Where the interval starts, inclusive and finite.
/// Where the interval stops, inclusive and finite.
/// Abscissa vector per level provider.
/// Weight vector per level provider.
/// First Level Step
/// The expected relative accuracy of the approximation.
/// Approximation of the finite integral in the given interval.
public static Complex ContourIntegrateAdaptiveTransformedOdd(
Func f,
double intervalBegin, double intervalEnd,
IEnumerable levelAbscissas, IEnumerable levelWeights,
double levelOneStep, double targetRelativeError)
{
if (f == null)
{
throw new ArgumentNullException(nameof(f));
}
if (levelAbscissas == null)
{
throw new ArgumentNullException(nameof(levelAbscissas));
}
if (levelWeights == null)
{
throw new ArgumentNullException(nameof(levelWeights));
}
double linearSlope = 0.5 * (intervalEnd - intervalBegin);
double linearOffset = 0.5 * (intervalEnd + intervalBegin);
targetRelativeError /= 5 * linearSlope;
using (var abcissasIterator = levelAbscissas.GetEnumerator())
using (var weightsIterator = levelWeights.GetEnumerator())
{
double step = levelOneStep;
// First Level
abcissasIterator.MoveNext();
weightsIterator.MoveNext();
double[] abcissasL1 = abcissasIterator.Current;
double[] weightsL1 = weightsIterator.Current;
Complex sum = f(linearOffset) * weightsL1[0];
for (int i = 1; i < abcissasL1.Length; i++)
{
sum += weightsL1[i] * (f((linearSlope * abcissasL1[i]) + linearOffset) + f(-(linearSlope * abcissasL1[i]) + linearOffset));
}
sum *= step;
// Additional Levels
double previousDelta = double.MaxValue;
for (int level = 1; abcissasIterator.MoveNext() && weightsIterator.MoveNext(); level++)
{
double[] abcissas = abcissasIterator.Current;
double[] weights = weightsIterator.Current;
Complex midpointsum = 0;
for (int i = 0; i < abcissas.Length; i++)
{
midpointsum += weights[i] * (f((linearSlope * abcissas[i]) + linearOffset) + f(-(linearSlope * abcissas[i]) + linearOffset));
}
midpointsum *= step;
sum = 0.5 * (sum + midpointsum);
step *= 0.5;
double delta = Complex.Abs(sum - midpointsum);
if (level == 1)
{
previousDelta = delta;
continue;
}
double r = Math.Log(delta) / Math.Log(previousDelta);
previousDelta = delta;
if (r > 1.9 && r < 2.1)
{
// convergence region
delta = Math.Sqrt(delta);
}
if (sum.Real.AlmostEqualNormRelative(midpointsum.Real, delta, targetRelativeError)
&& sum.Imaginary.AlmostEqualNormRelative(midpointsum.Imaginary, delta, targetRelativeError))
{
break;
}
}
return sum * linearSlope;
}
}
}
}