UnitComplex: More efficient sampling

This commit is contained in:
Vinzent Steinberg 2021-04-10 03:49:46 -03:00
parent 09f26385cc
commit 7417f6cbca

View File

@ -2,10 +2,7 @@
use quickcheck::{Arbitrary, Gen}; use quickcheck::{Arbitrary, Gen};
#[cfg(feature = "rand-no-std")] #[cfg(feature = "rand-no-std")]
use rand::{ use rand::{distributions::{Distribution, Standard}, Rng};
distributions::{Distribution, Uniform, uniform::SampleUniform, Standard},
Rng,
};
use num::One; use num::One;
use num_complex::Complex; use num_complex::Complex;
@ -401,13 +398,13 @@ where
impl<N: SimdRealField> Distribution<UnitComplex<N>> for Standard impl<N: SimdRealField> Distribution<UnitComplex<N>> for Standard
where where
N::Element: SimdRealField, N::Element: SimdRealField,
N: SampleUniform, rand_distr::UnitCircle: Distribution<[N; 2]>,
{ {
/// Generate a uniformly distributed random `UnitComplex`. /// Generate a uniformly distributed random `UnitComplex`.
#[inline] #[inline]
fn sample<'a, R: Rng + ?Sized>(&self, rng: &mut R) -> UnitComplex<N> { fn sample<'a, R: Rng + ?Sized>(&self, rng: &mut R) -> UnitComplex<N> {
let twopi = Uniform::new(N::zero(), N::simd_two_pi()); let x = rng.sample(rand_distr::UnitCircle);
UnitComplex::from_angle(rng.sample(twopi)) UnitComplex::new_unchecked(Complex::new(x[0], x[1]))
} }
} }