From 3aca1f4bf7de9c2f2fc00032d65ce894427c799e Mon Sep 17 00:00:00 2001 From: Christoph Ruegg Date: Mon, 23 Dec 2013 11:46:11 +0100 Subject: [PATCH] Interpolation: api cleanup --- src/Numerics/Interpolate.cs | 16 ++++++++-------- src/Numerics/Interpolation/CubicSpline.cs | 16 +++++++++++++++- .../InterpolationTests/CubicSplineTest.cs | 12 ++++++------ 3 files changed, 29 insertions(+), 15 deletions(-) diff --git a/src/Numerics/Interpolate.cs b/src/Numerics/Interpolate.cs index 50db7a0d..988dd055 100644 --- a/src/Numerics/Interpolate.cs +++ b/src/Numerics/Interpolate.cs @@ -71,14 +71,14 @@ namespace MathNet.Numerics /// /// Create a burlisch stoer rational interpolation based on arbitrary points. /// - /// The sample points t. Supports both lists and arrays. - /// The sample point values x(t). Supports both lists and arrays. + /// The sample points t. Optimized for arrays.. + /// The sample point values x(t). Optimized for arrays. /// /// An interpolation scheme optimized for the given sample points and values, /// which can then be used to compute interpolations and extrapolations /// on arbitrary points. /// - public static IInterpolation RationalWithPoles(IList points, IList values) + public static IInterpolation RationalWithPoles(IEnumerable points, IEnumerable values) { return new BulirschStoerRationalInterpolation(points, values); } @@ -107,14 +107,14 @@ namespace MathNet.Numerics /// If the points happen to be equidistant, consider to use the much more robust PolynomialEquidistant instead. /// Otherwise, consider whether RationalWithoutPoles would not be a more robust alternative. /// - /// The sample points t. Supports both lists and arrays. - /// The sample point values x(t). Supports both lists and arrays. + /// The sample points t. Optimized for arrays. + /// The sample point values x(t). Optimized for arrays. /// /// An interpolation scheme optimized for the given sample points and values, /// which can then be used to compute interpolations and extrapolations /// on arbitrary points. /// - public static IInterpolation Polynomial(IList points, IList values) + public static IInterpolation Polynomial(IEnumerable points, IEnumerable values) { return new NevillePolynomialInterpolation(points, values); } @@ -139,7 +139,7 @@ namespace MathNet.Numerics } /// - /// Create an piecewise cubic spline interpolation based on arbitrary points, with zero secondary derivatives at the boundaries. + /// Create an piecewise natural cubic spline interpolation based on arbitrary points, with zero secondary derivatives at the boundaries. /// /// The sample points t. Optimized for arrays. /// The sample point values x(t). Optimized for arrays. @@ -154,7 +154,7 @@ namespace MathNet.Numerics /// public static IInterpolation CubicSpline(IEnumerable points, IEnumerable values) { - return Interpolation.CubicSpline.Interpolate(points, values); + return Interpolation.CubicSpline.InterpolateNatural(points, values); } /// diff --git a/src/Numerics/Interpolation/CubicSpline.cs b/src/Numerics/Interpolation/CubicSpline.cs index 0468c599..8c8ea2bf 100644 --- a/src/Numerics/Interpolation/CubicSpline.cs +++ b/src/Numerics/Interpolation/CubicSpline.cs @@ -170,11 +170,25 @@ namespace MathNet.Numerics.Interpolation return InterpolateHermite(xx, yy, dd); } - public static CubicSpline Interpolate(IEnumerable x, IEnumerable y) + /// + /// Create a natural cubic spline interpolation from a set of (x,y) value pairs and zero second derivatives at the two boundaries. + /// + /// + /// The value pairs do not have to be sorted, but if they are not sorted ascendingly + /// and the passed x and y arguments are arrays, they will be sorted inplace and thus modified. + /// + public static CubicSpline InterpolateNatural(IEnumerable x, IEnumerable y) { return InterpolateBoundaries(x, y, SplineBoundaryCondition.SecondDerivative, 0.0, SplineBoundaryCondition.SecondDerivative, 0.0); } + /// + /// Create a cubic spline interpolation from a set of (x,y) value pairs and custom boundary/termination conditions. + /// + /// + /// The value pairs do not have to be sorted, but if they are not sorted ascendingly + /// and the passed x and y arguments are arrays, they will be sorted inplace and thus modified. + /// public static CubicSpline InterpolateBoundaries(IEnumerable x, IEnumerable y, SplineBoundaryCondition leftBoundaryCondition, double leftBoundary, SplineBoundaryCondition rightBoundaryCondition, double rightBoundary) diff --git a/src/UnitTests/InterpolationTests/CubicSplineTest.cs b/src/UnitTests/InterpolationTests/CubicSplineTest.cs index 4185b5d3..eba63f64 100644 --- a/src/UnitTests/InterpolationTests/CubicSplineTest.cs +++ b/src/UnitTests/InterpolationTests/CubicSplineTest.cs @@ -45,7 +45,7 @@ namespace MathNet.Numerics.UnitTests.InterpolationTests [Test] public void NaturalFitsAtSamplePoints() { - IInterpolation it = CubicSpline.Interpolate(_t, _y); + IInterpolation it = CubicSpline.InterpolateNatural(_t, _y); for (int i = 0; i < _y.Length; i++) { Assert.AreEqual(_y[i], it.Interpolate(_t[i]), "A Exact Point " + i); @@ -72,9 +72,9 @@ namespace MathNet.Numerics.UnitTests.InterpolationTests [TestCase(1.2, .30285714285714285716, 1e-15)] [TestCase(10.0, 189, 1e-15)] [TestCase(-10.0, 677, 1e-12)] - public void NaturalFitsAtArbitraryPointsWithMaple(double t, double x, double maxAbsoluteError) + public void NaturalFitsAtArbitraryPoints(double t, double x, double maxAbsoluteError) { - IInterpolation it = CubicSpline.Interpolate(_t, _y); + IInterpolation it = CubicSpline.InterpolateNatural(_t, _y); Assert.AreEqual(x, it.Interpolate(t), maxAbsoluteError, "Interpolation at {0}", t); } @@ -111,7 +111,7 @@ namespace MathNet.Numerics.UnitTests.InterpolationTests [TestCase(1.2, .4148571428571428571, 1e-15)] [TestCase(10.0, -608.14285714285714286, 1e-12)] [TestCase(-10.0, 1330.1428571428571429, 1e-12)] - public void FixedFirstDerivativeFitsAtArbitraryPointsWithMaple(double t, double x, double maxAbsoluteError) + public void FixedFirstDerivativeFitsAtArbitraryPoints(double t, double x, double maxAbsoluteError) { IInterpolation it = CubicSpline.InterpolateBoundaries(_t, _y, SplineBoundaryCondition.FirstDerivative, 1.0, SplineBoundaryCondition.FirstDerivative, -1.0); Assert.AreEqual(x, it.Interpolate(t), maxAbsoluteError, "Interpolation at {0}", t); @@ -150,7 +150,7 @@ namespace MathNet.Numerics.UnitTests.InterpolationTests [TestCase(1.2, .31771428571428571421, 1e-15)] [TestCase(10.0, 39, 1e-13)] [TestCase(-10.0, -37, 1e-12)] - public void FixedSecondDerivativeFitsAtArbitraryPointsWithMaple(double t, double x, double maxAbsoluteError) + public void FixedSecondDerivativeFitsAtArbitraryPoints(double t, double x, double maxAbsoluteError) { IInterpolation it = CubicSpline.InterpolateBoundaries(_t, _y, SplineBoundaryCondition.SecondDerivative, -5.0, SplineBoundaryCondition.SecondDerivative, -1.0); Assert.AreEqual(x, it.Interpolate(t), maxAbsoluteError, "Interpolation at {0}", t); @@ -167,7 +167,7 @@ namespace MathNet.Numerics.UnitTests.InterpolationTests { double[] x, y, xtest, ytest; LinearInterpolationCase.Build(out x, out y, out xtest, out ytest, samples); - IInterpolation it = CubicSpline.Interpolate(x, y); + IInterpolation it = CubicSpline.InterpolateNatural(x, y); for (int i = 0; i < xtest.Length; i++) { Assert.AreEqual(ytest[i], it.Interpolate(xtest[i]), 1e-15, "Linear with {0} samples, sample {1}", samples, i);