|
|
|
@ -33,21 +33,11 @@ using NUnit.Framework; |
|
|
|
|
|
|
|
namespace MathNet.Numerics.UnitTests.InterpolationTests |
|
|
|
{ |
|
|
|
/// <summary>
|
|
|
|
/// CubicSpline Test case.
|
|
|
|
/// </summary>
|
|
|
|
[TestFixture, Category("Interpolation")] |
|
|
|
public class CubicSplineTest |
|
|
|
{ |
|
|
|
/// <summary>
|
|
|
|
/// Sample points.
|
|
|
|
/// </summary>
|
|
|
|
readonly double[] _t = { -2.0, -1.0, 0.0, 1.0, 2.0 }; |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Sample values.
|
|
|
|
/// </summary>
|
|
|
|
readonly double[] _x = { 1.0, 2.0, -1.0, 0.0, 1.0 }; |
|
|
|
readonly double[] _y = { 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.
|
|
|
|
@ -55,11 +45,10 @@ namespace MathNet.Numerics.UnitTests.InterpolationTests |
|
|
|
[Test] |
|
|
|
public void NaturalFitsAtSamplePoints() |
|
|
|
{ |
|
|
|
IInterpolation interpolation = new CubicSplineInterpolation(_t, _x); |
|
|
|
|
|
|
|
for (int i = 0; i < _x.Length; i++) |
|
|
|
IInterpolation it = CubicSpline.Interpolate(_t, _y); |
|
|
|
for (int i = 0; i < _y.Length; i++) |
|
|
|
{ |
|
|
|
Assert.AreEqual(_x[i], interpolation.Interpolate(_t[i]), "A Exact Point " + i); |
|
|
|
Assert.AreEqual(_y[i], it.Interpolate(_t[i]), "A Exact Point " + i); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@ -85,9 +74,8 @@ namespace MathNet.Numerics.UnitTests.InterpolationTests |
|
|
|
[TestCase(-10.0, 677, 1e-12)] |
|
|
|
public void NaturalFitsAtArbitraryPointsWithMaple(double t, double x, double maxAbsoluteError) |
|
|
|
{ |
|
|
|
IInterpolation interpolation = new CubicSplineInterpolation(_t, _x); |
|
|
|
|
|
|
|
Assert.AreEqual(x, interpolation.Interpolate(t), maxAbsoluteError, "Interpolation at {0}", t); |
|
|
|
IInterpolation it = CubicSpline.Interpolate(_t, _y); |
|
|
|
Assert.AreEqual(x, it.Interpolate(t), maxAbsoluteError, "Interpolation at {0}", t); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
@ -96,11 +84,10 @@ namespace MathNet.Numerics.UnitTests.InterpolationTests |
|
|
|
[Test] |
|
|
|
public void FixedFirstDerivativeFitsAtSamplePoints() |
|
|
|
{ |
|
|
|
IInterpolation interpolation = new CubicSplineInterpolation(_t, _x, SplineBoundaryCondition.FirstDerivative, 1.0, SplineBoundaryCondition.FirstDerivative, -1.0); |
|
|
|
|
|
|
|
for (int i = 0; i < _x.Length; i++) |
|
|
|
IInterpolation it = CubicSpline.InterpolateBoundaries(_t, _y, SplineBoundaryCondition.FirstDerivative, 1.0, SplineBoundaryCondition.FirstDerivative, -1.0); |
|
|
|
for (int i = 0; i < _y.Length; i++) |
|
|
|
{ |
|
|
|
Assert.AreEqual(_x[i], interpolation.Interpolate(_t[i]), "A Exact Point " + i); |
|
|
|
Assert.AreEqual(_y[i], it.Interpolate(_t[i]), "A Exact Point " + i); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@ -126,9 +113,8 @@ namespace MathNet.Numerics.UnitTests.InterpolationTests |
|
|
|
[TestCase(-10.0, 1330.1428571428571429, 1e-12)] |
|
|
|
public void FixedFirstDerivativeFitsAtArbitraryPointsWithMaple(double t, double x, double maxAbsoluteError) |
|
|
|
{ |
|
|
|
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); |
|
|
|
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); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
@ -137,11 +123,10 @@ namespace MathNet.Numerics.UnitTests.InterpolationTests |
|
|
|
[Test] |
|
|
|
public void FixedSecondDerivativeFitsAtSamplePoints() |
|
|
|
{ |
|
|
|
IInterpolation interpolation = new CubicSplineInterpolation(_t, _x, SplineBoundaryCondition.SecondDerivative, -5.0, SplineBoundaryCondition.SecondDerivative, -1.0); |
|
|
|
|
|
|
|
for (int i = 0; i < _x.Length; i++) |
|
|
|
IInterpolation it = CubicSpline.InterpolateBoundaries(_t, _y, SplineBoundaryCondition.SecondDerivative, -5.0, SplineBoundaryCondition.SecondDerivative, -1.0); |
|
|
|
for (int i = 0; i < _y.Length; i++) |
|
|
|
{ |
|
|
|
Assert.AreEqual(_x[i], interpolation.Interpolate(_t[i]), "A Exact Point " + i); |
|
|
|
Assert.AreEqual(_y[i], it.Interpolate(_t[i]), "A Exact Point " + i); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@ -167,9 +152,8 @@ namespace MathNet.Numerics.UnitTests.InterpolationTests |
|
|
|
[TestCase(-10.0, -37, 1e-12)] |
|
|
|
public void FixedSecondDerivativeFitsAtArbitraryPointsWithMaple(double t, double x, double maxAbsoluteError) |
|
|
|
{ |
|
|
|
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); |
|
|
|
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); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
@ -183,10 +167,10 @@ namespace MathNet.Numerics.UnitTests.InterpolationTests |
|
|
|
{ |
|
|
|
double[] x, y, xtest, ytest; |
|
|
|
LinearInterpolationCase.Build(out x, out y, out xtest, out ytest, samples); |
|
|
|
IInterpolation interpolation = new CubicSplineInterpolation(x, y); |
|
|
|
IInterpolation it = CubicSpline.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], it.Interpolate(xtest[i]), 1e-15, "Linear with {0} samples, sample {1}", samples, i); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|