From 0478e36a66e78e2d4cb012aad442eb7493a8e383 Mon Sep 17 00:00:00 2001 From: Christoph Ruegg Date: Sat, 1 Aug 2009 05:12:01 +0800 Subject: [PATCH] interpolation test: now verifying minimum sample count check Signed-off-by: Christoph Ruegg --- .../InterpolationContract.cs | 52 +++++++++++++++---- .../InterpolationTests/InterpolationTest.cs | 16 +++--- .../BulirschStoerRationalInterpolation.cs | 5 ++ .../NevillePolynomialInterpolation.cs | 5 ++ 4 files changed, 60 insertions(+), 18 deletions(-) diff --git a/src/Managed.UnitTests/InterpolationTests/InterpolationContract.cs b/src/Managed.UnitTests/InterpolationTests/InterpolationContract.cs index 96c02f1f..4dc559d2 100644 --- a/src/Managed.UnitTests/InterpolationTests/InterpolationContract.cs +++ b/src/Managed.UnitTests/InterpolationTests/InterpolationContract.cs @@ -39,17 +39,12 @@ namespace MathNet.Numerics.UnitTests.InterpolationTests where TInterpolation : IInterpolation { public Func, IList, IInterpolation> Factory { get; set; } - public int[] Order { get; set; } + public int MinimumSampleCount { get; set; } public bool NonStandardParameters { get; set; } public bool LinearBehavior { get; set; } public bool PolynomialBehavior { get; set; } public bool RationalBehavior { get; set; } - public InterpolationContract() - { - Order = new[] { 4 }; - } - protected override IEnumerable GetContractVerificationTests() { // Infrastructure Tests @@ -59,6 +54,7 @@ namespace MathNet.Numerics.UnitTests.InterpolationTests yield return CreateInitChecksForNullTest("InitChecksForNull"); yield return CreateInitChecksForMatchingCountTest("InitChecksForMatchingCount"); + yield return CreateInitChecksForMinimumCountTest("InitChecksForMinimumCount"); if (!NonStandardParameters) { @@ -89,7 +85,10 @@ namespace MathNet.Numerics.UnitTests.InterpolationTests { return new TestCase(name, () => { - var interpolation = Factory(new List { 1, 2, 3 }, new List { 10, 20, 30 }); + double[] points, values; + SampleFunctionEquidistant(t => 5 + 10 * t, -2.0, 8.0, MinimumSampleCount, out points, out values); + + var interpolation = Factory(points, values); AssertionHelper.Verify(() => { @@ -139,6 +138,22 @@ namespace MathNet.Numerics.UnitTests.InterpolationTests }); } + private Test CreateInitChecksForMinimumCountTest(string name) + { + return new TestCase(name, () => + { + double[] pointsOk, valuesOk; + SampleFunctionEquidistant(t => 5 + 10 * t, -2.0, 8.0, MinimumSampleCount, out pointsOk, out valuesOk); + + Assert.DoesNotThrow(() => Factory(pointsOk, valuesOk)); + + double[] pointsFail, valuesFail; + SampleFunctionEquidistant(t => 5 + 10 * t, -2.0, 8.0, MinimumSampleCount - 1, out pointsFail, out valuesFail); + + Assert.Throws(typeof(ArgumentOutOfRangeException), () => Factory(pointsFail, valuesFail)); + }); + } + private Test CreateConstructorInitShortcutTest(string name) { return new TestCase(name, () => @@ -162,7 +177,10 @@ namespace MathNet.Numerics.UnitTests.InterpolationTests { return new TestCase(name, () => { - var interpolation = Factory(new List { 1, 2, 3 }, new List { 10, 20, 30 }); + double[] points, values; + SampleFunctionEquidistant(t => 5 + 10 * t, -2.0, 8.0, MinimumSampleCount, out points, out values); + + var interpolation = Factory(points, values); // verify consistent differentiation capability if (interpolation.SupportsDifferentiation) @@ -224,9 +242,11 @@ namespace MathNet.Numerics.UnitTests.InterpolationTests const double xOffset = 4.0; Random random = new Random(); - for (int k = 0; k < Order.Length; k++) + int[] orders = { MinimumSampleCount, MinimumSampleCount + 1, MinimumSampleCount + 5 }; + + for (int k = 0; k < orders.Length; k++) { - int order = Order[k]; + int order = orders[k]; // build linear samples double[] points = new double[order]; @@ -334,6 +354,18 @@ namespace MathNet.Numerics.UnitTests.InterpolationTests points = new double[samples]; values = new double[samples]; + if(samples == 0) + { + return; + } + + if(samples == 1) + { + double t = points[0] = 0.5 * (start + stop); + values[0] = f(t); + return; + } + double step = (stop - start) / (samples - 1); for (int i = 0; i < points.Length; i++) { diff --git a/src/Managed.UnitTests/InterpolationTests/InterpolationTest.cs b/src/Managed.UnitTests/InterpolationTests/InterpolationTest.cs index 84e2bf64..28667e62 100644 --- a/src/Managed.UnitTests/InterpolationTests/InterpolationTest.cs +++ b/src/Managed.UnitTests/InterpolationTests/InterpolationTest.cs @@ -43,7 +43,7 @@ namespace MathNet.Numerics.UnitTests.InterpolationTests public readonly IContract NevillePolynomialContractTests = new InterpolationContract() { Factory = (t, x) => new NevillePolynomialInterpolation(t, x), - Order = new[] { 1, 2, 6 }, + MinimumSampleCount = 1, LinearBehavior = true, PolynomialBehavior = true, RationalBehavior = false @@ -53,7 +53,7 @@ namespace MathNet.Numerics.UnitTests.InterpolationTests public readonly IContract BulirschStoerRationalContractTests = new InterpolationContract() { Factory = Interpolate.RationalWithPoles, - Order = new[] { 1, 2, 6 }, + MinimumSampleCount = 1, LinearBehavior = false, PolynomialBehavior = true, RationalBehavior = true @@ -65,7 +65,7 @@ namespace MathNet.Numerics.UnitTests.InterpolationTests public readonly IContract BarycentricContractTests = new InterpolationContract() { Factory = (t, x) => new BarycentricInterpolation(t, x, FloaterHormannRationalInterpolation.EvaluateBarycentricWeights(t, x, 2)), - Order = new[] { 1, 2, 6 }, + MinimumSampleCount = 3, NonStandardParameters = true, }; @@ -73,7 +73,7 @@ namespace MathNet.Numerics.UnitTests.InterpolationTests public readonly IContract FloaterHormannRationalContractTests = new InterpolationContract() { Factory = Interpolate.RationalWithoutPoles, - Order = new[] { 1, 2, 6 }, + MinimumSampleCount = 1, LinearBehavior = true, PolynomialBehavior = true, RationalBehavior = true @@ -85,7 +85,7 @@ namespace MathNet.Numerics.UnitTests.InterpolationTests public readonly IContract SplineContractTests = new InterpolationContract() { Factory = (t, x) => new SplineInterpolation(t, LinearSplineInterpolation.EvaluateSplineCoefficients(t, x)), - Order = new[] { 1, 2, 6 }, + MinimumSampleCount = 2, NonStandardParameters = true, }; @@ -93,7 +93,7 @@ namespace MathNet.Numerics.UnitTests.InterpolationTests public readonly IContract CubicHermiteSplineContractTests = new InterpolationContract() { Factory = (t, x) => new CubicHermiteSplineInterpolation(t, x, CubicSplineInterpolation.EvaluateSplineDerivatives(t, x, SplineBoundaryCondition.Natural, 0.0, SplineBoundaryCondition.Natural, 0.0)), - Order = new[] { 1, 2, 6 }, + MinimumSampleCount = 2, NonStandardParameters = true, }; @@ -101,7 +101,7 @@ namespace MathNet.Numerics.UnitTests.InterpolationTests public readonly IContract LinearSplineContractTests = new InterpolationContract() { Factory = Interpolate.LinearBetweenPoints, - Order = new[] { 2, 3, 6 }, + MinimumSampleCount = 2, LinearBehavior = true, PolynomialBehavior = false, RationalBehavior = false @@ -111,7 +111,7 @@ namespace MathNet.Numerics.UnitTests.InterpolationTests public readonly IContract CubicSplineContractTests = new InterpolationContract() { Factory = (t, x) => new CubicSplineInterpolation(t, x), - Order = new[] { 2, 3, 6 }, + MinimumSampleCount = 2, LinearBehavior = true, PolynomialBehavior = false, RationalBehavior = false diff --git a/src/Managed/Interpolation/Algorithms/BulirschStoerRationalInterpolation.cs b/src/Managed/Interpolation/Algorithms/BulirschStoerRationalInterpolation.cs index 719ce499..2d5ff753 100644 --- a/src/Managed/Interpolation/Algorithms/BulirschStoerRationalInterpolation.cs +++ b/src/Managed/Interpolation/Algorithms/BulirschStoerRationalInterpolation.cs @@ -108,6 +108,11 @@ namespace MathNet.Numerics.Interpolation.Algorithms throw new ArgumentNullException("sampleValues"); } + if (samplePoints.Count < 1) + { + throw new ArgumentOutOfRangeException("samplePoints"); + } + if (samplePoints.Count != sampleValues.Count) { throw new ArgumentException(Properties.Resources.ArgumentVectorsSameLengths); diff --git a/src/Managed/Interpolation/Algorithms/NevillePolynomialInterpolation.cs b/src/Managed/Interpolation/Algorithms/NevillePolynomialInterpolation.cs index bcb4f00e..f38ae4a6 100644 --- a/src/Managed/Interpolation/Algorithms/NevillePolynomialInterpolation.cs +++ b/src/Managed/Interpolation/Algorithms/NevillePolynomialInterpolation.cs @@ -113,6 +113,11 @@ namespace MathNet.Numerics.Interpolation.Algorithms throw new ArgumentNullException("sampleValues"); } + if (samplePoints.Count < 1) + { + throw new ArgumentOutOfRangeException("samplePoints"); + } + if (samplePoints.Count != sampleValues.Count) { throw new ArgumentException(Properties.Resources.ArgumentVectorsSameLengths);