Browse Source

Interpolation: Differentiate2 instead of DifferentiateAll; definite integral; cleanup

pull/184/head
Christoph Ruegg 12 years ago
parent
commit
aebb93a04a
  1. 41
      src/Numerics/Interpolation/AkimaSplineInterpolation.cs
  2. 31
      src/Numerics/Interpolation/BarycentricInterpolation.cs
  3. 31
      src/Numerics/Interpolation/BulirschStoerRationalInterpolation.cs
  4. 33
      src/Numerics/Interpolation/CubicHermiteSplineInterpolation.cs
  5. 33
      src/Numerics/Interpolation/CubicSplineInterpolation.cs
  6. 31
      src/Numerics/Interpolation/EquidistantPolynomialInterpolation.cs
  7. 31
      src/Numerics/Interpolation/FloaterHormannRationalInterpolation.cs
  8. 30
      src/Numerics/Interpolation/IInterpolation.cs
  9. 32
      src/Numerics/Interpolation/LinearSpline.cs
  10. 39
      src/Numerics/Interpolation/NevillePolynomialInterpolation.cs
  11. 70
      src/Numerics/Interpolation/SplineInterpolation.cs
  12. 16
      src/UnitTests/InterpolationTests/AkimaSplineTest.cs
  13. 12
      src/UnitTests/InterpolationTests/BulirschStoerRationalTest.cs
  14. 41
      src/UnitTests/InterpolationTests/CubicSplineTest.cs
  15. 14
      src/UnitTests/InterpolationTests/EquidistantPolynomialTest.cs
  16. 18
      src/UnitTests/InterpolationTests/FloaterHormannRationalTest.cs
  17. 8
      src/UnitTests/InterpolationTests/LinearInterpolationCase.cs
  18. 70
      src/UnitTests/InterpolationTests/LinearSplineTest.cs
  19. 28
      src/UnitTests/InterpolationTests/NevillePolynomialTest.cs

41
src/Numerics/Interpolation/AkimaSplineInterpolation.cs

@ -4,7 +4,7 @@
// http://github.com/mathnet/mathnet-numerics
// 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
// obtaining a copy of this software and associated documentation
@ -69,8 +69,6 @@ namespace MathNet.Numerics.Interpolation
/// <summary>
/// Gets a value indicating whether the algorithm supports differentiation (interpolated derivative).
/// </summary>
/// <seealso cref="Differentiate(double)"/>
/// <seealso cref="DifferentiateAll(double)"/>
bool IInterpolation.SupportsDifferentiation
{
get { return true; }
@ -79,7 +77,6 @@ namespace MathNet.Numerics.Interpolation
/// <summary>
/// Gets a value indicating whether the algorithm supports integration (interpolated quadrature).
/// </summary>
/// <seealso cref="Integrate"/>
bool IInterpolation.SupportsIntegration
{
get { return true; }
@ -155,11 +152,11 @@ namespace MathNet.Numerics.Interpolation
derivatives[i] =
weights[i - 1].AlmostEqual(0.0) && weights[i + 1].AlmostEqual(0.0)
? (((samplePoints[i + 1] - samplePoints[i])*differences[i - 1])
+ ((samplePoints[i] - samplePoints[i - 1])*differences[i]))
/(samplePoints[i + 1] - samplePoints[i - 1])
+ ((samplePoints[i] - samplePoints[i - 1])*differences[i]))
/(samplePoints[i + 1] - samplePoints[i - 1])
: ((weights[i + 1]*differences[i - 1])
+ (weights[i - 1]*differences[i]))
/(weights[i + 1] + weights[i - 1]);
+ (weights[i - 1]*differences[i]))
/(weights[i + 1] + weights[i - 1]);
}
derivatives[0] = DifferentiateThreePoint(samplePoints, sampleValues, 0, 0, 1, 2);
@ -227,34 +224,38 @@ namespace MathNet.Numerics.Interpolation
/// </summary>
/// <param name="t">Point t to interpolate at.</param>
/// <returns>Interpolated first derivative at point t.</returns>
/// <seealso cref="IInterpolation.SupportsDifferentiation"/>
/// <seealso cref="DifferentiateAll(double)"/>
public double Differentiate(double t)
{
return _spline.Differentiate(t);
}
/// <summary>
/// Interpolate, differentiate and 2nd differentiate at point t.
/// Differentiate twice 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)"/>
public Tuple<double, double, double> DifferentiateAll(double t)
/// <returns>Interpolated second derivative at point t.</returns>
public double Differentiate2(double t)
{
return _spline.DifferentiateAll(t);
return _spline.Differentiate2(t);
}
/// <summary>
/// Integrate up to point t.
/// Indefinite integral at 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"/>
/// <param name="t">Point t to integrate at.</param>
public double Integrate(double t)
{
return _spline.Integrate(t);
}
/// <summary>
/// Definite integral between points a and b.
/// </summary>
/// <param name="a">Left bound of the integration interval [a,b].</param>
/// <param name="b">Right bound of the integration interval [a,b].</param>
public double Integrate(double a, double b)
{
return _spline.Integrate(a, b);
}
}
}

31
src/Numerics/Interpolation/BarycentricInterpolation.cs

@ -4,7 +4,7 @@
// http://github.com/mathnet/mathnet-numerics
// 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
// obtaining a copy of this software and associated documentation
@ -77,8 +77,6 @@ namespace MathNet.Numerics.Interpolation
/// <summary>
/// Gets a value indicating whether the algorithm supports differentiation (interpolated derivative).
/// </summary>
/// <seealso cref="IInterpolation.Differentiate(double)"/>
/// <seealso cref="IInterpolation.DifferentiateAll(double)"/>
bool IInterpolation.SupportsDifferentiation
{
get { return false; }
@ -87,7 +85,6 @@ namespace MathNet.Numerics.Interpolation
/// <summary>
/// Gets a value indicating whether the algorithm supports integration (interpolated quadrature).
/// </summary>
/// <seealso cref="IInterpolation.Integrate"/>
bool IInterpolation.SupportsIntegration
{
get { return false; }
@ -201,34 +198,38 @@ namespace MathNet.Numerics.Interpolation
/// </summary>
/// <param name="t">Point t to interpolate at.</param>
/// <returns>Interpolated first derivative at point t.</returns>
/// <seealso cref="IInterpolation.SupportsDifferentiation"/>
/// <seealso cref="IInterpolation.DifferentiateAll(double)"/>
double IInterpolation.Differentiate(double t)
{
throw new NotSupportedException();
}
/// <summary>
/// Interpolate, differentiate and 2nd differentiate at point t. NOT SUPPORTED.
/// Differentiate twice at point t. NOT SUPPORTED.
/// </summary>
/// <param name="t">Point t to interpolate at.</param>
/// <returns>Interpolated first derivative at point t.</returns>
/// <seealso cref="IInterpolation.SupportsDifferentiation"/>
/// <seealso cref="IInterpolation.Differentiate(double)"/>
Tuple<double, double, double> IInterpolation.DifferentiateAll(double t)
/// <returns>Interpolated second derivative at point t.</returns>
double IInterpolation.Differentiate2(double t)
{
throw new NotSupportedException();
}
/// <summary>
/// Integrate up to point t. NOT SUPPORTED.
/// Indefinite integral at point t. NOT SUPPORTED.
/// </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"/>
/// <param name="t">Point t to integrate at.</param>
double IInterpolation.Integrate(double t)
{
throw new NotSupportedException();
}
/// <summary>
/// Definite integral between points a and b. NOT SUPPORTED.
/// </summary>
/// <param name="a">Left bound of the integration interval [a,b].</param>
/// <param name="b">Right bound of the integration interval [a,b].</param>
double IInterpolation.Integrate(double a, double b)
{
throw new NotSupportedException();
}
}
}

31
src/Numerics/Interpolation/BulirschStoerRationalInterpolation.cs

@ -4,7 +4,7 @@
// http://github.com/mathnet/mathnet-numerics
// 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
// obtaining a copy of this software and associated documentation
@ -73,8 +73,6 @@ namespace MathNet.Numerics.Interpolation
/// <summary>
/// Gets a value indicating whether the algorithm supports differentiation (interpolated derivative).
/// </summary>
/// <seealso cref="IInterpolation.Differentiate(double)"/>
/// <seealso cref="IInterpolation.DifferentiateAll(double)"/>
bool IInterpolation.SupportsDifferentiation
{
get { return false; }
@ -83,7 +81,6 @@ namespace MathNet.Numerics.Interpolation
/// <summary>
/// Gets a value indicating whether the algorithm supports integration (interpolated quadrature).
/// </summary>
/// <seealso cref="IInterpolation.Integrate"/>
bool IInterpolation.SupportsIntegration
{
get { return false; }
@ -187,34 +184,38 @@ namespace MathNet.Numerics.Interpolation
/// </summary>
/// <param name="t">Point t to interpolate at.</param>
/// <returns>Interpolated first derivative at point t.</returns>
/// <seealso cref="IInterpolation.SupportsDifferentiation"/>
/// <seealso cref="IInterpolation.DifferentiateAll(double)"/>
double IInterpolation.Differentiate(double t)
{
throw new NotSupportedException();
}
/// <summary>
/// Interpolate, differentiate and 2nd differentiate at point t. NOT SUPPORTED.
/// Differentiate twice at point t. NOT SUPPORTED.
/// </summary>
/// <param name="t">Point t to interpolate at.</param>
/// <returns>Interpolated first derivative at point t.</returns>
/// <seealso cref="IInterpolation.SupportsDifferentiation"/>
/// <seealso cref="IInterpolation.Differentiate(double)"/>
Tuple<double, double, double> IInterpolation.DifferentiateAll(double t)
/// <returns>Interpolated second derivative at point t.</returns>
double IInterpolation.Differentiate2(double t)
{
throw new NotSupportedException();
}
/// <summary>
/// Integrate up to point t. NOT SUPPORTED.
/// Indefinite integral at point t. NOT SUPPORTED.
/// </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"/>
/// <param name="t">Point t to integrate at.</param>
double IInterpolation.Integrate(double t)
{
throw new NotSupportedException();
}
/// <summary>
/// Definite integral between points a and b. NOT SUPPORTED.
/// </summary>
/// <param name="a">Left bound of the integration interval [a,b].</param>
/// <param name="b">Right bound of the integration interval [a,b].</param>
double IInterpolation.Integrate(double a, double b)
{
throw new NotSupportedException();
}
}
}

33
src/Numerics/Interpolation/CubicHermiteSplineInterpolation.cs

@ -4,7 +4,7 @@
// http://github.com/mathnet/mathnet-numerics
// 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
// obtaining a copy of this software and associated documentation
@ -70,8 +70,6 @@ namespace MathNet.Numerics.Interpolation
/// <summary>
/// Gets a value indicating whether the algorithm supports differentiation (interpolated derivative).
/// </summary>
/// <seealso cref="Differentiate(double)"/>
/// <seealso cref="DifferentiateAll(double)"/>
bool IInterpolation.SupportsDifferentiation
{
get { return true; }
@ -80,7 +78,6 @@ namespace MathNet.Numerics.Interpolation
/// <summary>
/// Gets a value indicating whether the algorithm supports integration (interpolated quadrature).
/// </summary>
/// <seealso cref="Integrate"/>
bool IInterpolation.SupportsIntegration
{
get { return true; }
@ -169,34 +166,38 @@ namespace MathNet.Numerics.Interpolation
/// </summary>
/// <param name="t">Point t to interpolate at.</param>
/// <returns>Interpolated first derivative at point t.</returns>
/// <seealso cref="IInterpolation.SupportsDifferentiation"/>
/// <seealso cref="DifferentiateAll(double)"/>
public double Differentiate(double t)
{
return _spline.Differentiate(t);
}
/// <summary>
/// Interpolate, differentiate and 2nd differentiate at point t.
/// Differentiate twice 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)"/>
public Tuple<double, double, double> DifferentiateAll(double t)
/// <returns>Interpolated second derivative at point t.</returns>
public double Differentiate2(double t)
{
return _spline.DifferentiateAll(t);
return _spline.Differentiate2(t);
}
/// <summary>
/// Integrate up to point t.
/// Indefinite integral at 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"/>
/// <param name="t">Point t to integrate at.</param>
public double Integrate(double t)
{
return _spline.Integrate(t);
}
/// <summary>
/// Definite integral between points a and b.
/// </summary>
/// <param name="a">Left bound of the integration interval [a,b].</param>
/// <param name="b">Right bound of the integration interval [a,b].</param>
public double Integrate(double a, double b)
{
return _spline.Integrate(a, b);
}
}
}

33
src/Numerics/Interpolation/CubicSplineInterpolation.cs

@ -4,7 +4,7 @@
// http://github.com/mathnet/mathnet-numerics
// 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
// obtaining a copy of this software and associated documentation
@ -91,8 +91,6 @@ namespace MathNet.Numerics.Interpolation
/// <summary>
/// Gets a value indicating whether the algorithm supports differentiation (interpolated derivative).
/// </summary>
/// <seealso cref="Differentiate(double)"/>
/// <seealso cref="DifferentiateAll(double)"/>
bool IInterpolation.SupportsDifferentiation
{
get { return true; }
@ -101,7 +99,6 @@ namespace MathNet.Numerics.Interpolation
/// <summary>
/// Gets a value indicating whether the algorithm supports integration (interpolated quadrature).
/// </summary>
/// <seealso cref="Integrate"/>
bool IInterpolation.SupportsIntegration
{
get { return true; }
@ -339,34 +336,38 @@ namespace MathNet.Numerics.Interpolation
/// </summary>
/// <param name="t">Point t to interpolate at.</param>
/// <returns>Interpolated first derivative at point t.</returns>
/// <seealso cref="IInterpolation.SupportsDifferentiation"/>
/// <seealso cref="DifferentiateAll(double)"/>
public double Differentiate(double t)
{
return _spline.Differentiate(t);
}
/// <summary>
/// Interpolate, differentiate and 2nd differentiate at point t.
/// Differentiate twice 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)"/>
public Tuple<double, double, double> DifferentiateAll(double t)
/// <returns>Interpolated second derivative at point t.</returns>
public double Differentiate2(double t)
{
return _spline.DifferentiateAll(t);
return _spline.Differentiate2(t);
}
/// <summary>
/// Integrate up to point t.
/// Indefinite integral at 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"/>
/// <param name="t">Point t to integrate at.</param>
public double Integrate(double t)
{
return _spline.Integrate(t);
}
/// <summary>
/// Definite integral between points a and b.
/// </summary>
/// <param name="a">Left bound of the integration interval [a,b].</param>
/// <param name="b">Right bound of the integration interval [a,b].</param>
public double Integrate(double a, double b)
{
return _spline.Integrate(a, b);
}
}
}

31
src/Numerics/Interpolation/EquidistantPolynomialInterpolation.cs

@ -4,7 +4,7 @@
// http://github.com/mathnet/mathnet-numerics
// 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
// obtaining a copy of this software and associated documentation
@ -80,8 +80,6 @@ namespace MathNet.Numerics.Interpolation
/// <summary>
/// Gets a value indicating whether the algorithm supports differentiation (interpolated derivative).
/// </summary>
/// <seealso cref="IInterpolation.Differentiate(double)"/>
/// <seealso cref="IInterpolation.DifferentiateAll(double)"/>
bool IInterpolation.SupportsDifferentiation
{
get { return false; }
@ -90,7 +88,6 @@ namespace MathNet.Numerics.Interpolation
/// <summary>
/// Gets a value indicating whether the algorithm supports integration (interpolated quadrature).
/// </summary>
/// <seealso cref="IInterpolation.Integrate"/>
bool IInterpolation.SupportsIntegration
{
get { return false; }
@ -181,34 +178,38 @@ namespace MathNet.Numerics.Interpolation
/// </summary>
/// <param name="t">Point t to interpolate at.</param>
/// <returns>Interpolated first derivative at point t.</returns>
/// <seealso cref="IInterpolation.SupportsDifferentiation"/>
/// <seealso cref="IInterpolation.DifferentiateAll(double)"/>
double IInterpolation.Differentiate(double t)
{
throw new NotSupportedException();
}
/// <summary>
/// Interpolate, differentiate and 2nd differentiate at point t. NOT SUPPORTED.
/// Differentiate twice at point t. NOT SUPPORTED.
/// </summary>
/// <param name="t">Point t to interpolate at.</param>
/// <returns>Interpolated first derivative at point t.</returns>
/// <seealso cref="IInterpolation.SupportsDifferentiation"/>
/// <seealso cref="IInterpolation.Differentiate(double)"/>
Tuple<double, double, double> IInterpolation.DifferentiateAll(double t)
/// <returns>Interpolated second derivative at point t.</returns>
double IInterpolation.Differentiate2(double t)
{
throw new NotSupportedException();
}
/// <summary>
/// Integrate up to point t. NOT SUPPORTED.
/// Indefinite integral at point t. NOT SUPPORTED.
/// </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"/>
/// <param name="t">Point t to integrate at.</param>
double IInterpolation.Integrate(double t)
{
throw new NotSupportedException();
}
/// <summary>
/// Definite integral between points a and b. NOT SUPPORTED.
/// </summary>
/// <param name="a">Left bound of the integration interval [a,b].</param>
/// <param name="b">Right bound of the integration interval [a,b].</param>
double IInterpolation.Integrate(double a, double b)
{
throw new NotSupportedException();
}
}
}

31
src/Numerics/Interpolation/FloaterHormannRationalInterpolation.cs

@ -4,7 +4,7 @@
// http://github.com/mathnet/mathnet-numerics
// 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
// obtaining a copy of this software and associated documentation
@ -83,8 +83,6 @@ namespace MathNet.Numerics.Interpolation
/// <summary>
/// Gets a value indicating whether the algorithm supports differentiation (interpolated derivative).
/// </summary>
/// <seealso cref="IInterpolation.Differentiate(double)"/>
/// <seealso cref="IInterpolation.DifferentiateAll(double)"/>
bool IInterpolation.SupportsDifferentiation
{
get { return false; }
@ -93,7 +91,6 @@ namespace MathNet.Numerics.Interpolation
/// <summary>
/// Gets a value indicating whether the algorithm supports integration (interpolated quadrature).
/// </summary>
/// <seealso cref="IInterpolation.Integrate"/>
bool IInterpolation.SupportsIntegration
{
get { return false; }
@ -250,34 +247,38 @@ namespace MathNet.Numerics.Interpolation
/// </summary>
/// <param name="t">Point t to interpolate at.</param>
/// <returns>Interpolated first derivative at point t.</returns>
/// <seealso cref="IInterpolation.SupportsDifferentiation"/>
/// <seealso cref="IInterpolation.DifferentiateAll(double)"/>
double IInterpolation.Differentiate(double t)
{
throw new NotSupportedException();
}
/// <summary>
/// Interpolate, differentiate and 2nd differentiate at point t. NOT SUPPORTED.
/// Differentiate twice at point t. NOT SUPPORTED.
/// </summary>
/// <param name="t">Point t to interpolate at.</param>
/// <returns>Interpolated first derivative at point t.</returns>
/// <seealso cref="IInterpolation.SupportsDifferentiation"/>
/// <seealso cref="IInterpolation.Differentiate(double)"/>
Tuple<double, double, double> IInterpolation.DifferentiateAll(double t)
/// <returns>Interpolated second derivative at point t.</returns>
double IInterpolation.Differentiate2(double t)
{
throw new NotSupportedException();
}
/// <summary>
/// Integrate up to point t. NOT SUPPORTED.
/// Indefinite integral at point t. NOT SUPPORTED.
/// </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"/>
/// <param name="t">Point t to integrate at.</param>
double IInterpolation.Integrate(double t)
{
throw new NotSupportedException();
}
/// <summary>
/// Definite integral between points a and b. NOT SUPPORTED.
/// </summary>
/// <param name="a">Left bound of the integration interval [a,b].</param>
/// <param name="b">Right bound of the integration interval [a,b].</param>
double IInterpolation.Integrate(double a, double b)
{
throw new NotSupportedException();
}
}
}

30
src/Numerics/Interpolation/IInterpolation.cs

@ -4,7 +4,7 @@
// http://github.com/mathnet/mathnet-numerics
// 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
// obtaining a copy of this software and associated documentation
@ -28,8 +28,6 @@
// OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
using System;
namespace MathNet.Numerics.Interpolation
{
/// <summary>
@ -40,14 +38,11 @@ namespace MathNet.Numerics.Interpolation
/// <summary>
/// Gets a value indicating whether the algorithm supports differentiation (interpolated derivative).
/// </summary>
/// <seealso cref="Differentiate(double)"/>
/// <seealso cref="DifferentiateAll(double)"/>
bool SupportsDifferentiation { get; }
/// <summary>
/// Gets a value indicating whether the algorithm supports integration (interpolated quadrature).
/// </summary>
/// <seealso cref="Integrate"/>
bool SupportsIntegration { get; }
/// <summary>
@ -62,25 +57,26 @@ namespace MathNet.Numerics.Interpolation
/// </summary>
/// <param name="t">Point t to interpolate at.</param>
/// <returns>Interpolated first derivative at point t.</returns>
/// <seealso cref="SupportsDifferentiation"/>
/// <seealso cref="DifferentiateAll(double)"/>
double Differentiate(double t);
/// <summary>
/// Interpolate, differentiate and 2nd differentiate at point t.
/// Differentiate twice at point t.
/// </summary>
/// <param name="t">Point t to interpolate at.</param>
/// <returns>Interpolated first derivative at point t.</returns>
/// <seealso cref="SupportsDifferentiation"/>
/// <seealso cref="Differentiate(double)"/>
Tuple<double, double, double> DifferentiateAll(double t);
/// <returns>Interpolated second derivative at point t.</returns>
double Differentiate2(double t);
/// <summary>
/// Integrate up to point t.
/// Indefinite integral at 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="SupportsIntegration"/>
/// <param name="t">Point t to integrate at.</param>
double Integrate(double t);
/// <summary>
/// Definite integral between points a and b.
/// </summary>
/// <param name="a">Left bound of the integration interval [a,b].</param>
/// <param name="b">Right bound of the integration interval [a,b].</param>
double Integrate(double a, double b);
}
}

32
src/Numerics/Interpolation/LinearSpline.cs

@ -44,6 +44,7 @@ namespace MathNet.Numerics.Interpolation
readonly double[] _x;
readonly double[] _c0;
readonly double[] _c1;
readonly Lazy<double[]> _indefiniteIntegral;
/// <param name="x">Sample points (N+1), sorted ascending</param>
/// <param name="c0">Sample values (N or N+1) at the corresponding points; intercept, zero order coefficients</param>
@ -53,6 +54,7 @@ namespace MathNet.Numerics.Interpolation
_x = x;
_c0 = c0;
_c1 = c1;
_indefiniteIntegral = new Lazy<double[]>(ComputeIndefiniteIntegral);
}
/// <summary>
@ -132,27 +134,35 @@ namespace MathNet.Numerics.Interpolation
}
/// <summary>
/// Integrate up to point t.
/// Indefinite integral at 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>
/// <param name="t">Point t to integrate at.</param>
public double Integrate(double t)
{
int k = LeftBracketIndex(t);
var x = (t - _x[k]);
return x*(_c0[k] + x*0.5*_c1[k]);
return _indefiniteIntegral.Value[k] + x*(_c0[k] + x*_c1[k]/2);
}
/// <summary>
/// Interpolate, differentiate and 2nd differentiate at point t.
/// Definite integral between points a and b.
/// </summary>
/// <param name="t">Point t to interpolate at.</param>
/// <returns>Interpolated first derivative at point t.</returns>
public Tuple<double, double, double> DifferentiateAll(double t)
/// <param name="a">Left bound of the integration interval [a,b].</param>
/// <param name="b">Right bound of the integration interval [a,b].</param>
public double Integrate(double a, double b)
{
int k = LeftBracketIndex(t);
var x = (t - _x[k]);
return new Tuple<double, double, double>(_c0[k] + x*_c1[k], _c1[k], 0d);
return Integrate(b) - Integrate(a);
}
double[] ComputeIndefiniteIntegral()
{
var integral = new double[_c1.Length];
for (int i = 0; i < integral.Length - 1; i++)
{
double w = _x[i + 1] - _x[i];
integral[i + 1] = integral[i] + w*(_c0[i] + w*_c1[i]/2);
}
return integral;
}
/// <summary>

39
src/Numerics/Interpolation/NevillePolynomialInterpolation.cs

@ -4,7 +4,7 @@
// http://github.com/mathnet/mathnet-numerics
// 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
// obtaining a copy of this software and associated documentation
@ -79,8 +79,6 @@ namespace MathNet.Numerics.Interpolation
/// <summary>
/// Gets a value indicating whether the algorithm supports differentiation (interpolated derivative).
/// </summary>
/// <seealso cref="Differentiate(double)"/>
/// <seealso cref="DifferentiateAll(double)"/>
bool IInterpolation.SupportsDifferentiation
{
get { return true; }
@ -89,7 +87,6 @@ namespace MathNet.Numerics.Interpolation
/// <summary>
/// Gets a value indicating whether the algorithm supports integration (interpolated quadrature).
/// </summary>
/// <seealso cref="IInterpolation.Integrate"/>
bool IInterpolation.SupportsIntegration
{
get { return false; }
@ -159,8 +156,6 @@ namespace MathNet.Numerics.Interpolation
/// </summary>
/// <param name="t">Point t to interpolate at.</param>
/// <returns>Interpolated first derivative at point t.</returns>
/// <seealso cref="IInterpolation.SupportsDifferentiation"/>
/// <seealso cref="DifferentiateAll(double)"/>
public double Differentiate(double t)
{
var x = new double[_values.Count];
@ -183,13 +178,11 @@ namespace MathNet.Numerics.Interpolation
}
/// <summary>
/// Interpolate, differentiate and 2nd differentiate at point t.
/// Differentiate twice 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)"/>
public Tuple<double, double, double> DifferentiateAll(double t)
/// <returns>Interpolated second derivative at point t.</returns>
public double Differentiate2(double t)
{
var x = new double[_values.Count];
var dx = new double[_values.Count];
@ -203,24 +196,32 @@ namespace MathNet.Numerics.Interpolation
double hp = t - _points[i + level];
double ho = _points[i] - t;
double den = _points[i] - _points[i + level];
ddx[i] = ((hp * ddx[i]) + (ho * ddx[i + 1]) + (2 * dx[i]) - (2 * dx[i + 1])) / den;
dx[i] = ((hp * dx[i]) + x[i] + (ho * dx[i + 1]) - x[i + 1]) / den;
x[i] = ((hp * x[i]) + (ho * x[i + 1])) / den;
ddx[i] = ((hp*ddx[i]) + (ho*ddx[i + 1]) + (2*dx[i]) - (2*dx[i + 1]))/den;
dx[i] = ((hp*dx[i]) + x[i] + (ho*dx[i + 1]) - x[i + 1])/den;
x[i] = ((hp*x[i]) + (ho*x[i + 1]))/den;
}
}
return new Tuple<double, double, double>(x[0], dx[0], ddx[0]);
return ddx[0];
}
/// <summary>
/// Integrate up to point t. NOT SUPPORTED.
/// Indefinite integral at point t. NOT SUPPORTED.
/// </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"/>
/// <param name="t">Point t to integrate at.</param>
double IInterpolation.Integrate(double t)
{
throw new NotSupportedException();
}
/// <summary>
/// Definite integral between points a and b. NOT SUPPORTED.
/// </summary>
/// <param name="a">Left bound of the integration interval [a,b].</param>
/// <param name="b">Right bound of the integration interval [a,b].</param>
double IInterpolation.Integrate(double a, double b)
{
throw new NotSupportedException();
}
}
}

70
src/Numerics/Interpolation/SplineInterpolation.cs

@ -4,7 +4,7 @@
// http://github.com/mathnet/mathnet-numerics
// 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
// obtaining a copy of this software and associated documentation
@ -77,8 +77,6 @@ namespace MathNet.Numerics.Interpolation
/// <summary>
/// Gets a value indicating whether the algorithm supports differentiation (interpolated derivative).
/// </summary>
/// <seealso cref="Differentiate(double)"/>
/// <seealso cref="DifferentiateAll(double)"/>
bool IInterpolation.SupportsDifferentiation
{
get { return true; }
@ -87,7 +85,6 @@ namespace MathNet.Numerics.Interpolation
/// <summary>
/// Gets a value indicating whether the algorithm supports integration (interpolated quadrature).
/// </summary>
/// <seealso cref="Integrate"/>
bool IInterpolation.SupportsIntegration
{
get { return true; }
@ -136,16 +133,16 @@ namespace MathNet.Numerics.Interpolation
/// <returns>Interpolated value x(t).</returns>
public double Interpolate(double t)
{
int closestLeftIndex = IndexOfClosestPointLeftOf(t);
int closestLeftIndex = LeftBracketIndex(t);
// Interpolation
double offset = t - _points[closestLeftIndex];
int k = closestLeftIndex << 2;
return _coefficients[k]
+ (offset*(_coefficients[k + 1]
+ (offset*(_coefficients[k + 2]
+ (offset*_coefficients[k + 3])))));
+ (offset*(_coefficients[k + 1]
+ (offset*(_coefficients[k + 2]
+ (offset*_coefficients[k + 3])))));
}
/// <summary>
@ -153,49 +150,38 @@ namespace MathNet.Numerics.Interpolation
/// </summary>
/// <param name="t">Point t to interpolate at.</param>
/// <returns>Interpolated first derivative at point t.</returns>
/// <seealso cref="IInterpolation.SupportsDifferentiation"/>
/// <seealso cref="DifferentiateAll(double)"/>
public double Differentiate(double t)
{
int closestLeftIndex = IndexOfClosestPointLeftOf(t);
// Differentiation
int closestLeftIndex = LeftBracketIndex(t);
double offset = t - _points[closestLeftIndex];
int k = closestLeftIndex << 2;
return _coefficients[k + 1]
+ (2*offset*_coefficients[k + 2])
+ (3*offset*offset*_coefficients[k + 3]);
+ (2*offset*_coefficients[k + 2])
+ (3*offset*offset*_coefficients[k + 3]);
}
/// <summary>
/// Interpolate, differentiate and 2nd differentiate at point t.
/// Differentiate twice 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)"/>
public Tuple<double, double, double> DifferentiateAll(double t)
/// <returns>Interpolated second derivative at point t.</returns>
public double Differentiate2(double t)
{
int closestLeftIndex = IndexOfClosestPointLeftOf(t);
int closestLeftIndex = LeftBracketIndex(t);
double offset = t - _points[closestLeftIndex];
int k = closestLeftIndex << 2;
return new Tuple<double, double, double>(
_coefficients[k] + (offset*(_coefficients[k + 1] + (offset*(_coefficients[k + 2] + (offset*_coefficients[k + 3]))))),
_coefficients[k + 1] + (2*offset*_coefficients[k + 2]) + (3*offset*offset*_coefficients[k + 3]),
(2*_coefficients[k + 2]) + (6*offset*_coefficients[k + 3]));
return (2*_coefficients[k + 2]) + (6*offset*_coefficients[k + 3]);
}
/// <summary>
/// Integrate up to point t.
/// Indefinite integral at 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"/>
/// <param name="t">Point t to integrate at.</param>
public double Integrate(double t)
{
int closestLeftIndex = IndexOfClosestPointLeftOf(t);
int closestLeftIndex = LeftBracketIndex(t);
// Integration
double result = 0;
@ -203,18 +189,28 @@ namespace MathNet.Numerics.Interpolation
{
double w = _points[i + 1] - _points[i];
result += w*(_coefficients[j]
+ ((w*_coefficients[j + 1]*0.5)
+ (w*((_coefficients[j + 2]/3)
+ (w*_coefficients[j + 3]*0.25)))));
+ ((w*_coefficients[j + 1]*0.5)
+ (w*((_coefficients[j + 2]/3)
+ (w*_coefficients[j + 3]*0.25)))));
}
double offset = t - _points[closestLeftIndex];
int k = closestLeftIndex << 2;
return result + (offset*(_coefficients[k]
+ (offset*_coefficients[k + 1]*0.5)
+ (offset*_coefficients[k + 2]/3)
+ (offset*_coefficients[k + 3]*0.25)));
+ (offset*_coefficients[k + 1]*0.5)
+ (offset*_coefficients[k + 2]/3)
+ (offset*_coefficients[k + 3]*0.25)));
}
/// <summary>
/// Definite integral between points a and b.
/// </summary>
/// <param name="a">Left bound of the integration interval [a,b].</param>
/// <param name="b">Right bound of the integration interval [a,b].</param>
public double Integrate(double a, double b)
{
return Integrate(b) - Integrate(a);
}
/// <summary>
@ -222,7 +218,7 @@ namespace MathNet.Numerics.Interpolation
/// </summary>
/// <param name="t">The value to look for.</param>
/// <returns>The sample point index.</returns>
int IndexOfClosestPointLeftOf(double t)
int LeftBracketIndex(double t)
{
// Binary search in the [ t[0], ..., t[n-2] ] (t[n-1] is not included)
int low = 0;

16
src/UnitTests/InterpolationTests/AkimaSplineTest.cs

@ -28,11 +28,11 @@
// OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
using MathNet.Numerics.Interpolation;
using NUnit.Framework;
namespace MathNet.Numerics.UnitTests.InterpolationTests
{
using Interpolation;
using NUnit.Framework;
/// <summary>
/// AkimaSpline test case.
/// </summary>
@ -42,12 +42,12 @@ namespace MathNet.Numerics.UnitTests.InterpolationTests
/// <summary>
/// Sample points.
/// </summary>
private readonly double[] _t = new[] { -2.0, -1.0, 0.0, 1.0, 2.0 };
readonly double[] _t = { -2.0, -1.0, 0.0, 1.0, 2.0 };
/// <summary>
/// Sample values.
/// </summary>
private readonly double[] _x = new[] { 1.0, 2.0, -1.0, 0.0, 1.0 };
readonly double[] _x = { 1.0, 2.0, -1.0, 0.0, 1.0 };
/// <summary>
/// Verifies that the interpolation matches the given value at all the provided sample points.
@ -60,9 +60,6 @@ namespace MathNet.Numerics.UnitTests.InterpolationTests
for (int i = 0; i < _x.Length; i++)
{
Assert.AreEqual(_x[i], interpolation.Interpolate(_t[i]), "A Exact Point " + i);
var actual = interpolation.DifferentiateAll(_t[i]);
Assert.AreEqual(_x[i], actual.Item1, "B Exact Point " + i);
}
}
@ -87,9 +84,6 @@ namespace MathNet.Numerics.UnitTests.InterpolationTests
// TODO: Verify the expected values (that they are really the expected ones)
Assert.AreEqual(x, interpolation.Interpolate(t), maxAbsoluteError, "Interpolation at {0}", t);
var actual = interpolation.DifferentiateAll(t);
Assert.AreEqual(x, actual.Item1, maxAbsoluteError, "Interpolation as by-product of differentiation at {0}", t);
}
/// <summary>

12
src/UnitTests/InterpolationTests/BulirschStoerRationalTest.cs

@ -4,7 +4,7 @@
// http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com
//
// Copyright (c) 2002-2011 Math.NET
// Copyright (c) 2002-2013 Math.NET
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
@ -28,11 +28,11 @@
// OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
using MathNet.Numerics.Interpolation;
using NUnit.Framework;
namespace MathNet.Numerics.UnitTests.InterpolationTests
{
using Interpolation;
using NUnit.Framework;
/// <summary>
/// BulirschStoerRational test case.
/// </summary>
@ -42,12 +42,12 @@ namespace MathNet.Numerics.UnitTests.InterpolationTests
/// <summary>
/// Sample points.
/// </summary>
private readonly double[] _t = new[] { 0d, 1, 3, 4, 5 };
readonly double[] _t = { 0d, 1, 3, 4, 5 };
/// <summary>
/// Sample values.
/// </summary>
private readonly double[] _x = new[] { 0d, 3, 1000, -1000, 3 };
readonly double[] _x = { 0d, 3, 1000, -1000, 3 };
/// <summary>
/// Verifies that the interpolation matches the given value at all the provided sample points.

41
src/UnitTests/InterpolationTests/CubicSplineTest.cs

@ -28,12 +28,11 @@
// OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
using MathNet.Numerics.Interpolation;
using NUnit.Framework;
namespace MathNet.Numerics.UnitTests.InterpolationTests
{
using System;
using Interpolation;
using NUnit.Framework;
/// <summary>
/// CubicSpline Test case.
/// </summary>
@ -43,12 +42,12 @@ namespace MathNet.Numerics.UnitTests.InterpolationTests
/// <summary>
/// Sample points.
/// </summary>
private readonly double[] _t = new[] { -2.0, -1.0, 0.0, 1.0, 2.0 };
readonly double[] _t = { -2.0, -1.0, 0.0, 1.0, 2.0 };
/// <summary>
/// Sample values.
/// </summary>
private readonly double[] _x = new[] { 1.0, 2.0, -1.0, 0.0, 1.0 };
readonly double[] _x = { 1.0, 2.0, -1.0, 0.0, 1.0 };
/// <summary>
/// Verifies that the interpolation matches the given value at all the provided sample points.
@ -61,9 +60,6 @@ namespace MathNet.Numerics.UnitTests.InterpolationTests
for (int i = 0; i < _x.Length; i++)
{
Assert.AreEqual(_x[i], interpolation.Interpolate(_t[i]), "A Exact Point " + i);
var actual = interpolation.DifferentiateAll(_t[i]);
Assert.AreEqual(_x[i], actual.Item1, "B Exact Point " + i);
}
}
@ -92,9 +88,6 @@ namespace MathNet.Numerics.UnitTests.InterpolationTests
IInterpolation interpolation = new CubicSplineInterpolation(_t, _x);
Assert.AreEqual(x, interpolation.Interpolate(t), maxAbsoluteError, "Interpolation at {0}", t);
var actual = interpolation.DifferentiateAll(t);
Assert.AreEqual(x, actual.Item1, maxAbsoluteError, "Interpolation as by-product of differentiation at {0}", t);
}
/// <summary>
@ -108,9 +101,6 @@ namespace MathNet.Numerics.UnitTests.InterpolationTests
for (int i = 0; i < _x.Length; i++)
{
Assert.AreEqual(_x[i], interpolation.Interpolate(_t[i]), "A Exact Point " + i);
var actual = interpolation.DifferentiateAll(_t[i]);
Assert.AreEqual(_x[i], actual.Item1, "B Exact Point " + i);
}
}
@ -139,9 +129,6 @@ namespace MathNet.Numerics.UnitTests.InterpolationTests
IInterpolation interpolation = new CubicSplineInterpolation(_t, _x, SplineBoundaryCondition.FirstDerivative, 1.0, SplineBoundaryCondition.FirstDerivative, -1.0);
Assert.AreEqual(x, interpolation.Interpolate(t), maxAbsoluteError, "Interpolation at {0}", t);
var actual = interpolation.DifferentiateAll(t);
Assert.AreEqual(x, actual.Item1, maxAbsoluteError, "Interpolation as by-product of differentiation at {0}", t);
}
/// <summary>
@ -155,9 +142,6 @@ namespace MathNet.Numerics.UnitTests.InterpolationTests
for (int i = 0; i < _x.Length; i++)
{
Assert.AreEqual(_x[i], interpolation.Interpolate(_t[i]), "A Exact Point " + i);
var actual = interpolation.DifferentiateAll(_t[i]);
Assert.AreEqual(_x[i], actual.Item1, "B Exact Point " + i);
}
}
@ -186,9 +170,6 @@ namespace MathNet.Numerics.UnitTests.InterpolationTests
IInterpolation interpolation = new CubicSplineInterpolation(_t, _x, SplineBoundaryCondition.SecondDerivative, -5.0, SplineBoundaryCondition.SecondDerivative, -1.0);
Assert.AreEqual(x, interpolation.Interpolate(t), maxAbsoluteError, "Interpolation at {0}", t);
var actual = interpolation.DifferentiateAll(t);
Assert.AreEqual(x, actual.Item1, maxAbsoluteError, "Interpolation as by-product of differentiation at {0}", t);
}
/// <summary>
@ -208,17 +189,5 @@ namespace MathNet.Numerics.UnitTests.InterpolationTests
Assert.AreEqual(ytest[i], interpolation.Interpolate(xtest[i]), 1e-15, "Linear with {0} samples, sample {1}", samples, i);
}
}
/// <summary>
/// Verifies that sample points are required to be sorted in strictly monotonically ascending order.
/// </summary>
[Test]
[ExpectedException(typeof(ArgumentException))]
public void Constructor_SamplePointsNotStrictlyAscending_Throws()
{
var x = new[] { -1.0, 0.0, 1.5, 1.5, 2.5, 4.0 };
var y = new[] { 1.0, 0.3, -0.7, -0.6, -0.1, 0.4 };
var interpolation = new CubicSplineInterpolation(x, y);
}
}
}

14
src/UnitTests/InterpolationTests/EquidistantPolynomialTest.cs

@ -4,7 +4,7 @@
// http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com
//
// Copyright (c) 2002-2011 Math.NET
// Copyright (c) 2002-2013 Math.NET
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
@ -28,11 +28,11 @@
// OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
using MathNet.Numerics.Interpolation;
using NUnit.Framework;
namespace MathNet.Numerics.UnitTests.InterpolationTests
{
using Interpolation;
using NUnit.Framework;
/// <summary>
/// EquidistantPolynomial Test case.
/// </summary>
@ -42,17 +42,17 @@ namespace MathNet.Numerics.UnitTests.InterpolationTests
/// <summary>
/// Left bound;
/// </summary>
private const double Tmin = 0.0;
const double Tmin = 0.0;
/// <summary>
/// Right bound.
/// </summary>
private const double Tmax = 4.0;
const double Tmax = 4.0;
/// <summary>
/// Sample values.
/// </summary>
private readonly double[] _x = new[] { 0.0, 3.0, 2.5, 1.0, 3.0 };
readonly double[] _x = { 0.0, 3.0, 2.5, 1.0, 3.0 };
/// <summary>
/// Verifies that the interpolation matches the given value at all the provided sample points.

18
src/UnitTests/InterpolationTests/FloaterHormannRationalTest.cs

@ -4,7 +4,7 @@
// http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com
//
// Copyright (c) 2002-2011 Math.NET
// Copyright (c) 2002-2013 Math.NET
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
@ -28,11 +28,11 @@
// OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
using MathNet.Numerics.Interpolation;
using NUnit.Framework;
namespace MathNet.Numerics.UnitTests.InterpolationTests
{
using Interpolation;
using NUnit.Framework;
/// <summary>
/// FloaterHormannRational test case.
/// </summary>
@ -42,12 +42,12 @@ namespace MathNet.Numerics.UnitTests.InterpolationTests
/// <summary>
/// Sample points.
/// </summary>
private readonly double[] _t = new[] { -2.0, -1.0, 0.0, 1.0, 2.0 };
readonly double[] _t = { -2.0, -1.0, 0.0, 1.0, 2.0 };
/// <summary>
/// Sample values.
/// </summary>
private readonly double[] _x = new[] { 1.0, 2.0, -1.0, 0.0, 1.0 };
readonly double[] _x = { 1.0, 2.0, -1.0, 0.0, 1.0 };
/// <summary>
/// Verifies that the interpolation matches the given value at all the provided polynomial sample points.
@ -99,12 +99,12 @@ namespace MathNet.Numerics.UnitTests.InterpolationTests
var t = new double[40];
var x = new double[40];
const double Step = 10.0 / 39.0;
const double Step = 10.0/39.0;
for (int i = 0; i < t.Length; i++)
{
double tt = -5 + (i * Step);
double tt = -5 + (i*Step);
t[i] = tt;
x[i] = 1.0 / (1.0 + (tt * tt));
x[i] = 1.0/(1.0 + (tt*tt));
}
IInterpolation interpolation = new FloaterHormannRationalInterpolation(t, x);

8
src/UnitTests/InterpolationTests/LinearInterpolationCase.cs

@ -4,7 +4,7 @@
// http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com
//
// Copyright (c) 2002-2011 Math.NET
// Copyright (c) 2002-2013 Math.NET
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
@ -60,7 +60,7 @@ namespace MathNet.Numerics.UnitTests.InterpolationTests
for (int i = 0; i < x.Length; i++)
{
x[i] = i + sampleOffset;
y[i] = (x[i] * slope) + intercept;
y[i] = (x[i]*slope) + intercept;
}
// build linear test vectors randomly between the sample points
@ -71,14 +71,14 @@ namespace MathNet.Numerics.UnitTests.InterpolationTests
// y = const
xtest[0] = sampleOffset - uniform.Sample();
xtest[1] = sampleOffset + uniform.Sample();
ytest[0] = ytest[1] = (sampleOffset * slope) + intercept;
ytest[0] = ytest[1] = (sampleOffset*slope) + intercept;
}
else
{
for (int i = 0; i < xtest.Length; i++)
{
xtest[i] = (i - 1) + sampleOffset + uniform.Sample();
ytest[i] = (xtest[i] * slope) + intercept;
ytest[i] = (xtest[i]*slope) + intercept;
}
}
}

70
src/UnitTests/InterpolationTests/LinearSplineTest.cs

@ -33,21 +33,46 @@ using NUnit.Framework;
namespace MathNet.Numerics.UnitTests.InterpolationTests
{
/// <summary>
/// LinearSpline test case
/// </summary>
[TestFixture, Category("Interpolation")]
public class LinearSplineTest
{
/// <summary>
/// Sample points.
/// </summary>
readonly double[] _t = { -2.0, -1.0, 0.0, 1.0, 2.0 };
readonly double[] _y = { 1.0, 2.0, -1.0, 0.0, 1.0 };
/// <summary>
/// Sample values.
/// </summary>
readonly double[] _x = { 1.0, 2.0, -1.0, 0.0, 1.0 };
[Test]
public void FirstDerivative()
{
IInterpolation ip = LinearSpline.Interpolate(_t, _y);
Assert.That(ip.Differentiate(-3.0), Is.EqualTo(1.0));
Assert.That(ip.Differentiate(-2.0), Is.EqualTo(1.0));
Assert.That(ip.Differentiate(-1.5), Is.EqualTo(1.0));
Assert.That(ip.Differentiate(-1.0), Is.EqualTo(-3.0));
Assert.That(ip.Differentiate(-0.5), Is.EqualTo(-3.0));
Assert.That(ip.Differentiate(0.0), Is.EqualTo(1.0));
Assert.That(ip.Differentiate(0.5), Is.EqualTo(1.0));
Assert.That(ip.Differentiate(1.0), Is.EqualTo(1.0));
Assert.That(ip.Differentiate(2.0), Is.EqualTo(1.0));
Assert.That(ip.Differentiate(3.0), Is.EqualTo(1.0));
}
[Test]
public void DefiniteIntegral()
{
IInterpolation ip = LinearSpline.Interpolate(_t, _y);
Assert.That(ip.Integrate(-4.0, -3.0), Is.EqualTo(-0.5));
Assert.That(ip.Integrate(-3.0, -2.0), Is.EqualTo(0.5));
Assert.That(ip.Integrate(-2.0, -1.0), Is.EqualTo(1.5));
Assert.That(ip.Integrate(-1.0, 0.0), Is.EqualTo(0.5));
Assert.That(ip.Integrate(0.0, 1.0), Is.EqualTo(-0.5));
Assert.That(ip.Integrate(1.0, 2.0), Is.EqualTo(0.5));
Assert.That(ip.Integrate(2.0, 3.0), Is.EqualTo(1.5));
Assert.That(ip.Integrate(3.0, 4.0), Is.EqualTo(2.5));
Assert.That(ip.Integrate(0.0, 4.0), Is.EqualTo(4.0));
Assert.That(ip.Integrate(-3.0, -1.0), Is.EqualTo(2.0));
Assert.That(ip.Integrate(-3.0, 4.0), Is.EqualTo(6.5));
Assert.That(ip.Integrate(0.5, 1.5), Is.EqualTo(0.0));
Assert.That(ip.Integrate(-2.5, -1.0), Is.EqualTo(1.875));
}
/// <summary>
/// Verifies that the interpolation matches the given value at all the provided sample points.
@ -55,14 +80,10 @@ namespace MathNet.Numerics.UnitTests.InterpolationTests
[Test]
public void FitsAtSamplePoints()
{
IInterpolation interpolation = LinearSpline.Interpolate(_t, _x);
for (int i = 0; i < _x.Length; i++)
IInterpolation ip = LinearSpline.Interpolate(_t, _y);
for (int i = 0; i < _y.Length; i++)
{
Assert.AreEqual(_x[i], interpolation.Interpolate(_t[i]), "A Exact Point " + i);
var actual = interpolation.DifferentiateAll(_t[i]);
Assert.AreEqual(_x[i], actual.Item1, "B Exact Point " + i);
Assert.AreEqual(_y[i], ip.Interpolate(_t[i]), "A Exact Point " + i);
}
}
@ -86,14 +107,10 @@ namespace MathNet.Numerics.UnitTests.InterpolationTests
[TestCase(1.2, .2, 1e-15)]
[TestCase(10.0, 9.0, 1e-15)]
[TestCase(-10.0, -7.0, 1e-15)]
public void FitsAtArbitraryPointsWithMaple(double t, double x, double maxAbsoluteError)
public void FitsAtArbitraryPoints(double t, double x, double maxAbsoluteError)
{
IInterpolation interpolation = LinearSpline.Interpolate(_t, _x);
Assert.AreEqual(x, interpolation.Interpolate(t), maxAbsoluteError, "Interpolation at {0}", t);
var actual = interpolation.DifferentiateAll(t);
Assert.AreEqual(x, actual.Item1, maxAbsoluteError, "Interpolation as by-product of differentiation at {0}", t);
IInterpolation ip = LinearSpline.Interpolate(_t, _y);
Assert.AreEqual(x, ip.Interpolate(t), maxAbsoluteError, "Interpolation at {0}", t);
}
/// <summary>
@ -107,10 +124,11 @@ namespace MathNet.Numerics.UnitTests.InterpolationTests
{
double[] x, y, xtest, ytest;
LinearInterpolationCase.Build(out x, out y, out xtest, out ytest, samples);
IInterpolation interpolation = LinearSpline.Interpolate(x, y);
IInterpolation ip = LinearSpline.Interpolate(x, y);
for (int i = 0; i < xtest.Length; i++)
{
Assert.AreEqual(ytest[i], interpolation.Interpolate(xtest[i]), 1e-15, "Linear with {0} samples, sample {1}", samples, i);
Assert.AreEqual(ytest[i], ip.Interpolate(xtest[i]), 1e-15, "Linear with {0} samples, sample {1}", samples, i);
}
}
}

28
src/UnitTests/InterpolationTests/NevillePolynomialTest.cs

@ -4,7 +4,7 @@
// http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com
//
// Copyright (c) 2002-2011 Math.NET
// Copyright (c) 2002-2013 Math.NET
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
@ -28,15 +28,15 @@
// OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
using System;
using System.Globalization;
using System.IO;
using System.Linq;
using MathNet.Numerics.Interpolation;
using NUnit.Framework;
namespace MathNet.Numerics.UnitTests.InterpolationTests
{
using System;
using System.Globalization;
using System.IO;
using System.Linq;
using Interpolation;
using NUnit.Framework;
/// <summary>
/// NevillePolynomial test case.
/// </summary>
@ -46,12 +46,12 @@ namespace MathNet.Numerics.UnitTests.InterpolationTests
/// <summary>
/// Sample points.
/// </summary>
private readonly double[] _t = new[] { 0.0, 1.0, 3.0, 4.0 };
readonly double[] _t = { 0.0, 1.0, 3.0, 4.0 };
/// <summary>
/// Sample values.
/// </summary>
private readonly double[] _x = new[] { 0.0, 3.0, 1.0, 3.0 };
readonly double[] _x = { 0.0, 3.0, 1.0, 3.0 };
/// <summary>
/// Verifies that the interpolation matches the given value at all the provided sample points.
@ -64,9 +64,6 @@ namespace MathNet.Numerics.UnitTests.InterpolationTests
for (int i = 0; i < _x.Length; i++)
{
Assert.AreEqual(_x[i], interpolation.Interpolate(_t[i]), "A Exact Point " + i);
var actual = interpolation.DifferentiateAll(_t[i]);
Assert.AreEqual(_x[i], actual.Item1, "B Exact Point " + i);
}
}
@ -93,9 +90,6 @@ namespace MathNet.Numerics.UnitTests.InterpolationTests
IInterpolation interpolation = new NevillePolynomialInterpolation(_t, _x);
Assert.AreEqual(x, interpolation.Interpolate(t), maxAbsoluteError, "Interpolation at {0}", t);
var actual = interpolation.DifferentiateAll(t);
Assert.AreEqual(x, actual.Item1, maxAbsoluteError, "Interpolation as by-product of differentiation at {0}", t);
}
/// <summary>
@ -120,7 +114,7 @@ namespace MathNet.Numerics.UnitTests.InterpolationTests
/// Verifies that all sample points must be unique.
/// </summary>
[Test]
[ExpectedException(typeof(ArgumentException))]
[ExpectedException(typeof (ArgumentException))]
public void Constructor_SamplePointsNotUnique_Throws()
{
var x = new[] { -1.0, 0.0, 1.5, 1.5, 2.5, 4.0 };

Loading…
Cancel
Save