Debian Codegraph @

debian/missing-sources/nalgebra/tests/core/helper.rs

raw
1 // This module implement several methods to fill some
2 // missing features of num-complex when it comes to randomness.
3
4 use na::RealField;
5 use num_complex::Complex;
6 use quickcheck::{Arbitrary, Gen};
7 use rand::Rng;
8 use rand::distr::{Distribution, StandardUniform};
9
10 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
11 pub struct RandComplex<T>(pub Complex<T>);
12
13 impl<T: Arbitrary + RealField> Arbitrary for RandComplex<T> {
14 #[inline]
15 fn arbitrary(rng: &mut Gen) -> Self {
16 let im = Arbitrary::arbitrary(rng);
17 let re = Arbitrary::arbitrary(rng);
18 RandComplex(Complex::new(re, im))
19 }
20 }
21
22 impl<T: RealField> Distribution<RandComplex<T>> for StandardUniform
23 where
24 StandardUniform: Distribution<T>,
25 {
26 #[inline]
27 fn sample<'a, G: Rng + ?Sized>(&self, rng: &'a mut G) -> RandComplex<T> {
28 let re = rng.random();
29 let im = rng.random();
30 RandComplex(Complex::new(re, im))
31 }
32 }
33
34 // This is a wrapper similar to RandComplex, but for non-complex.
35 // This exists only to make generic tests easier to write.
36 //
37 // Generates variates in the range [0, 1).
38 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
39 pub struct RandScalar<T>(pub T);
40
41 impl<T: Arbitrary> Arbitrary for RandScalar<T> {
42 #[inline]
43 fn arbitrary(rng: &mut Gen) -> Self {
44 RandScalar(Arbitrary::arbitrary(rng))
45 }
46 }
47
48 impl<T: RealField> Distribution<RandScalar<T>> for StandardUniform
49 where
50 StandardUniform: Distribution<T>,
51 {
52 #[inline]
53 fn sample<'a, G: Rng + ?Sized>(&self, rng: &'a mut G) -> RandScalar<T> {
54 RandScalar(self.sample(rng))
55 }
56 }