Browse Source

Integration: clean up old overly complicated formatting

pull/163/head
Christoph Ruegg 13 years ago
parent
commit
f1a6746f3b
  1. 23
      src/Numerics/Integrate.cs
  2. 255
      src/Numerics/Integration/DoubleExponentialTransformation.cs
  3. 28
      src/Numerics/Integration/NewtonCotesTrapeziumRule.cs
  4. 13
      src/Numerics/Integration/SimpsonRule.cs
  5. 16
      src/Numerics/Interpolate.cs

23
src/Numerics/Integrate.cs

@ -51,17 +51,9 @@ namespace MathNet.Numerics
/// <param name="intervalEnd">Where the interval stops, inclusive and finite.</param> /// <param name="intervalEnd">Where the interval stops, inclusive and finite.</param>
/// <param name="targetAbsoluteError">The expected relative accuracy of the approximation.</param> /// <param name="targetAbsoluteError">The expected relative accuracy of the approximation.</param>
/// <returns>Approximation of the finite integral in the given interval.</returns> /// <returns>Approximation of the finite integral in the given interval.</returns>
public static double OnClosedInterval( public static double OnClosedInterval(Func<double, double> f, double intervalBegin, double intervalEnd, double targetAbsoluteError)
Func<double, double> f,
double intervalBegin,
double intervalEnd,
double targetAbsoluteError)
{ {
return Det.Integrate( return Det.Integrate(f, intervalBegin, intervalEnd, targetAbsoluteError);
f,
intervalBegin,
intervalEnd,
targetAbsoluteError);
} }
/// <summary> /// <summary>
@ -71,16 +63,9 @@ namespace MathNet.Numerics
/// <param name="intervalBegin">Where the interval starts, inclusive and finite.</param> /// <param name="intervalBegin">Where the interval starts, inclusive and finite.</param>
/// <param name="intervalEnd">Where the interval stops, inclusive and finite.</param> /// <param name="intervalEnd">Where the interval stops, inclusive and finite.</param>
/// <returns>Approximation of the finite integral in the given interval.</returns> /// <returns>Approximation of the finite integral in the given interval.</returns>
public static double OnClosedInterval( public static double OnClosedInterval(Func<double, double> f, double intervalBegin, double intervalEnd)
Func<double, double> f,
double intervalBegin,
double intervalEnd)
{ {
return Det.Integrate( return Det.Integrate(f, intervalBegin, intervalEnd, 1e-8);
f,
intervalBegin,
intervalEnd,
1e-8);
} }
} }
} }

255
src/Numerics/Integration/DoubleExponentialTransformation.cs

@ -4,7 +4,7 @@
// http://github.com/mathnet/mathnet-numerics // http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com // http://mathnetnumerics.codeplex.com
// //
// Copyright (c) 2009-2010 Math.NET // Copyright (c) 2009-2013 Math.NET
// //
// Permission is hereby granted, free of charge, to any person // Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation // obtaining a copy of this software and associated documentation
@ -39,6 +39,129 @@ namespace MathNet.Numerics.Integration
/// </summary> /// </summary>
public class DoubleExponentialTransformation public class DoubleExponentialTransformation
{ {
/// <summary>
/// Maximum number of iterations, until the asked
/// maximum error is (likely to be) satisfied.
/// </summary>
const int NumberOfMaximumLevels = 10;
/// <summary>
/// Abscissa vector per level provider.
/// </summary>
IEnumerable<double[]> _levelAbcissas;
/// <summary>
/// Weight vector per level provider.
/// </summary>
IEnumerable<double[]> _levelWeights;
/// <summary>
/// Approximate the integral by the double exponential transformation
/// </summary>
/// <param name="f">The analytic smooth function to integrate.</param>
/// <param name="intervalBegin">Where the interval starts, inclusive and finite.</param>
/// <param name="intervalEnd">Where the interval stops, inclusive and finite.</param>
/// <param name="targetRelativeError">The expected relative accuracy of the approximation.</param>
/// <returns>Approximation of the finite integral in the given interval.</returns>
public double Integrate(Func<double, double> f, double intervalBegin, double intervalEnd, double targetRelativeError)
{
if (_levelAbcissas == null)
{
_levelAbcissas = ProvideLevelAbcissas();
_levelWeights = ProvideLevelWeights();
}
return NewtonCotesTrapeziumRule.IntegrateAdaptiveTransformedOdd(
f,
intervalBegin, intervalEnd,
_levelAbcissas, _levelWeights,
1, targetRelativeError);
}
/// <summary>
/// Abscissa vector per level provider.
/// </summary>
/// <returns>Level Enumerator.</returns>
internal static IEnumerable<double[]> ProvideLevelAbcissas()
{
for (int i = 0; i < NumberOfMaximumLevels; i++)
{
yield return EvaluateAbcissas(i);
}
}
/// <summary>
/// Weight vector per level provider.
/// </summary>
/// <returns>Level Enumerator.</returns>
internal static IEnumerable<double[]> ProvideLevelWeights()
{
for (int i = 0; i < NumberOfMaximumLevels; i++)
{
yield return EvaluateWeights(i);
}
}
/// <summary>
/// Compute the abscissa vector for a single level.
/// </summary>
/// <param name="level">The level to evaluate the abscissa vector for.</param>
/// <returns>Abscissa Vector.</returns>
static double[] EvaluateAbcissas(int level)
{
if (level < PrecomputedAbscissas.Length)
{
return PrecomputedAbscissas[level];
}
double step = level <= 1 ? 1.0 : (1.0/(2 << (level - 2)));
double offset = level == 0 ? 0.0 : (1.0/(2 << (level - 1)));
int length = level == 0 ? 4 : (3 << (level - 1));
double t = 0;
double[] abcissas = new double[length];
for (int i = 0; i < abcissas.Length; i++)
{
double arg = offset + t;
t += step;
abcissas[i] = Math.Tanh(Constants.PiOver2*Math.Sinh(arg));
}
return abcissas;
}
/// <summary>
/// Compute the weight vector for a single level.
/// </summary>
/// <param name="level">The level to evaluate the weight vector for.</param>
/// <returns>Weight Vector.</returns>
static double[] EvaluateWeights(int level)
{
if (level < PrecomputedWeights.Length)
{
return PrecomputedWeights[level];
}
double step = level <= 1 ? 1.0 : (1.0/(2 << (level - 2)));
double offset = level == 0 ? 0.0 : (1.0/(2 << (level - 1)));
int length = level == 0 ? 4 : (3 << (level - 1));
double t = 0;
double[] weights = new double[length];
for (int i = 0; i < weights.Length; i++)
{
double arg = offset + t;
t += step;
// TODO: reuse abcissas as computed in EvaluateAbcissas
double abcissa = Math.Tanh(Constants.PiOver2*Math.Sinh(arg));
weights[i] = Constants.PiOver2*(1 - (abcissa*abcissa))*Math.Cosh(arg);
}
return weights;
}
#region Precomputed Abcissas and Weights #region Precomputed Abcissas and Weights
/// <summary> /// <summary>
@ -486,135 +609,5 @@ namespace MathNet.Numerics.Integration
}; };
#endregion #endregion
/// <summary>
/// Maximum number of iterations, until the asked
/// maximum error is (likely to be) satisfied.
/// </summary>
const int NumberOfMaximumLevels = 10;
/// <summary>
/// Abscissa vector per level provider.
/// </summary>
IEnumerable<double[]> _levelAbcissas;
/// <summary>
/// Weight vector per level provider.
/// </summary>
IEnumerable<double[]> _levelWeights;
/// <summary>
/// Approximate the integral by the double exponential transformation
/// </summary>
/// <param name="f">The analytic smooth function to integrate.</param>
/// <param name="intervalBegin">Where the interval starts, inclusive and finite.</param>
/// <param name="intervalEnd">Where the interval stops, inclusive and finite.</param>
/// <param name="targetRelativeError">The expected relative accuracy of the approximation.</param>
/// <returns>Approximation of the finite integral in the given interval.</returns>
public double Integrate(
Func<double, double> f,
double intervalBegin,
double intervalEnd,
double targetRelativeError)
{
if (_levelAbcissas == null)
{
_levelAbcissas = ProvideLevelAbcissas();
_levelWeights = ProvideLevelWeights();
}
return NewtonCotesTrapeziumRule.IntegrateAdaptiveTransformedOdd(
f,
intervalBegin,
intervalEnd,
_levelAbcissas,
_levelWeights,
1,
targetRelativeError);
}
/// <summary>
/// Abscissa vector per level provider.
/// </summary>
/// <returns>Level Enumerator.</returns>
internal static IEnumerable<double[]> ProvideLevelAbcissas()
{
for (int i = 0; i < NumberOfMaximumLevels; i++)
{
yield return EvaluateAbcissas(i);
}
}
/// <summary>
/// Weight vector per level provider.
/// </summary>
/// <returns>Level Enumerator.</returns>
internal static IEnumerable<double[]> ProvideLevelWeights()
{
for (int i = 0; i < NumberOfMaximumLevels; i++)
{
yield return EvaluateWeights(i);
}
}
/// <summary>
/// Compute the abscissa vector for a single level.
/// </summary>
/// <param name="level">The level to evaluate the abscissa vector for.</param>
/// <returns>Abscissa Vector.</returns>
static double[] EvaluateAbcissas(int level)
{
if (level < PrecomputedAbscissas.Length)
{
return PrecomputedAbscissas[level];
}
double step = level <= 1 ? 1.0 : (1.0/(2 << (level - 2)));
double offset = level == 0 ? 0.0 : (1.0/(2 << (level - 1)));
int length = level == 0 ? 4 : (3 << (level - 1));
double t = 0;
double[] abcissas = new double[length];
for (int i = 0; i < abcissas.Length; i++)
{
double arg = offset + t;
t += step;
abcissas[i] = Math.Tanh(Constants.PiOver2*Math.Sinh(arg));
}
return abcissas;
}
/// <summary>
/// Compute the weight vector for a single level.
/// </summary>
/// <param name="level">The level to evaluate the weight vector for.</param>
/// <returns>Weight Vector.</returns>
static double[] EvaluateWeights(int level)
{
if (level < PrecomputedWeights.Length)
{
return PrecomputedWeights[level];
}
double step = level <= 1 ? 1.0 : (1.0/(2 << (level - 2)));
double offset = level == 0 ? 0.0 : (1.0/(2 << (level - 1)));
int length = level == 0 ? 4 : (3 << (level - 1));
double t = 0;
double[] weights = new double[length];
for (int i = 0; i < weights.Length; i++)
{
double arg = offset + t;
t += step;
// TODO: reuse abcissas as computed in EvaluateAbcissas
double abcissa = Math.Tanh(Constants.PiOver2*Math.Sinh(arg));
weights[i] = Constants.PiOver2*(1 - (abcissa*abcissa))*Math.Cosh(arg);
}
return weights;
}
} }
} }

28
src/Numerics/Integration/NewtonCotesTrapeziumRule.cs

@ -4,7 +4,7 @@
// http://github.com/mathnet/mathnet-numerics // http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com // http://mathnetnumerics.codeplex.com
// //
// Copyright (c) 2009-2010 Math.NET // Copyright (c) 2009-2013 Math.NET
// //
// Permission is hereby granted, free of charge, to any person // Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation // obtaining a copy of this software and associated documentation
@ -49,10 +49,7 @@ namespace MathNet.Numerics.Integration
/// <param name="intervalBegin">Where the interval starts, inclusive and finite.</param> /// <param name="intervalBegin">Where the interval starts, inclusive and finite.</param>
/// <param name="intervalEnd">Where the interval stops, inclusive and finite.</param> /// <param name="intervalEnd">Where the interval stops, inclusive and finite.</param>
/// <returns>Approximation of the finite integral in the given interval.</returns> /// <returns>Approximation of the finite integral in the given interval.</returns>
public static double IntegrateTwoPoint( public static double IntegrateTwoPoint(Func<double, double> f, double intervalBegin, double intervalEnd)
Func<double, double> f,
double intervalBegin,
double intervalEnd)
{ {
if (f == null) if (f == null)
{ {
@ -70,11 +67,7 @@ namespace MathNet.Numerics.Integration
/// <param name="intervalEnd">Where the interval stops, inclusive and finite.</param> /// <param name="intervalEnd">Where the interval stops, inclusive and finite.</param>
/// <param name="numberOfPartitions">Number of composite subdivision partitions.</param> /// <param name="numberOfPartitions">Number of composite subdivision partitions.</param>
/// <returns>Approximation of the finite integral in the given interval.</returns> /// <returns>Approximation of the finite integral in the given interval.</returns>
public static double IntegrateComposite( public static double IntegrateComposite(Func<double, double> f, double intervalBegin, double intervalEnd, int numberOfPartitions)
Func<double, double> f,
double intervalBegin,
double intervalEnd,
int numberOfPartitions)
{ {
if (f == null) if (f == null)
{ {
@ -108,11 +101,7 @@ namespace MathNet.Numerics.Integration
/// <param name="intervalEnd">Where the interval stops, inclusive and finite.</param> /// <param name="intervalEnd">Where the interval stops, inclusive and finite.</param>
/// <param name="targetError">The expected accuracy of the approximation.</param> /// <param name="targetError">The expected accuracy of the approximation.</param>
/// <returns>Approximation of the finite integral in the given interval.</returns> /// <returns>Approximation of the finite integral in the given interval.</returns>
public static double IntegrateAdaptive( public static double IntegrateAdaptive(Func<double, double> f, double intervalBegin, double intervalEnd, double targetError)
Func<double, double> f,
double intervalBegin,
double intervalEnd,
double targetError)
{ {
if (f == null) if (f == null)
{ {
@ -157,12 +146,9 @@ namespace MathNet.Numerics.Integration
/// <returns>Approximation of the finite integral in the given interval.</returns> /// <returns>Approximation of the finite integral in the given interval.</returns>
public static double IntegrateAdaptiveTransformedOdd( public static double IntegrateAdaptiveTransformedOdd(
Func<double, double> f, Func<double, double> f,
double intervalBegin, double intervalBegin, double intervalEnd,
double intervalEnd, IEnumerable<double[]> levelAbscissas, IEnumerable<double[]> levelWeights,
IEnumerable<double[]> levelAbscissas, double levelOneStep, double targetRelativeError)
IEnumerable<double[]> levelWeights,
double levelOneStep,
double targetRelativeError)
{ {
if (f == null) if (f == null)
{ {

13
src/Numerics/Integration/SimpsonRule.cs

@ -4,7 +4,7 @@
// http://github.com/mathnet/mathnet-numerics // http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com // http://mathnetnumerics.codeplex.com
// //
// Copyright (c) 2009-2010 Math.NET // Copyright (c) 2009-2013 Math.NET
// //
// Permission is hereby granted, free of charge, to any person // Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation // obtaining a copy of this software and associated documentation
@ -46,10 +46,7 @@ namespace MathNet.Numerics.Integration
/// <param name="intervalBegin">Where the interval starts, inclusive and finite.</param> /// <param name="intervalBegin">Where the interval starts, inclusive and finite.</param>
/// <param name="intervalEnd">Where the interval stops, inclusive and finite.</param> /// <param name="intervalEnd">Where the interval stops, inclusive and finite.</param>
/// <returns>Approximation of the finite integral in the given interval.</returns> /// <returns>Approximation of the finite integral in the given interval.</returns>
public static double IntegrateThreePoint( public static double IntegrateThreePoint(Func<double, double> f, double intervalBegin, double intervalEnd)
Func<double, double> f,
double intervalBegin,
double intervalEnd)
{ {
if (f == null) if (f == null)
{ {
@ -68,11 +65,7 @@ namespace MathNet.Numerics.Integration
/// <param name="intervalEnd">Where the interval stops, inclusive and finite.</param> /// <param name="intervalEnd">Where the interval stops, inclusive and finite.</param>
/// <param name="numberOfPartitions">Even number of composite subdivision partitions.</param> /// <param name="numberOfPartitions">Even number of composite subdivision partitions.</param>
/// <returns>Approximation of the finite integral in the given interval.</returns> /// <returns>Approximation of the finite integral in the given interval.</returns>
public static double IntegrateComposite( public static double IntegrateComposite(Func<double, double> f, double intervalBegin, double intervalEnd, int numberOfPartitions)
Func<double, double> f,
double intervalBegin,
double intervalEnd,
int numberOfPartitions)
{ {
if (f == null) if (f == null)
{ {

16
src/Numerics/Interpolate.cs

@ -48,9 +48,7 @@ namespace MathNet.Numerics
/// which can then be used to compute interpolations and extrapolations /// which can then be used to compute interpolations and extrapolations
/// on arbitrary points. /// on arbitrary points.
/// </returns> /// </returns>
public static IInterpolation Common( public static IInterpolation Common(IList<double> points, IList<double> values)
IList<double> points,
IList<double> values)
{ {
return RationalWithoutPoles(points, values); return RationalWithoutPoles(points, values);
} }
@ -65,9 +63,7 @@ namespace MathNet.Numerics
/// which can then be used to compute interpolations and extrapolations /// which can then be used to compute interpolations and extrapolations
/// on arbitrary points. /// on arbitrary points.
/// </returns> /// </returns>
public static IInterpolation LinearBetweenPoints( public static IInterpolation LinearBetweenPoints(IList<double> points, IList<double> values)
IList<double> points,
IList<double> values)
{ {
var method = new LinearSplineInterpolation(); var method = new LinearSplineInterpolation();
method.Initialize(points, values); method.Initialize(points, values);
@ -84,9 +80,7 @@ namespace MathNet.Numerics
/// which can then be used to compute interpolations and extrapolations /// which can then be used to compute interpolations and extrapolations
/// on arbitrary points. /// on arbitrary points.
/// </returns> /// </returns>
public static IInterpolation RationalWithoutPoles( public static IInterpolation RationalWithoutPoles(IList<double> points, IList<double> values)
IList<double> points,
IList<double> values)
{ {
var method = new FloaterHormannRationalInterpolation(); var method = new FloaterHormannRationalInterpolation();
method.Initialize(points, values); method.Initialize(points, values);
@ -103,9 +97,7 @@ namespace MathNet.Numerics
/// which can then be used to compute interpolations and extrapolations /// which can then be used to compute interpolations and extrapolations
/// on arbitrary points. /// on arbitrary points.
/// </returns> /// </returns>
public static IInterpolation RationalWithPoles( public static IInterpolation RationalWithPoles(IList<double> points, IList<double> values)
IList<double> points,
IList<double> values)
{ {
var method = new BulirschStoerRationalInterpolation(); var method = new BulirschStoerRationalInterpolation();
method.Initialize(points, values); method.Initialize(points, values);

Loading…
Cancel
Save