UnitComplex: make use of UnitCircle where available

This commit is contained in:
Diggory Hardy 2019-07-26 15:38:21 +01:00
parent 2e22821740
commit 16d78f4680
1 changed files with 15 additions and 2 deletions

View File

@ -3,7 +3,7 @@ use quickcheck::{Arbitrary, Gen};
use num::One;
use num_complex::Complex;
use rand::distributions::{Distribution, Standard, uniform::SampleUniform};
use rand::distributions::{Distribution, Standard};
use rand::Rng;
use alga::general::RealField;
@ -275,8 +275,21 @@ impl<N: RealField> One for UnitComplex<N> {
}
}
#[cfg(feature = "std")]
impl<N: RealField> Distribution<UnitComplex<N>> for Standard
where N: SampleUniform
where rand_distr::UnitCircle: Distribution<[N; 2]>
{
/// Generate a uniformly distributed random `UnitComplex`.
#[inline]
fn sample<'a, R: Rng + ?Sized>(&self, rng: &mut R) -> UnitComplex<N> {
let x = rand_distr::UnitCircle.sample(rng);
UnitComplex::new_unchecked(Complex::new(x[0], x[1]))
}
}
#[cfg(not(feature = "std"))]
impl<N: RealField> Distribution<UnitComplex<N>> for Standard
where N: rand::distributions::uniform::SampleUniform
{
/// Generate a uniformly distributed random `UnitComplex`.
#[inline]