Refactored to use generic TNum for any floating point type

This commit is contained in:
Thorsten Sommer committed 2023-07-07 14:41:01 +02:00
1 parent 6b656b90e3
commit 1c28a57c06
55 files changed
+578 -1147

No files matched your search

+20 -19
View File
@@ -1,4 +1,5 @@
using System;
using System.Numerics;
using System.Threading;
using System.Threading.Tasks;
using FastRng.Distributions;
@@ -8,13 +9,13 @@ namespace FastRng;
/// <summary>
/// ShapeFitter is a rejection sampler, cf. https://en.wikipedia.org/wiki/Rejection_sampling
/// </summary>
public sealed class ShapeFitter
public sealed class ShapeFitter<TNum> where TNum : IFloatingPointIeee754<TNum>, IDivisionOperators<TNum, TNum, TNum>
{
private readonly float[] probabilities;
private readonly IRandom rng;
private readonly float max;
private readonly float sampleSize;
private readonly IDistribution uniform;
private readonly TNum[] probabilities;
private readonly IRandom<TNum> rng;
private readonly TNum max;
private readonly TNum sampleSize;
private readonly IDistribution<TNum> uniform;
/// <summary>
/// Creates a shape fitter instance.
@@ -22,16 +23,16 @@ public sealed class ShapeFitter
/// <param name="shapeFunction">The function which describes the desired shape.</param>
/// <param name="rng">The random number generator instance to use.</param>
/// <param name="sampleSize">The number of sampling steps to sample the given function.</param>
public ShapeFitter(Func<float, float> shapeFunction, IRandom rng, ushort sampleSize = 50)
public ShapeFitter(Func<TNum, TNum> shapeFunction, IRandom<TNum> rng, ushort sampleSize = 50)
{
this.rng = rng;
this.uniform = new Uniform(rng);
this.sampleSize = sampleSize;
this.probabilities = new float[sampleSize];
this.uniform = new Uniform<TNum>(rng);
this.sampleSize = TNum.CreateChecked(sampleSize);
this.probabilities = new TNum[sampleSize];
var sampleStepSize = 1.0f / sampleSize;
var nextStep = 0.0f + sampleStepSize;
var maxValue = 0.0f;
var sampleStepSize = TNum.One / TNum.CreateChecked(sampleSize);
var nextStep = TNum.Zero + sampleStepSize;
var maxValue = TNum.Zero;
for (var n = 0; n < sampleSize; n++)
{
this.probabilities[n] = shapeFunction(nextStep);
@@ -49,21 +50,21 @@ public sealed class ShapeFitter
/// </summary>
/// <param name="token">An optional cancellation token.</param>
/// <returns>The next value regarding the given shape.</returns>
public async ValueTask<float> NextNumber(CancellationToken token = default)
public async ValueTask<TNum> NextNumber(CancellationToken token = default)
{
while (!token.IsCancellationRequested)
{
var x = await this.rng.GetUniform(token);
if (float.IsNaN(x))
if (TNum.IsNaN(x))
return x;
var nextBucket = (int)MathF.Floor(x * this.sampleSize);
var nextBucket = int.CreateChecked(TNum.Floor(x * this.sampleSize));
if (nextBucket >= this.probabilities.Length)
nextBucket = this.probabilities.Length - 1;
var threshold = this.probabilities[nextBucket];
var y = await this.uniform.NextNumber(0.0f, this.max, token);
if (float.IsNaN(y))
var y = await this.uniform.NextNumber(TNum.Zero, this.max, token);
if (TNum.IsNaN(y))
return y;
if(y > threshold)
@@ -72,6 +73,6 @@ public sealed class ShapeFitter
return x;
}
return float.NaN;
return TNum.NaN;
}
}