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 where TInterpolation : IInterpolation
{ {
public Func<IList<double>, IList<double>, IInterpolation> Factory { get; set; } 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 NonStandardParameters { get; set; }
public bool LinearBehavior { get; set; } public bool LinearBehavior { get; set; }
public bool PolynomialBehavior { get; set; } public bool PolynomialBehavior { get; set; }
public bool RationalBehavior { get; set; } public bool RationalBehavior { get; set; }
public InterpolationContract()
{
Order = new[] { 4 };
}
protected override IEnumerable<Test> GetContractVerificationTests() protected override IEnumerable<Test> GetContractVerificationTests()
{ {
// Infrastructure Tests // Infrastructure Tests
@ -59,6 +54,7 @@ namespace MathNet.Numerics.UnitTests.InterpolationTests
yield return CreateInitChecksForNullTest("InitChecksForNull"); yield return CreateInitChecksForNullTest("InitChecksForNull");
yield return CreateInitChecksForMatchingCountTest("InitChecksForMatchingCount"); yield return CreateInitChecksForMatchingCountTest("InitChecksForMatchingCount");
yield return CreateInitChecksForMinimumCountTest("InitChecksForMinimumCount");
if (!NonStandardParameters) if (!NonStandardParameters)
{ {
@ -89,7 +85,10 @@ namespace MathNet.Numerics.UnitTests.InterpolationTests
{ {
return new TestCase(name, () => 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(() => 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) private Test CreateConstructorInitShortcutTest(string name)
{ {
return new TestCase(name, () => return new TestCase(name, () =>
@ -162,7 +177,10 @@ namespace MathNet.Numerics.UnitTests.InterpolationTests
{ {
return new TestCase(name, () => 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 // verify consistent differentiation capability
if (interpolation.SupportsDifferentiation) if (interpolation.SupportsDifferentiation)
@ -224,9 +242,11 @@ namespace MathNet.Numerics.UnitTests.InterpolationTests
const double xOffset = 4.0; const double xOffset = 4.0;
Random random = new Random(); 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 // build linear samples
double[] points = new double[order]; double[] points = new double[order];
@ -334,6 +354,18 @@ namespace MathNet.Numerics.UnitTests.InterpolationTests
points = new double[samples]; points = new double[samples];
values = 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); double step = (stop - start) / (samples - 1);
for (int i = 0; i < points.Length; i++) 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>() public readonly IContract NevillePolynomialContractTests = new InterpolationContract<NevillePolynomialInterpolation>()
{ {
Factory = (t, x) => new NevillePolynomialInterpolation(t, x), Factory = (t, x) => new NevillePolynomialInterpolation(t, x),
Order = new[] { 1, 2, 6 }, MinimumSampleCount = 1,
LinearBehavior = true, LinearBehavior = true,
PolynomialBehavior = true, PolynomialBehavior = true,
RationalBehavior = false RationalBehavior = false
@ -53,7 +53,7 @@ namespace MathNet.Numerics.UnitTests.InterpolationTests
public readonly IContract BulirschStoerRationalContractTests = new InterpolationContract<BulirschStoerRationalInterpolation>() public readonly IContract BulirschStoerRationalContractTests = new InterpolationContract<BulirschStoerRationalInterpolation>()
{ {
Factory = Interpolate.RationalWithPoles, Factory = Interpolate.RationalWithPoles,
Order = new[] { 1, 2, 6 }, MinimumSampleCount = 1,
LinearBehavior = false, LinearBehavior = false,
PolynomialBehavior = true, PolynomialBehavior = true,
RationalBehavior = true RationalBehavior = true
@ -65,7 +65,7 @@ namespace MathNet.Numerics.UnitTests.InterpolationTests
public readonly IContract BarycentricContractTests = new InterpolationContract<BarycentricInterpolation>() public readonly IContract BarycentricContractTests = new InterpolationContract<BarycentricInterpolation>()
{ {
Factory = (t, x) => new BarycentricInterpolation(t, x, FloaterHormannRationalInterpolation.EvaluateBarycentricWeights(t, x, 2)), Factory = (t, x) => new BarycentricInterpolation(t, x, FloaterHormannRationalInterpolation.EvaluateBarycentricWeights(t, x, 2)),
Order = new[] { 1, 2, 6 }, MinimumSampleCount = 3,
NonStandardParameters = true, NonStandardParameters = true,
}; };
@ -73,7 +73,7 @@ namespace MathNet.Numerics.UnitTests.InterpolationTests
public readonly IContract FloaterHormannRationalContractTests = new InterpolationContract<FloaterHormannRationalInterpolation>() public readonly IContract FloaterHormannRationalContractTests = new InterpolationContract<FloaterHormannRationalInterpolation>()
{ {
Factory = Interpolate.RationalWithoutPoles, Factory = Interpolate.RationalWithoutPoles,
Order = new[] { 1, 2, 6 }, MinimumSampleCount = 1,
LinearBehavior = true, LinearBehavior = true,
PolynomialBehavior = true, PolynomialBehavior = true,
RationalBehavior = true RationalBehavior = true
@ -85,7 +85,7 @@ namespace MathNet.Numerics.UnitTests.InterpolationTests
public readonly IContract SplineContractTests = new InterpolationContract<SplineInterpolation>() public readonly IContract SplineContractTests = new InterpolationContract<SplineInterpolation>()
{ {
Factory = (t, x) => new SplineInterpolation(t, LinearSplineInterpolation.EvaluateSplineCoefficients(t, x)), Factory = (t, x) => new SplineInterpolation(t, LinearSplineInterpolation.EvaluateSplineCoefficients(t, x)),
Order = new[] { 1, 2, 6 }, MinimumSampleCount = 2,
NonStandardParameters = true, NonStandardParameters = true,
}; };
@ -93,7 +93,7 @@ namespace MathNet.Numerics.UnitTests.InterpolationTests
public readonly IContract CubicHermiteSplineContractTests = new InterpolationContract<CubicHermiteSplineInterpolation>() 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)), 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, NonStandardParameters = true,
}; };
@ -101,7 +101,7 @@ namespace MathNet.Numerics.UnitTests.InterpolationTests
public readonly IContract LinearSplineContractTests = new InterpolationContract<LinearSplineInterpolation>() public readonly IContract LinearSplineContractTests = new InterpolationContract<LinearSplineInterpolation>()
{ {
Factory = Interpolate.LinearBetweenPoints, Factory = Interpolate.LinearBetweenPoints,
Order = new[] { 2, 3, 6 }, MinimumSampleCount = 2,
LinearBehavior = true, LinearBehavior = true,
PolynomialBehavior = false, PolynomialBehavior = false,
RationalBehavior = false RationalBehavior = false
@ -111,7 +111,7 @@ namespace MathNet.Numerics.UnitTests.InterpolationTests
public readonly IContract CubicSplineContractTests = new InterpolationContract<CubicSplineInterpolation>() public readonly IContract CubicSplineContractTests = new InterpolationContract<CubicSplineInterpolation>()
{ {
Factory = (t, x) => new CubicSplineInterpolation(t, x), Factory = (t, x) => new CubicSplineInterpolation(t, x),
Order = new[] { 2, 3, 6 }, MinimumSampleCount = 2,
LinearBehavior = true, LinearBehavior = true,
PolynomialBehavior = false, PolynomialBehavior = false,
RationalBehavior = false RationalBehavior = false

5
src/Managed/Interpolation/Algorithms/BulirschStoerRationalInterpolation.cs

@ -108,6 +108,11 @@ namespace MathNet.Numerics.Interpolation.Algorithms
throw new ArgumentNullException("sampleValues"); throw new ArgumentNullException("sampleValues");
} }
if (samplePoints.Count < 1)
{
throw new ArgumentOutOfRangeException("samplePoints");
}
if (samplePoints.Count != sampleValues.Count) if (samplePoints.Count != sampleValues.Count)
{ {
throw new ArgumentException(Properties.Resources.ArgumentVectorsSameLengths); 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"); throw new ArgumentNullException("sampleValues");
} }
if (samplePoints.Count < 1)
{
throw new ArgumentOutOfRangeException("samplePoints");
}
if (samplePoints.Count != sampleValues.Count) if (samplePoints.Count != sampleValues.Count)
{ {
throw new ArgumentException(Properties.Resources.ArgumentVectorsSameLengths); throw new ArgumentException(Properties.Resources.ArgumentVectorsSameLengths);

Loading…
Cancel
Save