Merge pull request #864 from vks/idiomatic-rand

More idiomatic use of Rand
This commit is contained in:
Sébastien Crozet 2021-04-11 13:52:48 +02:00 committed by GitHub
commit b2dadffcf2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 27 additions and 17 deletions

View File

@ -284,7 +284,8 @@ where
where where
Standard: Distribution<N>, Standard: Distribution<N>,
{ {
Self::from_fn_generic(nrows, ncols, |_, _| rand::random()) let mut rng = rand::thread_rng();
Self::from_fn_generic(nrows, ncols, |_, _| rng.gen())
} }
/// Creates a matrix filled with random values from the given distribution. /// Creates a matrix filled with random values from the given distribution.
@ -852,6 +853,7 @@ where
} }
} }
// TODO(specialization): faster impls possible for D≤4 (see rand_distr::{UnitCircle, UnitSphere})
#[cfg(feature = "rand")] #[cfg(feature = "rand")]
impl<N: crate::RealField, D: DimName> Distribution<Unit<VectorN<N, D>>> for Standard impl<N: crate::RealField, D: DimName> Distribution<Unit<VectorN<N, D>>> for Standard
where where

View File

@ -691,6 +691,7 @@ impl<N: RealField> Distribution<Orthographic3<N>> for Standard
where where
Standard: Distribution<N>, Standard: Distribution<N>,
{ {
/// Generate an arbitrary random variate for testing purposes.
fn sample<R: Rng + ?Sized>(&self, r: &mut R) -> Orthographic3<N> { fn sample<R: Rng + ?Sized>(&self, r: &mut R) -> Orthographic3<N> {
use crate::base::helper; use crate::base::helper;
let left = r.gen(); let left = r.gen();

View File

@ -275,6 +275,7 @@ impl<N: RealField> Distribution<Perspective3<N>> for Standard
where where
Standard: Distribution<N>, Standard: Distribution<N>,
{ {
/// Generate an arbitrary random variate for testing purposes.
fn sample<'a, R: Rng + ?Sized>(&self, r: &'a mut R) -> Perspective3<N> { fn sample<'a, R: Rng + ?Sized>(&self, r: &'a mut R) -> Perspective3<N> {
use crate::base::helper; use crate::base::helper;
let znear = r.gen(); let znear = r.gen();

View File

@ -164,6 +164,7 @@ where
DefaultAllocator: Allocator<N, D>, DefaultAllocator: Allocator<N, D>,
Standard: Distribution<N>, Standard: Distribution<N>,
{ {
/// Generate a `Point` where each coordinate is an independent variate from `[0, 1)`.
#[inline] #[inline]
fn sample<'a, G: Rng + ?Sized>(&self, rng: &mut G) -> Point<N, D> { fn sample<'a, G: Rng + ?Sized>(&self, rng: &mut G) -> Point<N, D> {
Point::from(rng.gen::<VectorN<N, D>>()) Point::from(rng.gen::<VectorN<N, D>>())

View File

@ -7,7 +7,7 @@ use quickcheck::{Arbitrary, Gen};
#[cfg(feature = "rand-no-std")] #[cfg(feature = "rand-no-std")]
use rand::{ use rand::{
distributions::{Distribution, OpenClosed01, Standard}, distributions::{Distribution, OpenClosed01, Standard, Uniform, uniform::SampleUniform},
Rng, Rng,
}; };
@ -855,6 +855,7 @@ impl<N: SimdRealField> Distribution<UnitQuaternion<N>> for Standard
where where
N::Element: SimdRealField, N::Element: SimdRealField,
OpenClosed01: Distribution<N>, OpenClosed01: Distribution<N>,
N: SampleUniform,
{ {
/// Generate a uniformly distributed random rotation quaternion. /// Generate a uniformly distributed random rotation quaternion.
#[inline] #[inline]
@ -863,10 +864,9 @@ where
// Uniform random rotations. // Uniform random rotations.
// In D. Kirk, editor, Graphics Gems III, pages 124-132. Academic, New York, 1992. // In D. Kirk, editor, Graphics Gems III, pages 124-132. Academic, New York, 1992.
let x0 = rng.sample(OpenClosed01); let x0 = rng.sample(OpenClosed01);
let x1 = rng.sample(OpenClosed01); let twopi = Uniform::new(N::zero(), N::simd_two_pi());
let x2 = rng.sample(OpenClosed01); let theta1 = rng.sample(&twopi);
let theta1 = N::simd_two_pi() * x1; let theta2 = rng.sample(&twopi);
let theta2 = N::simd_two_pi() * x2;
let s1 = theta1.simd_sin(); let s1 = theta1.simd_sin();
let c1 = theta1.simd_cos(); let c1 = theta1.simd_cos();
let s2 = theta2.simd_sin(); let s2 = theta2.simd_sin();

View File

@ -7,7 +7,7 @@ use num::Zero;
#[cfg(feature = "rand-no-std")] #[cfg(feature = "rand-no-std")]
use rand::{ use rand::{
distributions::{Distribution, OpenClosed01, Standard}, distributions::{Distribution, OpenClosed01, Standard, Uniform, uniform::SampleUniform},
Rng, Rng,
}; };
@ -265,12 +265,13 @@ impl<N: SimdRealField> Rotation2<N> {
impl<N: SimdRealField> Distribution<Rotation2<N>> for Standard impl<N: SimdRealField> Distribution<Rotation2<N>> for Standard
where where
N::Element: SimdRealField, N::Element: SimdRealField,
OpenClosed01: Distribution<N>, N: SampleUniform,
{ {
/// Generate a uniformly distributed random rotation. /// Generate a uniformly distributed random rotation.
#[inline] #[inline]
fn sample<'a, R: Rng + ?Sized>(&self, rng: &'a mut R) -> Rotation2<N> { fn sample<'a, R: Rng + ?Sized>(&self, rng: &'a mut R) -> Rotation2<N> {
Rotation2::new(rng.sample(OpenClosed01) * N::simd_two_pi()) let twopi = Uniform::new(N::zero(), N::simd_two_pi());
Rotation2::new(rng.sample(twopi))
} }
} }
@ -923,6 +924,7 @@ impl<N: SimdRealField> Distribution<Rotation3<N>> for Standard
where where
N::Element: SimdRealField, N::Element: SimdRealField,
OpenClosed01: Distribution<N>, OpenClosed01: Distribution<N>,
N: SampleUniform,
{ {
/// Generate a uniformly distributed random rotation. /// Generate a uniformly distributed random rotation.
#[inline] #[inline]
@ -932,7 +934,8 @@ where
// In D. Kirk, editor, Graphics Gems III, pages 117-120. Academic, New York, 1992. // In D. Kirk, editor, Graphics Gems III, pages 117-120. Academic, New York, 1992.
// Compute a random rotation around Z // Compute a random rotation around Z
let theta = N::simd_two_pi() * rng.sample(OpenClosed01); let twopi = Uniform::new(N::zero(), N::simd_two_pi());
let theta = rng.sample(&twopi);
let (ts, tc) = theta.simd_sin_cos(); let (ts, tc) = theta.simd_sin_cos();
let a = MatrixN::<N, U3>::new( let a = MatrixN::<N, U3>::new(
tc, tc,
@ -947,7 +950,7 @@ where
); );
// Compute a random rotation *of* Z // Compute a random rotation *of* Z
let phi = N::simd_two_pi() * rng.sample(OpenClosed01); let phi = rng.sample(&twopi);
let z = rng.sample(OpenClosed01); let z = rng.sample(OpenClosed01);
let (ps, pc) = phi.simd_sin_cos(); let (ps, pc) = phi.simd_sin_cos();
let sqrt_z = z.simd_sqrt(); let sqrt_z = z.simd_sqrt();

View File

@ -69,6 +69,7 @@ where
DefaultAllocator: Allocator<N, D>, DefaultAllocator: Allocator<N, D>,
Standard: Distribution<N> + Distribution<R>, Standard: Distribution<N> + Distribution<R>,
{ {
/// Generate an arbitrary random variate for testing purposes.
#[inline] #[inline]
fn sample<'a, G: Rng + ?Sized>(&self, rng: &mut G) -> Similarity<N, D, R> { fn sample<'a, G: Rng + ?Sized>(&self, rng: &mut G) -> Similarity<N, D, R> {
let mut s = rng.gen(); let mut s = rng.gen();

View File

@ -78,6 +78,7 @@ where
DefaultAllocator: Allocator<N, D>, DefaultAllocator: Allocator<N, D>,
Standard: Distribution<N>, Standard: Distribution<N>,
{ {
/// Generate an arbitrary random variate for testing purposes.
#[inline] #[inline]
fn sample<'a, G: Rng + ?Sized>(&self, rng: &'a mut G) -> Translation<N, D> { fn sample<'a, G: Rng + ?Sized>(&self, rng: &'a mut G) -> Translation<N, D> {
Translation::from(rng.gen::<VectorN<N, D>>()) Translation::from(rng.gen::<VectorN<N, D>>())

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, OpenClosed01, Standard},
Rng,
};
use num::One; use num::One;
use num_complex::Complex; use num_complex::Complex;
@ -401,12 +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,
OpenClosed01: Distribution<N>, 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> {
UnitComplex::from_angle(rng.sample(OpenClosed01) * N::simd_two_pi()) let x = rng.sample(rand_distr::UnitCircle);
UnitComplex::new_unchecked(Complex::new(x[0], x[1]))
} }
} }

View File

@ -33,6 +33,8 @@ where
// This is a wrapper similar to RandComplex, but for non-complex. // This is a wrapper similar to RandComplex, but for non-complex.
// This exists only to make generic tests easier to write. // This exists only to make generic tests easier to write.
//
// Generates variates in the range [0, 1).
#[derive(Copy, Clone, Debug, PartialEq, Eq)] #[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct RandScalar<N>(pub N); pub struct RandScalar<N>(pub N);