Browse Source

interpolation: ported bulirsch-stoer rational algorithm

Signed-off-by: Christoph Ruegg <git@cdrnet.ch>
pull/36/head
Christoph Ruegg 17 years ago
parent
commit
6d3db3c0d3
  1. 7
      src/Managed.UnitTests/InterpolationTests/InterpolationContract.cs
  2. 13
      src/Managed.UnitTests/InterpolationTests/InterpolationTest.cs
  3. 222
      src/Managed/Interpolation/Algorithms/BulirschStoerRationalInterpolation.cs
  4. 2
      src/Managed/Interpolation/Algorithms/FloaterHormannRationalInterpolation.cs
  5. 2
      src/Managed/Interpolation/Algorithms/NevillePolynomialInterpolation.cs
  6. 19
      src/Managed/Interpolation/Interpolate.cs
  7. 1
      src/Managed/Managed.csproj
  8. 3
      src/Native/Native.csproj

7
src/Managed.UnitTests/InterpolationTests/InterpolationContract.cs

@ -40,6 +40,7 @@ namespace MathNet.Numerics.UnitTests.InterpolationTests
{
public Func<IList<double>, IList<double>, IInterpolation> Factory { get; set; }
public int[] Order { get; set; }
public bool LinearBehavior { get; set; }
public bool PolynomialBehavior { get; set; }
public bool RationalBehavior { get; set; }
@ -56,7 +57,11 @@ namespace MathNet.Numerics.UnitTests.InterpolationTests
yield return CreateConstructorInitShortcutTest("ConstructorInitShortcut");
yield return CreateConsistentCapabilityBehaviorTest("ConsistentCapabilityBehavior");
yield return CreateInterpolationMatchesNodePointsTest("InterpolationMatchesNodePoints");
yield return CreateLinearBehaviorTest("LinearBehavior");
if (LinearBehavior)
{
yield return CreateLinearBehaviorTest("LinearBehavior");
}
if (PolynomialBehavior)
{

13
src/Managed.UnitTests/InterpolationTests/InterpolationTest.cs

@ -42,6 +42,7 @@ namespace MathNet.Numerics.UnitTests.InterpolationTests
{
Factory = Interpolate.LinearBetweenPoints,
Order = new[] { 2, 3, 6 },
LinearBehavior = true,
PolynomialBehavior = false,
RationalBehavior = false
};
@ -51,6 +52,7 @@ namespace MathNet.Numerics.UnitTests.InterpolationTests
{
Factory = Interpolate.RationalWithoutPoles,
Order = new[] { 1, 2, 6 },
LinearBehavior = true,
PolynomialBehavior = true,
RationalBehavior = true
};
@ -60,8 +62,19 @@ namespace MathNet.Numerics.UnitTests.InterpolationTests
{
Factory = (t, x) => new NevillePolynomialInterpolation(t, x),
Order = new[] { 1, 2, 6 },
LinearBehavior = true,
PolynomialBehavior = true,
RationalBehavior = false
};
[VerifyContract]
public readonly IContract BulirschStoerRationalContractTests = new InterpolationContract<BulirschStoerRationalInterpolation>()
{
Factory = Interpolate.RationalWithPoles,
Order = new[] { 1, 2, 6 },
LinearBehavior = false,
PolynomialBehavior = true,
RationalBehavior = true
};
}
}

222
src/Managed/Interpolation/Algorithms/BulirschStoerRationalInterpolation.cs

@ -0,0 +1,222 @@
// <copyright file="BulirschStoerRationalInterpolation.cs" company="Math.NET">
// Math.NET Numerics, part of the Math.NET Project
// http://mathnet.opensourcedotnet.info
//
// Copyright (c) 2009 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.
// </copyright>
namespace MathNet.Numerics.Interpolation.Algorithms
{
using System;
using System.Collections.Generic;
/// <summary>
/// Rational Interpolation (with poles) using Roland Bulirsch and Josef Stoer's Algorithm.
/// </summary>
/// <remarks>
/// <para>
/// This algorithm supports neither differentiation nor integration.
/// </para>
/// </remarks>
public class BulirschStoerRationalInterpolation : IInterpolation
{
/// <summary>
/// Sample Points t.
/// </summary>
private IList<double> _points;
/// <summary>
/// Spline Values x(t).
/// </summary>
private IList<double> _values;
/// <summary>
/// Initializes a new instance of the BulirschStoerRationalInterpolation class.
/// </summary>
public BulirschStoerRationalInterpolation()
{
}
/// <summary>
/// Initializes a new instance of the BulirschStoerRationalInterpolation class.
/// </summary>
/// <param name="samplePoints">Sample Points t</param>
/// <param name="sampleValues">Sample Values x(t)</param>
public BulirschStoerRationalInterpolation(
IList<double> samplePoints,
IList<double> sampleValues)
{
Initialize(samplePoints, sampleValues);
}
/// <summary>
/// Gets a value indicating whether the algorithm supports differentiation (interpolated derivative).
/// </summary>
/// <seealso cref="Differentiate(double)"/>
/// <seealso cref="Differentiate(double, out double, out double)"/>
bool IInterpolation.SupportsDifferentiation
{
get { return false; }
}
/// <summary>
/// Gets a value indicating whether the algorithm supports integration (interpolated quadrature).
/// </summary>
/// <seealso cref="Integrate"/>
bool IInterpolation.SupportsIntegration
{
get { return false; }
}
/// <summary>
/// Initialize the interpolation method with the given sample pairs.
/// </summary>
/// <param name="samplePoints">Sample Points t</param>
/// <param name="sampleValues">Sample Values x(t)</param>
public void Initialize(
IList<double> samplePoints,
IList<double> sampleValues)
{
if (null == samplePoints)
{
throw new ArgumentNullException("samplePoints");
}
if (null == sampleValues)
{
throw new ArgumentNullException("sampleValues");
}
if (samplePoints.Count != sampleValues.Count)
{
throw new ArgumentException(Properties.Resources.ArgumentVectorsSameLengths);
}
_points = samplePoints;
_values = sampleValues;
}
/// <summary>
/// Interpolate at point t.
/// </summary>
/// <param name="t">Point t to interpolate at.</param>
/// <returns>Interpolated value x(t).</returns>
public double Interpolate(double t)
{
const double Tiny = 1.0e-25;
int n = _points.Count;
double[] c = new double[n];
double[] d = new double[n];
int nearestIndex = 0;
double nearestDistance = Math.Abs(t - _points[0]);
for (int i = 0; i < n; i++)
{
double distance = Math.Abs(t - _points[i]);
if (distance.AlmostZero())
{
return _values[i];
}
if (distance < nearestDistance)
{
nearestIndex = i;
nearestDistance = distance;
}
c[i] = _values[i];
d[i] = _values[i] + Tiny;
}
double x = _values[nearestIndex];
for (int level = 1; level < n; level++)
{
for (int i = 0; i < n - level; i++)
{
double hp = _points[i + level] - t;
double ho = (_points[i] - t) * d[i] / hp;
double den = ho - c[i + 1];
if (den.AlmostZero())
{
return double.NaN; // zero-div, singularity
}
den = (c[i + 1] - d[i]) / den;
d[i] = c[i + 1] * den;
c[i] = ho * den;
}
x += (2 * nearestIndex) < (n - level)
? c[nearestIndex]
: d[--nearestIndex];
}
return x;
}
/// <summary>
/// Differentiate at point t.
/// </summary>
/// <param name="t">Point t to interpolate at.</param>
/// <returns>Interpolated first derivative at point t.</returns>
/// <seealso cref="IInterpolation.SupportsDifferentiation"/>
/// <seealso cref="Differentiate(double, out double, out double)"/>
double IInterpolation.Differentiate(double t)
{
throw new NotSupportedException();
}
/// <summary>
/// Differentiate at point t.
/// </summary>
/// <param name="t">Point t to interpolate at.</param>
/// <param name="interpolatedValue">Interpolated value x(t)</param>
/// <param name="secondDerivative">Interpolated second derivative at point t.</param>
/// <returns>Interpolated first derivative at point t.</returns>
/// <seealso cref="IInterpolation.SupportsDifferentiation"/>
/// <seealso cref="Differentiate(double)"/>
double IInterpolation.Differentiate(
double t,
out double interpolatedValue,
out double secondDerivative)
{
throw new NotSupportedException();
}
/// <summary>
/// Integrate up to point t.
/// </summary>
/// <param name="t">Right bound of the integration interval [a,t].</param>
/// <returns>Interpolated definite integral over the interval [a,t].</returns>
/// <seealso cref="IInterpolation.SupportsIntegration"/>
double IInterpolation.Integrate(double t)
{
throw new NotSupportedException();
}
}
}

2
src/Managed/Interpolation/Algorithms/FloaterHormannRationalInterpolation.cs

@ -32,7 +32,7 @@ namespace MathNet.Numerics.Interpolation.Algorithms
using System.Collections.Generic;
/// <summary>
/// Barycentric Rational Interpolation without poles, using Floater and Hormann's Algorithm.
/// Barycentric Rational Interpolation without poles, using Mike Floater and Kai Hormann's Algorithm.
/// </summary>
/// <remarks>
/// This algorithm neither supports differentiation nor integration.

2
src/Managed/Interpolation/Algorithms/NevillePolynomialInterpolation.cs

@ -95,7 +95,7 @@ namespace MathNet.Numerics.Interpolation.Algorithms
}
/// <summary>
/// Initialize the interpolation method with the given spline coefficients.
/// Initialize the interpolation method with the given sample pairs.
/// </summary>
/// <param name="samplePoints">Sample Points t</param>
/// <param name="sampleValues">Sample Values x(t)</param>

19
src/Managed/Interpolation/Interpolate.cs

@ -90,5 +90,24 @@ namespace MathNet.Numerics.Interpolation
method.Initialize(points, values);
return method;
}
/// <summary>
/// Create a burlish stoer rational interpolation based on arbitrary points.
/// </summary>
/// <param name="points">The sample points t. Supports both lists and arrays.</param>
/// <param name="values">The sample point values x(t). Supports both lists and arrays.</param>
/// <returns>
/// An interpolation scheme optimized for the given sample points and values,
/// which can then be used to compute interpolations and extrapolations
/// on arbitrary points.
/// </returns>
public static IInterpolation RationalWithPoles(
IList<double> points,
IList<double> values)
{
BulirschStoerRationalInterpolation method = new BulirschStoerRationalInterpolation();
method.Initialize(points, values);
return method;
}
}
}

1
src/Managed/Managed.csproj

@ -55,6 +55,7 @@
<Compile Include="Distributions\IDiscreteDistribution.cs" />
<Compile Include="Distributions\IDistribution.cs" />
<Compile Include="Interpolation\Algorithms\BarycentricInterpolation.cs" />
<Compile Include="Interpolation\Algorithms\BulirschStoerRationalInterpolation.cs" />
<Compile Include="Interpolation\Algorithms\FloaterHormannRationalInterpolation.cs" />
<Compile Include="Interpolation\Algorithms\LinearSplineInterpolation.cs" />
<Compile Include="Interpolation\Algorithms\NevillePolynomialInterpolation.cs" />

3
src/Native/Native.csproj

@ -74,6 +74,9 @@
<Compile Include="..\Managed\Interpolation\Algorithms\BarycentricInterpolation.cs">
<Link>Interpolation\Algorithms\BarycentricInterpolation.cs</Link>
</Compile>
<Compile Include="..\Managed\Interpolation\Algorithms\BulirschStoerRationalInterpolation.cs">
<Link>Interpolation\Algorithms\BulirschStoerRationalInterpolation.cs</Link>
</Compile>
<Compile Include="..\Managed\Interpolation\Algorithms\FloaterHormannRationalInterpolation.cs">
<Link>Interpolation\Algorithms\FloaterHormannRationalInterpolation.cs</Link>
</Compile>

Loading…
Cancel
Save