Browse Source

sampling: applying random sampling to unit tests

Signed-off-by: Christoph Ruegg <git@cdrnet.ch>
pull/2/head
Christoph Ruegg 17 years ago
parent
commit
adcd527722
  1. 1
      src/MathNet.Numerics.4.5.resharper
  2. 15
      src/UnitTests/IntegralTransformsTests/FourierTest.cs
  3. 10
      src/UnitTests/IntegralTransformsTests/HartleyTest.cs
  4. 16
      src/UnitTests/IntegralTransformsTests/InverseTransformTest.cs
  5. 11
      src/UnitTests/IntegralTransformsTests/MatchingNaiveTransformTest.cs
  6. 15
      src/UnitTests/IntegralTransformsTests/ParsevalTheoremTest.cs
  7. 61
      src/UnitTests/IntegralTransformsTests/SampleProvider.cs
  8. 14
      src/UnitTests/IntegrationTests/IntegrationTest.cs
  9. 1
      src/UnitTests/UnitTests.csproj

1
src/MathNet.Numerics.4.5.resharper

@ -841,6 +841,7 @@ Chebyshev</UserWords>
<Abbreviation Text="FFT" /> <Abbreviation Text="FFT" />
<Abbreviation Text="LU" /> <Abbreviation Text="LU" />
<Abbreviation Text="DC" /> <Abbreviation Text="DC" />
<Abbreviation Text="DFT" />
</Naming2> </Naming2>
</CodeStyleSettings> </CodeStyleSettings>
<SharedSolutionTemplateManager> <SharedSolutionTemplateManager>

15
src/UnitTests/IntegralTransformsTests/FourierTest.cs

@ -29,14 +29,17 @@
namespace MathNet.Numerics.UnitTests.IntegralTransformsTests namespace MathNet.Numerics.UnitTests.IntegralTransformsTests
{ {
using System; using System;
using MbUnit.Framework; using Distributions;
using IntegralTransforms; using IntegralTransforms;
using IntegralTransforms.Algorithms; using IntegralTransforms.Algorithms;
using MbUnit.Framework;
using Sampling; using Sampling;
[TestFixture] [TestFixture]
public class FourierTest public class FourierTest
{ {
private IContinuousDistribution _uniform = new ContinuousUniform(-1, 1);
[Test] [Test]
public void NaiveTransformsRealSineCorrectly() public void NaiveTransformsRealSineCorrectly()
{ {
@ -53,9 +56,9 @@ namespace MathNet.Numerics.UnitTests.IntegralTransformsTests
} }
// all imaginary components except second and last musth be zero // all imaginary components except second and last musth be zero
for(int i = 0; i<spectrum.Length; i++) for (int i = 0; i < spectrum.Length; i++)
{ {
if(i == 1) if (i == 1)
{ {
Assert.AreApproximatelyEqual(-8, spectrum[i].Imaginary, 1e-12, "imag second"); Assert.AreApproximatelyEqual(-8, spectrum[i].Imaginary, 1e-12, "imag second");
} }
@ -73,7 +76,7 @@ namespace MathNet.Numerics.UnitTests.IntegralTransformsTests
[Test] [Test]
public void Radix2ThrowsWhenNotPowerOfTwo() public void Radix2ThrowsWhenNotPowerOfTwo()
{ {
var samples = SampleProvider.ProvideComplexSamples(0x7F); var samples = Sample.Random((u, v) => new Complex(u, v), _uniform, 0x7F);
var dft = new DiscreteFourierTransform(); var dft = new DiscreteFourierTransform();
@ -86,7 +89,7 @@ namespace MathNet.Numerics.UnitTests.IntegralTransformsTests
() => dft.Radix2Inverse(samples, FourierOptions.Default)); () => dft.Radix2Inverse(samples, FourierOptions.Default));
Assert.Throws( Assert.Throws(
typeof (ArgumentException), typeof(ArgumentException),
() => DiscreteFourierTransform.Radix2(samples, -1)); () => DiscreteFourierTransform.Radix2(samples, -1));
Assert.Throws( Assert.Throws(
@ -94,4 +97,4 @@ namespace MathNet.Numerics.UnitTests.IntegralTransformsTests
() => DiscreteFourierTransform.Radix2Parallel(samples, -1)); () => DiscreteFourierTransform.Radix2Parallel(samples, -1));
} }
} }
} }

10
src/UnitTests/IntegralTransformsTests/HartleyTest.cs

@ -29,13 +29,17 @@
namespace MathNet.Numerics.UnitTests.IntegralTransformsTests namespace MathNet.Numerics.UnitTests.IntegralTransformsTests
{ {
using System; using System;
using MbUnit.Framework; using Distributions;
using IntegralTransforms; using IntegralTransforms;
using IntegralTransforms.Algorithms; using IntegralTransforms.Algorithms;
using MbUnit.Framework;
using Sampling;
[TestFixture] [TestFixture]
public class HartleyTest public class HartleyTest
{ {
private IContinuousDistribution _uniform = new ContinuousUniform(-1, 1);
private static void VerifyMatchesDFT( private static void VerifyMatchesDFT(
double[] samples, double[] samples,
double maximumError, double maximumError,
@ -59,7 +63,7 @@ namespace MathNet.Numerics.UnitTests.IntegralTransformsTests
public void NaiveMatchesDFT(HartleyOptions hartleyOptions, FourierOptions fourierOptions) public void NaiveMatchesDFT(HartleyOptions hartleyOptions, FourierOptions fourierOptions)
{ {
var dht = new DiscreteHartleyTransform(); var dht = new DiscreteHartleyTransform();
var samples = SampleProvider.ProvideRealSamples(0x80); var samples = Sample.Random(x => x, _uniform, 0x80);
VerifyMatchesDFT( VerifyMatchesDFT(
samples, samples,
@ -76,4 +80,4 @@ namespace MathNet.Numerics.UnitTests.IntegralTransformsTests
s => dht.NaiveInverse(s, hartleyOptions)); s => dht.NaiveInverse(s, hartleyOptions));
} }
} }
} }

16
src/UnitTests/IntegralTransformsTests/InverseTransformTest.cs

@ -29,20 +29,24 @@
namespace MathNet.Numerics.UnitTests.IntegralTransformsTests namespace MathNet.Numerics.UnitTests.IntegralTransformsTests
{ {
using System; using System;
using Distributions;
using IntegralTransforms; using IntegralTransforms;
using IntegralTransforms.Algorithms; using IntegralTransforms.Algorithms;
using MbUnit.Framework; using MbUnit.Framework;
using Sampling;
[TestFixture] [TestFixture]
public class InverseTransformTest public class InverseTransformTest
{ {
private static void VerifyIsReversibleComplex( private IContinuousDistribution _uniform = new ContinuousUniform(-1, 1);
private void VerifyIsReversibleComplex(
int count, int count,
double maximumError, double maximumError,
Func<Complex[], Complex[]> forward, Func<Complex[], Complex[]> forward,
Func<Complex[], Complex[]> inverse) Func<Complex[], Complex[]> inverse)
{ {
var samples = SampleProvider.ProvideComplexSamples(count); var samples = Sample.Random((u, v) => new Complex(u, v), _uniform, count);
var work = new Complex[samples.Length]; var work = new Complex[samples.Length];
samples.CopyTo(work, 0); samples.CopyTo(work, 0);
@ -55,13 +59,13 @@ namespace MathNet.Numerics.UnitTests.IntegralTransformsTests
AssertHelpers.AlmostEqualList(samples, work, maximumError); AssertHelpers.AlmostEqualList(samples, work, maximumError);
} }
private static void VerifyIsReversibleReal( private void VerifyIsReversibleReal(
int count, int count,
double maximumError, double maximumError,
Func<double[], double[]> forward, Func<double[], double[]> forward,
Func<double[], double[]> inverse) Func<double[], double[]> inverse)
{ {
var samples = SampleProvider.ProvideRealSamples(count); var samples = Sample.Random(x => x, _uniform, count);
var work = new double[samples.Length]; var work = new double[samples.Length];
samples.CopyTo(work, 0); samples.CopyTo(work, 0);
@ -149,7 +153,7 @@ namespace MathNet.Numerics.UnitTests.IntegralTransformsTests
[Test] [Test]
public void FourierDefaultTransformIsReversible() public void FourierDefaultTransformIsReversible()
{ {
var samples = SampleProvider.ProvideComplexSamples(0x7FFF); var samples = Sample.Random((u, v) => new Complex(u, v), _uniform, 0x7FFF);
var work = new Complex[samples.Length]; var work = new Complex[samples.Length];
samples.CopyTo(work, 0); samples.CopyTo(work, 0);
@ -170,4 +174,4 @@ namespace MathNet.Numerics.UnitTests.IntegralTransformsTests
AssertHelpers.AlmostEqualList(samples, work, 1e-12); AssertHelpers.AlmostEqualList(samples, work, 1e-12);
} }
} }
} }

11
src/UnitTests/IntegralTransformsTests/MatchingNaiveTransformTest.cs

@ -29,6 +29,7 @@
namespace MathNet.Numerics.UnitTests.IntegralTransformsTests namespace MathNet.Numerics.UnitTests.IntegralTransformsTests
{ {
using System; using System;
using Distributions;
using IntegralTransforms; using IntegralTransforms;
using IntegralTransforms.Algorithms; using IntegralTransforms.Algorithms;
using MbUnit.Framework; using MbUnit.Framework;
@ -37,6 +38,8 @@ namespace MathNet.Numerics.UnitTests.IntegralTransformsTests
[TestFixture] [TestFixture]
public class MatchingNaiveTransformTest public class MatchingNaiveTransformTest
{ {
private IContinuousDistribution _uniform = new ContinuousUniform(-1, 1);
private static void VerifyMatchesNaiveComplex( private static void VerifyMatchesNaiveComplex(
Complex[] samples, Complex[] samples,
double maximumError, double maximumError,
@ -81,7 +84,7 @@ namespace MathNet.Numerics.UnitTests.IntegralTransformsTests
public void FourierRadix2MatchesNaiveOnRandom(FourierOptions options) public void FourierRadix2MatchesNaiveOnRandom(FourierOptions options)
{ {
var dft = new DiscreteFourierTransform(); var dft = new DiscreteFourierTransform();
var samples = SampleProvider.ProvideComplexSamples(0x80); var samples = Sample.Random((u, v) => new Complex(u, v), _uniform, 0x80);
VerifyMatchesNaiveComplex( VerifyMatchesNaiveComplex(
samples, samples,
@ -125,7 +128,7 @@ namespace MathNet.Numerics.UnitTests.IntegralTransformsTests
public void FourierBluesteinMatchesNaiveOnRandomPowerOfTwo(FourierOptions options) public void FourierBluesteinMatchesNaiveOnRandomPowerOfTwo(FourierOptions options)
{ {
var dft = new DiscreteFourierTransform(); var dft = new DiscreteFourierTransform();
var samples = SampleProvider.ProvideComplexSamples(0x80); var samples = Sample.Random((u, v) => new Complex(u, v), _uniform, 0x80);
VerifyMatchesNaiveComplex( VerifyMatchesNaiveComplex(
samples, samples,
@ -147,7 +150,7 @@ namespace MathNet.Numerics.UnitTests.IntegralTransformsTests
public void FourierBluesteinMatchesNaiveOnRandomNonPowerOfTwo(FourierOptions options) public void FourierBluesteinMatchesNaiveOnRandomNonPowerOfTwo(FourierOptions options)
{ {
var dft = new DiscreteFourierTransform(); var dft = new DiscreteFourierTransform();
var samples = SampleProvider.ProvideComplexSamples(0x7F); var samples = Sample.Random((u, v) => new Complex(u, v), _uniform, 0x7F);
VerifyMatchesNaiveComplex( VerifyMatchesNaiveComplex(
samples, samples,
@ -162,4 +165,4 @@ namespace MathNet.Numerics.UnitTests.IntegralTransformsTests
s => dft.BluesteinInverse(s, options)); s => dft.BluesteinInverse(s, options));
} }
} }
} }

15
src/UnitTests/IntegralTransformsTests/ParsevalTheoremTest.cs

@ -26,24 +26,27 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using MathNet.Numerics.IntegralTransforms.Algorithms;
namespace MathNet.Numerics.UnitTests.IntegralTransformsTests namespace MathNet.Numerics.UnitTests.IntegralTransformsTests
{ {
using System.Linq; using System.Linq;
using MbUnit.Framework; using Distributions;
using IntegralTransforms; using IntegralTransforms;
using IntegralTransforms.Algorithms;
using MbUnit.Framework;
using Sampling;
using Statistics; using Statistics;
[TestFixture] [TestFixture]
public class ParsevalTheoremTest public class ParsevalTheoremTest
{ {
private IContinuousDistribution _uniform = new ContinuousUniform(-1, 1);
[Test] [Test]
[Row(0x1000)] [Row(0x1000)]
[Row(0x7FF)] [Row(0x7FF)]
public void FourierDefaultTransformSatisfiesParsevalsTheorem(int count) public void FourierDefaultTransformSatisfiesParsevalsTheorem(int count)
{ {
var samples = SampleProvider.ProvideComplexSamples(count); var samples = Sample.Random((u, v) => new Complex(u, v), _uniform, count);
var timeSpaceEnergy = (from s in samples select s.ModulusSquared).Mean(); var timeSpaceEnergy = (from s in samples select s.ModulusSquared).Mean();
@ -63,7 +66,7 @@ namespace MathNet.Numerics.UnitTests.IntegralTransformsTests
[Row(0x1F)] [Row(0x1F)]
public void HartleyDefaultNaiveSatisfiesParsevalsTheorem(int count) public void HartleyDefaultNaiveSatisfiesParsevalsTheorem(int count)
{ {
var samples = SampleProvider.ProvideRealSamples(count); var samples = Sample.Random(x => x, _uniform, count);
var timeSpaceEnergy = (from s in samples select s * s).Mean(); var timeSpaceEnergy = (from s in samples select s * s).Mean();
@ -79,4 +82,4 @@ namespace MathNet.Numerics.UnitTests.IntegralTransformsTests
Assert.AreApproximatelyEqual(timeSpaceEnergy, frequencySpaceEnergy, 1e-12); Assert.AreApproximatelyEqual(timeSpaceEnergy, frequencySpaceEnergy, 1e-12);
} }
} }
} }

61
src/UnitTests/IntegralTransformsTests/SampleProvider.cs

@ -1,61 +0,0 @@
// <copyright file="SampleProvider.cs" company="Math.NET">
// Math.NET Numerics, part of the Math.NET Project
// http://mathnet.opensourcedotnet.info
//
// Copyright (c) 2009 Math.NET
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
namespace MathNet.Numerics.UnitTests.IntegralTransformsTests
{
using System;
internal static class SampleProvider
{
private static readonly Random _random = new Random();
internal static Complex[] ProvideComplexSamples(int count)
{
var samples = new Complex[count];
for (int i = 0; i < samples.Length; i++)
{
samples[i] = Complex.WithRealImaginary(
1 - (2 * _random.NextDouble()),
1 - (2 * _random.NextDouble()));
}
return samples;
}
internal static double[] ProvideRealSamples(int count)
{
var samples = new double[count];
for (int i = 0; i < samples.Length; i++)
{
samples[i] = 1 - (2 * _random.NextDouble());
}
return samples;
}
}
}

14
src/UnitTests/IntegrationTests/IntegrationTest.cs

@ -89,7 +89,8 @@ namespace MathNet.Numerics.UnitTests.IntegrationTests
TargetAreaA, TargetAreaA,
algorithm.Integrate(TargetFunctionA, StartA, StopA, targetRelativeError), algorithm.Integrate(TargetFunctionA, StartA, StopA, targetRelativeError),
targetRelativeError * TargetAreaA, targetRelativeError * TargetAreaA,
"DET Adaptive {0}", targetRelativeError); "DET Adaptive {0}",
targetRelativeError);
} }
[VerifyContract] [VerifyContract]
@ -121,7 +122,8 @@ namespace MathNet.Numerics.UnitTests.IntegrationTests
TargetAreaA, TargetAreaA,
NewtonCotesTrapeziumRule.IntegrateComposite(TargetFunctionA, StartA, StopA, partitions), NewtonCotesTrapeziumRule.IntegrateComposite(TargetFunctionA, StartA, StopA, partitions),
maxRelativeError * TargetAreaA, maxRelativeError * TargetAreaA,
"Composite {0} Partitions", partitions); "Composite {0} Partitions",
partitions);
} }
[Test] [Test]
@ -134,7 +136,8 @@ namespace MathNet.Numerics.UnitTests.IntegrationTests
TargetAreaA, TargetAreaA,
NewtonCotesTrapeziumRule.IntegrateAdaptive(TargetFunctionA, StartA, StopA, targetRelativeError), NewtonCotesTrapeziumRule.IntegrateAdaptive(TargetFunctionA, StartA, StopA, targetRelativeError),
targetRelativeError * TargetAreaA, targetRelativeError * TargetAreaA,
"Adaptive {0}", targetRelativeError); "Adaptive {0}",
targetRelativeError);
} }
[VerifyContract] [VerifyContract]
@ -181,7 +184,8 @@ namespace MathNet.Numerics.UnitTests.IntegrationTests
TargetAreaA, TargetAreaA,
SimpsonRule.IntegrateComposite(TargetFunctionA, StartA, StopA, partitions), SimpsonRule.IntegrateComposite(TargetFunctionA, StartA, StopA, partitions),
maxRelativeError * TargetAreaA, maxRelativeError * TargetAreaA,
"Composite {0} Partitions", partitions); "Composite {0} Partitions",
partitions);
} }
[VerifyContract] [VerifyContract]
@ -202,4 +206,4 @@ namespace MathNet.Numerics.UnitTests.IntegrationTests
} }
}; };
} }
} }

1
src/UnitTests/UnitTests.csproj

@ -74,7 +74,6 @@
<Compile Include="IntegralTransformsTests\MatchingNaiveTransformTest.cs" /> <Compile Include="IntegralTransformsTests\MatchingNaiveTransformTest.cs" />
<Compile Include="IntegralTransformsTests\ParsevalTheoremTest.cs" /> <Compile Include="IntegralTransformsTests\ParsevalTheoremTest.cs" />
<Compile Include="IntegralTransformsTests\InverseTransformTest.cs" /> <Compile Include="IntegralTransformsTests\InverseTransformTest.cs" />
<Compile Include="IntegralTransformsTests\SampleProvider.cs" />
<Compile Include="IntegrationTests\IntegrationTest.cs" /> <Compile Include="IntegrationTests\IntegrationTest.cs" />
<Compile Include="FunctionalHelpers.cs" /> <Compile Include="FunctionalHelpers.cs" />
<Compile Include="InterpolationTests\InterpolationInfrastuctureTest.cs" /> <Compile Include="InterpolationTests\InterpolationInfrastuctureTest.cs" />

Loading…
Cancel
Save