/// <summary> /// Returns the sampler for a specified rng, data type and distribution spec /// </summary> /// <param name="rng">The random stream</param> /// <param name="spec">The distribution specifier</param> /// <typeparam name="T">The sample point type</typeparam> public static ISampler <T> Sampler <T>(this MklRng rng, IDistributionSpec <T> spec) where T : unmanaged { var sampler = default(ISampler <T>); switch (spec.DistKind) { case DistKind.Uniform: sampler = rng.UniformSampler <T>(UniformSpec.From(spec)); break; case DistKind.UniformBits: sampler = rng.UniformBitsSampler <T>(UniformBitsSpec.From(spec)); break; case DistKind.Bernoulli: sampler = rng.BernoulliSampler <T>(BernoulliSpec.From(spec)); break; case DistKind.Gaussian: sampler = rng.GaussianSampler <T>(GaussianSpec.From(spec)); break; default: throw unsupported <T>(); } return(sampler); }
/// <summary> /// Returns the sampler for a specified rng, data type and distribution spec /// </summary> /// <param name="rng">The random stream</param> /// <param name="spec">The distribution specifier</param> /// <typeparam name="T">The sample point type</typeparam> public static IRngSampler <T> Sampler <T>(this MklRng rng, IDistributionSpec <T> spec) where T : unmanaged { var sampler = default(IRngSampler <T>); switch (spec.DistKind) { case DistributionKind.Uniform: sampler = rng.UniformSampler <T>(Distributions.uniform(spec)); break; case DistributionKind.UniformBits: sampler = rng.UniformBitsSampler <T>(Distributions.uniformbits(spec)); break; case DistributionKind.Bernoulli: sampler = rng.BernoulliSampler <T>(Distributions.bernoulli(spec)); break; case DistributionKind.Gaussian: sampler = rng.GaussianSampler <T>(Distributions.gaussian(spec)); break; default: throw Unsupported.define <T>(); } return(sampler); }
public Sampler(MklRng src, S distspec, int?bufferLen = null) { this.Source = src; this.BufferLength = bufferLen ?? Pow2.T14; this.Buffer = MemorySpan.Alloc <T>(this.BufferLength); this.Remaining = 0; this.DistSpec = distspec; }
public Sampler(MklRng src, S distspec, int?bufferLen = null) { this.Source = src; this.BufferLength = bufferLen ?? Pow2.T14; this.Buffer = new T[BufferLength]; this.Remaining = 0; this.DistSpec = distspec; }
public static ISampler <T> BernoulliSampler <T>(this MklRng rng, BernoulliSpec <T> spec) where T : unmanaged { var sampler = default(ISampler <T>); if (typeof(T) == typeof(int)) { sampler = samplers.bernoulli(rng, spec) as ISampler <T>; } else { throw unsupported <T>(); } return(sampler); }
public static ISampler <T> GaussianSampler <T>(this MklRng rng, GaussianSpec <T> spec) where T : unmanaged { var sampler = default(ISampler <T>); if (typeof(T) == typeof(float)) { sampler = samplers.gaussian(rng, spec.ToFloat32()) as ISampler <T>; } else if (typeof(T) == typeof(double)) { sampler = samplers.gaussian(rng, spec.ToFloat64()) as ISampler <T>; } else { throw unsupported <T>(); } return(sampler); }
public void CreateMt2203Generators() { var gencount = Pow2.T08; var samplesize = Pow2.T16; var seeds = Random.Array <uint>(gencount); var streams = new MklRng[gencount]; for (var i = 0; i < gencount; i++) { streams[i] = rng.mt2203(seeds[i], i); } var bufferF64 = new double[samplesize]; var bufferU32 = new uint[samplesize]; var bufferI32 = new int[samplesize]; var ufRange = closed(1.0, 250.0); for (var i = 0; i < gencount; i++) { var stream = streams[i]; sample.uniform(stream, ufRange, bufferF64); Dataset.Load(bufferF64, 1).Extrema(); var max = Dataset.Load(bufferF64, 1).Max()[0]; Claim.lteq(max, ufRange.Right); Claim.neq(max, 0); sample.bits(stream, bufferU32); sample.bernoulli(stream, .40, bufferI32); for (var j = 0; j < samplesize; j++) { Claim.yea(bufferI32[j] == 0 || bufferI32[j] == 1); } sample.gaussian(stream, .75, .75, bufferF64); sample.laplace(stream, .5, .5, bufferF64); } for (var i = 0; i < gencount; i++) { streams[i].Dispose(); } }
public static ISampler <T> UniformBitsSampler <T>(this MklRng rng, UniformBitsSpec <T>?spec = null) where T : unmanaged { var sampler = default(ISampler <T>); var _spec = spec ?? UniformBitsSpec.Define <T>(); if (typeof(T) == typeof(uint)) { sampler = samplers.bits(rng, _spec.ToUInt32()) as ISampler <T>; } else if (typeof(T) == typeof(ulong)) { sampler = samplers.bits(rng, _spec.ToUInt64()) as ISampler <T>; } else { throw unsupported <T>(); } return(sampler); }
public static IRngSampler <T> UniformSampler <T>(this MklRng rng, UniformSpec <T>?spec = null) where T : unmanaged { var _spec = spec ?? Distributions.uniform <T>(Numeric.minval <T>(), Numeric.maxval <T>()); var sampler = default(IRngSampler <T>); if (typeof(T) == typeof(int)) { sampler = samplers.uniform(rng, _spec.ToInt32()) as IRngSampler <T>; } else if (typeof(T) == typeof(float)) { sampler = samplers.uniform(rng, _spec.ToFloat32()) as IRngSampler <T>; } else if (typeof(T) == typeof(double)) { sampler = samplers.uniform(rng, _spec.ToFloat64()) as IRngSampler <T>; } else { throw no <T>(); } return(sampler); }
public static ISampler <T> UniformSampler <T>(this MklRng rng, UniformSpec <T>?spec = null) where T : unmanaged { var _spec = spec ?? UniformSpec.Define <T>(PrimalInfo.minval <T>(), PrimalInfo.maxval <T>()); var sampler = default(ISampler <T>); if (typeof(T) == typeof(int)) { sampler = samplers.uniform(rng, _spec.ToInt32()) as ISampler <T>; } else if (typeof(T) == typeof(float)) { sampler = samplers.uniform(rng, _spec.ToFloat32()) as ISampler <T>; } else if (typeof(T) == typeof(double)) { sampler = samplers.uniform(rng, _spec.ToFloat64()) as ISampler <T>; } else { throw unsupported <T>(); } return(sampler); }
public static void laplace(MklRng stream, double mean, double beta, Span <double> dst) => VSL.vdRngLaplace(0, stream.Source, dst.Length, ref first(dst), mean, beta).ThrowOnError();
public static void exp(MklRng stream, float dx, float beta, Span <float> dst) => VSL.vsRngExponential(0, stream.Source, dst.Length, ref first(dst), dx, beta).ThrowOnError();
public static void gamma(MklRng stream, double alpha, double dx, double beta, Span <double> dst) => VSL.vdRngGamma(VslGammaMethod.GNorm, stream.Source, dst.Length, ref first(dst), alpha, dx, beta).ThrowOnError();
public static void poisson(MklRng stream, double alpha, Span <int> dst) => VSL.viRngPoisson(0, stream.Source, dst.Length, ref first(dst), alpha).ThrowOnError();
public static void exp(MklRng stream, double dx, double beta, Span <double> dst) => VSL.vdRngExponential(0, stream.Source, dst.Length, ref first(dst), dx, beta).ThrowOnError();
public GammaSampler(MklRng src, GammaSpec <T> spec, int?buferLen = null) : base(src, spec, buferLen) { }
public UniformBitsSampler(MklRng src, int?buferLen = null) : base(src, default, buferLen) { }
public static IRngSampler <int> uniform(MklRng src, int min, int max, int?capacity = null) => new UniformSampler <int>(src, (min, max), capacity);
public static IRngSampler <ulong> bits(MklRng src, UniformBitsSpec <ulong> spec, int?capacity = null) => new UniformBitsSampler <ulong>(src, capacity);
public static void laplace(MklRng stream, float a, float b, Span <float> dst) => VSL.vsRngLaplace(0, stream.Source, dst.Length, ref first(dst), a, b).ThrowOnError();
public static IRngSampler <uint> bits32(MklRng src, int?capacity = null) => new UniformBitsSampler <uint>(src, capacity);
public static void uniform(MklRng stream, float min, float max, Span <float> dst) => VSL.vsRngUniform(0, stream.Source, dst.Length, ref first(dst), min, max).ThrowOnError();
public static IRngSampler <int> uniform(MklRng src, Interval <int> range, int?capacity = null) => new UniformSampler <int>(src, range, capacity);
public static void uniform(MklRng stream, Interval <double> range, Span <double> dst) => VSL.vdRngUniform(0, stream.Source, dst.Length, ref first(dst), range.Left, range.Right).ThrowOnError();
public PoissonSampler(MklRng src, PoissonSpec <T> spec, int?buferLen = null) : base(src, spec, buferLen) { }
public static void uniform(MklRng stream, double min, double max, Span <double> dst) => VSL.vdRngUniform(0, stream.Source, dst.Length, ref first(dst), min, max).ThrowOnError();
public UniformSampler(MklRng src, UniformSpec <T> distspec, int?buferLen = null) : base(src, distspec, buferLen) { }
public static void bits(MklRng stream, Span <ulong> dst) => VSL.viRngUniformBits64(0, stream.Source, dst.Length, ref first(dst)).ThrowOnError();
public GammaSampler(MklRng src, T alpha, T dx, T beta, int?buferLen = null) : base(src, GammaSpec <T> .Define(alpha, dx, beta), buferLen) { }
public BernoulliSampler(MklRng src, BernoulliSpec <double> spec, int?buferLen = null) : base(src, spec, buferLen) { }