diff --git a/src/FSharpUnitTests/Program.fs b/src/FSharpUnitTests/Program.fs index 33981fe7..3d1e98d3 100644 --- a/src/FSharpUnitTests/Program.fs +++ b/src/FSharpUnitTests/Program.fs @@ -13,17 +13,15 @@ let DenseVectorTests = specs "DenseVector" [ spec "DenseVector.init" - (Double.DenseVector.init 100 (fun i -> float i / 100.0) |> should approximately_vector_equal 16 largev) + (Double.DenseVector.init 100 (fun i -> float i / 100.0) |> should equal largev) spec "DenseVector.of_list" - (Double.DenseVector.of_list [ for i in 0 .. 99 -> float i / 100.0 ] |> should approximately_vector_equal 16 largev) + (Double.DenseVector.of_list [ for i in 0 .. 99 -> float i / 100.0 ] |> should equal largev) spec "DenseVector.of_seq" - (Double.DenseVector.of_seq (seq { for i in 0 .. 99 -> float i / 100.0 }) |> should approximately_vector_equal 16 largev) + (Double.DenseVector.of_seq (seq { for i in 0 .. 99 -> float i / 100.0 }) |> should equal largev) spec "DenseVector.rangef" - (Double.DenseVector.rangef 0.0 0.01 0.99 - |> should approximately_vector_equal 16 (new Double.DenseVector( [| for i in 0 .. 99 -> 0.01 * float i |] ) )) + (Double.DenseVector.rangef 0.0 0.01 0.99 |> should equal (new Double.DenseVector( [| for i in 0 .. 99 -> 0.01 * float i |] ) )) spec "DenseVector.range" - (Double.DenseVector.range 0 99 - |> should approximately_vector_equal 16 (new Double.DenseVector( [| for i in 0 .. 99 -> float i |] ) )) + (Double.DenseVector.range 0 99 |> should equal (new Double.DenseVector( [| for i in 0 .. 99 -> float i |] ) )) ] /// Report on errors and success and exit. diff --git a/src/Numerics/Numerics.csproj b/src/Numerics/Numerics.csproj index 3897ebf2..bc812b2a 100644 --- a/src/Numerics/Numerics.csproj +++ b/src/Numerics/Numerics.csproj @@ -100,6 +100,7 @@ True Resources.resx + diff --git a/src/Numerics/Properties/Resources.Designer.cs b/src/Numerics/Properties/Resources.Designer.cs index 5228a1ce..112dd6f4 100644 --- a/src/Numerics/Properties/Resources.Designer.cs +++ b/src/Numerics/Properties/Resources.Designer.cs @@ -1,7 +1,7 @@ //------------------------------------------------------------------------------ // // This code was generated by a tool. -// Runtime Version:2.0.50727.4927 +// Runtime Version:2.0.50727.3074 // // Changes to this file may cause incorrect behavior and will be lost if // the code is regenerated. @@ -222,6 +222,15 @@ namespace MathNet.Numerics.Properties { } } + /// + /// Looks up a localized string similar to In the specified range, the minimum is greater than maximum.. + /// + internal static string ArgumentMinValueGreaterThanMaxValue { + get { + return ResourceManager.GetString("ArgumentMinValueGreaterThanMaxValue", resourceCulture); + } + } + /// /// Looks up a localized string similar to Value must be positive.. /// diff --git a/src/Numerics/Properties/Resources.resx b/src/Numerics/Properties/Resources.resx index 001aa37d..0686b769 100644 --- a/src/Numerics/Properties/Resources.resx +++ b/src/Numerics/Properties/Resources.resx @@ -264,4 +264,7 @@ A user defined provider has not been specified. + + In the specified range, the minimum is greater than maximum. + \ No newline at end of file diff --git a/src/Numerics/Random/AbstractRandomNumberGenerator.cs b/src/Numerics/Random/AbstractRandomNumberGenerator.cs new file mode 100644 index 00000000..147a8d59 --- /dev/null +++ b/src/Numerics/Random/AbstractRandomNumberGenerator.cs @@ -0,0 +1,194 @@ +// +// 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. +// + +namespace MathNet.Numerics.Random +{ + using System; + using System.Collections.Generic; + using Properties; + + /// + /// Abstract class for random number generators. This class introduces a layer between + /// and the Math.Net Numerics random number generators to provide thread safety. + /// + public abstract class AbstractRandomNumberGenerator : System.Random + { + /// + /// A delegate type that represents a method that generates random numbers. + /// + /// Randomly distributed numbers. + private delegate double SampleMethod(); + + /// + /// The method that actually generates samples. + /// + private readonly SampleMethod _sampleMethod; + + /// + /// The object that will be locked for thread safety. + /// + private readonly object _lock = new object(); + + + /// + /// Initializes a new instance of the class using + /// the value of to set whether + /// the instance is thread safe or not. + /// + protected AbstractRandomNumberGenerator(): this(Control.ThreadSafeRandomNumberGenerators) + { + + } + + /// + /// Initializes a new instance of the class. + /// + /// if set to true , the class is thread safe. + /// Thread safe instances are two and half times slower than non-thread + /// safe classes. + protected AbstractRandomNumberGenerator(bool threadSafe) + { + _sampleMethod = threadSafe ? (SampleMethod) ThreadSafeSample : DoSample; + } + + /// + /// Returns an array of uniformly distributed random doubles in the interval [0.0,1.0]. + /// + /// The size of the array. + /// + /// An array of uniformly distributed random doubles in the interval [0.0,1.0]. + /// + /// if n is not greater than 0. + public double[] NextDouble(int n) + { + if (n < 1) + { + throw new ArgumentException(Resources.ArgumentMustBePositive); + } + var ret = new double[n]; + for (int i = 0; i < ret.Length; i++) + { + ret[i] = Sample(); + } + return ret; + } + + /// + /// Returns a nonnegative random number. + /// + /// + /// A 32-bit signed integer greater than or equal to zero and less than . + /// + public override int Next() + { + return (int)(Sample() * int.MaxValue); + } + + /// + /// Returns a random number less then a specified maximum. + /// + /// The exclusive upper bound of the random number returned. + /// A 32-bit signed integer less than . + /// is negative. + public override int Next(int maxValue) + { + if (0 > maxValue) + { + throw new ArgumentOutOfRangeException(Resources.ArgumentMustBePositive); + } + + return (int) (Sample() % maxValue); + } + + /// + /// Returns a random number within a specified range. + /// + /// The inclusive lower bound of the random number returned. + /// The exclusive upper bound of the random number returned. must be greater than or equal to . + /// + /// A 32-bit signed integer greater than or equal to and less than ; that is, the range of return values includes but not . If equals , is returned. + /// + /// is greater than . + public override int Next(int minValue, int maxValue) + { + if (minValue > maxValue) + { + throw new ArgumentOutOfRangeException(Resources.ArgumentMinValueGreaterThanMaxValue); + } + + return (int) (Sample()*(maxValue - minValue)) + minValue; + } + + /// + /// Fills the elements of a specified array of bytes with random numbers. + /// + /// An array of bytes to contain random numbers. + /// is null. + public override void NextBytes(byte[] buffer) + { + if (buffer == null) + { + throw new ArgumentNullException("buffer"); + } + + for (int i = 0; i < buffer.Length; i++) + { + buffer[i] = (byte) (Next()%256); + } + } + + /// + /// Returns a random number between 0.0 and 1.0. + /// + /// A double-precision floating point number greater than or equal to 0.0, and less than 1.0. + protected override double Sample() + { + return _sampleMethod(); + } + + /// + /// Thread safe version of which returns a random number between 0.0 and 1.0. + /// + /// + private double ThreadSafeSample() + { + lock(_lock) + { + return DoSample(); + } + } + + /// + /// Returns a random number between 0.0 and 1.0. + /// + /// + /// A double-precision floating point number greater than or equal to 0.0, and less than 1.0. + /// + protected abstract double DoSample(); + } +} \ No newline at end of file