Browse Source

interpolation test: now verifying minimum sample count check

Signed-off-by: Christoph Ruegg <git@cdrnet.ch>
pull/2/head
Christoph Ruegg 17 years ago
parent
commit
0478e36a66
  1. 52
      src/Managed.UnitTests/InterpolationTests/InterpolationContract.cs
  2. 16
      src/Managed.UnitTests/InterpolationTests/InterpolationTest.cs
  3. 5
      src/Managed/Interpolation/Algorithms/BulirschStoerRationalInterpolation.cs
  4. 5
      src/Managed/Interpolation/Algorithms/NevillePolynomialInterpolation.cs

52
src/Managed.UnitTests/InterpolationTests/InterpolationContract.cs

@ -39,17 +39,12 @@ namespace MathNet.Numerics.UnitTests.InterpolationTests
where TInterpolation : IInterpolation
{
public Func<IList<double>, IList<double>, 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<Test> 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<double> { 1, 2, 3 }, new List<double> { 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<double> { 1, 2, 3 }, new List<double> { 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++)
{

16
src/Managed.UnitTests/InterpolationTests/InterpolationTest.cs

@ -43,7 +43,7 @@ namespace MathNet.Numerics.UnitTests.InterpolationTests
public readonly IContract NevillePolynomialContractTests = new InterpolationContract<NevillePolynomialInterpolation>()
{
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<BulirschStoerRationalInterpolation>()
{
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<BarycentricInterpolation>()
{
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<FloaterHormannRationalInterpolation>()
{
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<SplineInterpolation>()
{
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<CubicHermiteSplineInterpolation>()
{
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<LinearSplineInterpolation>()
{
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<CubicSplineInterpolation>()
{
Factory = (t, x) => new CubicSplineInterpolation(t, x),
Order = new[] { 2, 3, 6 },
MinimumSampleCount = 2,
LinearBehavior = true,
PolynomialBehavior = false,
RationalBehavior = false

5
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);

5
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);

Loading…
Cancel
Save