From 45ba168a588dee1d810501a5a3c3e9da1d63ebb5 Mon Sep 17 00:00:00 2001 From: Christoph Ruegg Date: Mon, 6 Jul 2009 22:47:12 +0800 Subject: [PATCH] interpolation test: added rational behavior contract Signed-off-by: Christoph Ruegg --- .../InterpolationContract.cs | 57 +++++++++++++++++++ .../InterpolationTests/InterpolationTest.cs | 9 ++- 2 files changed, 63 insertions(+), 3 deletions(-) diff --git a/src/Managed.UnitTests/InterpolationTests/InterpolationContract.cs b/src/Managed.UnitTests/InterpolationTests/InterpolationContract.cs index 92bd04ed..43a283be 100644 --- a/src/Managed.UnitTests/InterpolationTests/InterpolationContract.cs +++ b/src/Managed.UnitTests/InterpolationTests/InterpolationContract.cs @@ -41,6 +41,7 @@ namespace MathNet.Numerics.UnitTests.InterpolationTests public Func, IList, IInterpolation> Factory { get; set; } public int[] Order { get; set; } public bool PolynomialBehavior { get; set; } + public bool RationalBehavior { get; set; } public InterpolationContract() { @@ -58,6 +59,11 @@ namespace MathNet.Numerics.UnitTests.InterpolationTests { yield return CreatePolynomialBehaviorTest("PolynomialBehavior"); } + + if (RationalBehavior) + { + yield return CreateRationalBehaviorTest("RationalBehavior"); + } } private Test CreateFactoryReturnsCorrectTypeTest(string name) @@ -216,5 +222,56 @@ namespace MathNet.Numerics.UnitTests.InterpolationTests Assert.AreApproximatelyEqual(-5071.0, interpolation.Interpolate(-10.0), 1e-6, "A -10.0"); }); } + + private Test CreateRationalBehaviorTest(string name) + { + return new TestCase(name, () => + { + double[] points, values; + SampleFunctionEquidistant(t => 1 / (1 + (t * t)), -5.0, 5.0, 41, out points, out values); + var interpolation = Factory(points, values); + + for (int i = 0; i < points.Length; i++) + { + Assert.AreApproximatelyEqual( + values[i], + interpolation.Interpolate(points[i]), + 1e-12, + "Match on knots"); + } + + double[] testPoints, testValues; + SampleFunctionEquidistant(t => 1 / (1 + (t * t)), -5.0, 5.0, 81, out testPoints, out testValues); + + for (int i = 0; i < testPoints.Length; i++) + { + Assert.AreApproximatelyEqual( + testValues[i], + interpolation.Interpolate(testPoints[i]), + 1e-5, + "Match between knots"); + } + }); + } + + private static void SampleFunctionEquidistant( + Func f, + double start, + double stop, + int samples, + out double[] points, + out double[] values) + { + points = new double[samples]; + values = new double[samples]; + + double step = (stop - start) / (samples - 1); + for (int i = 0; i < points.Length; i++) + { + double t = start + (i * step); + points[i] = t; + values[i] = f(t); + } + } } } diff --git a/src/Managed.UnitTests/InterpolationTests/InterpolationTest.cs b/src/Managed.UnitTests/InterpolationTests/InterpolationTest.cs index b7d36a4e..12070189 100644 --- a/src/Managed.UnitTests/InterpolationTests/InterpolationTest.cs +++ b/src/Managed.UnitTests/InterpolationTests/InterpolationTest.cs @@ -42,7 +42,8 @@ namespace MathNet.Numerics.UnitTests.InterpolationTests { Factory = Interpolation.CreateLinearBetweenPoints, Order = new[] { 2, 3, 6 }, - PolynomialBehavior = false + PolynomialBehavior = false, + RationalBehavior = false }; [VerifyContract] @@ -50,7 +51,8 @@ namespace MathNet.Numerics.UnitTests.InterpolationTests { Factory = Interpolation.CreateRationalPoleFree, Order = new[] { 1, 2, 6 }, - PolynomialBehavior = true + PolynomialBehavior = true, + RationalBehavior = true }; [VerifyContract] @@ -58,7 +60,8 @@ namespace MathNet.Numerics.UnitTests.InterpolationTests { Factory = (t, x) => new NevillePolynomialInterpolation(t, x), Order = new[] { 1, 2, 6 }, - PolynomialBehavior = true + PolynomialBehavior = true, + RationalBehavior = false }; } }