concision_core/init/
utils.rs

1/*
2   Appellation: utils <mod>
3   Contrib: FL03 <jo3mccain@icloud.com>
4*/
5use ndarray::*;
6use ndrand::RandomExt;
7use num::complex::{Complex, ComplexDistribution};
8use num::Num;
9use rand::distributions::uniform::{SampleUniform, Uniform};
10use rand::rngs::StdRng;
11use rand::{rngs, SeedableRng};
12use rand_distr::{Distribution, StandardNormal};
13
14/// Generate a random array of complex numbers with real and imaginary parts in the range [0, 1)
15pub fn randc<A, S, D>(shape: impl IntoDimension<Dim = D>) -> ArrayBase<S, D>
16where
17    A: Clone + Num,
18    D: Dimension,
19    S: DataOwned<Elem = Complex<A>>,
20    ComplexDistribution<A, A>: Distribution<Complex<A>>,
21{
22    let distr = ComplexDistribution::<A, A>::new(A::one(), A::one());
23    ArrayBase::random(shape, distr)
24}
25
26/// Given a shape, generate a random array using the StandardNormal distribution
27pub fn stdnorm<S, D, Sh>(shape: Sh) -> ArrayBase<S, D>
28where
29    D: Dimension,
30    S: DataOwned,
31    Sh: ShapeBuilder<Dim = D>,
32    StandardNormal: Distribution<S::Elem>,
33{
34    ArrayBase::random(shape, StandardNormal)
35}
36
37pub fn stdnorm_from_seed<S, D, Sh>(shape: Sh, seed: u64) -> ArrayBase<S, D>
38where
39    D: Dimension,
40    S: DataOwned,
41    Sh: ShapeBuilder<Dim = D>,
42    StandardNormal: Distribution<S::Elem>,
43{
44    ArrayBase::random_using(shape, StandardNormal, &mut StdRng::seed_from_u64(seed))
45}
46/// Creates a random array from a uniform distribution using a given key
47pub fn uniform_from_seed<T, D>(
48    key: u64,
49    start: T,
50    stop: T,
51    shape: impl IntoDimension<Dim = D>,
52) -> Array<T, D>
53where
54    D: Dimension,
55    T: SampleUniform,
56{
57    Array::random_using(
58        shape,
59        Uniform::new(start, stop),
60        &mut rngs::StdRng::seed_from_u64(key),
61    )
62}