1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
//! Constructors for creating random matrices.
//!
//! This module provides functions and builders for creating matrices filled with random values
//! from various distributions such as normal distribution and uniform distribution.
use crate::{
dim::DimTrait,
matrix::{MatrixBase, OwnedMatrix},
matrix_impl::Matrix,
memory_impl::OwnedMem,
num::Num,
};
use rand::prelude::*;
use rand_distr::{num_traits::Float, uniform::SampleUniform, Normal, StandardNormal, Uniform};
/// Creates a matrix filled with random values from a normal distribution.
///
/// # Arguments
///
/// * `mean` - The mean of the normal distribution.
/// * `std_dev` - The standard deviation of the normal distribution.
/// * `shape` - The shape of the matrix.
/// * `seed` - An optional seed for the random number generator.
pub fn normal<T: Num, D: DimTrait>(
mean: T,
std_dev: T,
shape: D,
seed: Option<u64>,
) -> Matrix<OwnedMem<T>, D>
where
StandardNormal: Distribution<T>,
{
let mut rng: Box<dyn RngCore> = if let Some(seed) = seed {
Box::new(StdRng::seed_from_u64(seed))
} else {
Box::new(thread_rng())
};
let normal = Normal::new(mean, std_dev).unwrap();
let mut data = Vec::with_capacity(shape.num_elm());
for _ in 0..shape.num_elm() {
data.push(normal.sample(&mut *rng));
}
Matrix::from_vec(data, shape)
}
/// Creates a matrix filled with random values from a normal distribution with the same shape as another matrix.
///
/// # Arguments
///
/// * `mean` - The mean of the normal distribution.
/// * `std_dev` - The standard deviation of the normal distribution.
/// * `a` - The matrix whose shape is used.
/// * `seed` - An optional seed for the random number generator.
pub fn normal_like<T: Num, D: DimTrait>(
mean: T,
std_dev: T,
a: &Matrix<OwnedMem<T>, D>,
seed: Option<u64>,
) -> Matrix<OwnedMem<T>, D>
where
StandardNormal: Distribution<T>,
{
normal(mean, std_dev, a.shape(), seed)
}
/// Creates a matrix filled with random values from a uniform distribution.
///
/// # Arguments
///
/// * `low` - The lower bound of the uniform distribution.
/// * `high` - The upper bound of the uniform distribution.
/// * `shape` - The shape of the matrix.
/// * `seed` - An optional seed for the random number generator.
pub fn uniform<T, D: DimTrait>(
low: T,
high: T,
shape: D,
seed: Option<u64>,
) -> Matrix<OwnedMem<T>, D>
where
T: Num,
Uniform<T>: Distribution<T>,
{
let mut rng: Box<dyn RngCore> = if let Some(seed) = seed {
Box::new(StdRng::seed_from_u64(seed))
} else {
Box::new(thread_rng())
};
let uniform = Uniform::new(low, high);
let mut data = Vec::with_capacity(shape.num_elm());
for _ in 0..shape.num_elm() {
data.push(uniform.sample(&mut *rng));
}
Matrix::from_vec(data, shape)
}
/// Creates a matrix filled with random values from a uniform distribution with the same shape as another matrix.
///
/// # Arguments
///
/// * `low` - The lower bound of the uniform distribution.
/// * `high` - The upper bound of the uniform distribution.
/// * `a` - The matrix whose shape is used.
/// * `seed` - An optional seed for the random number generator.
pub fn uniform_like<T, D: DimTrait>(
low: T,
high: T,
a: &Matrix<OwnedMem<T>, D>,
seed: Option<u64>,
) -> Matrix<OwnedMem<T>, D>
where
T: Num,
Uniform<T>: Distribution<T>,
{
uniform(low, high, a.shape(), seed)
}
/// A builder for creating matrices filled with random values from a normal distribution.
#[derive(Debug, Clone, Default)]
pub struct NormalBuilder<T: Num + Float, D: DimTrait> {
mean: Option<T>,
std_dev: Option<T>,
shape: Option<D>,
seed: Option<u64>,
}
impl<T, D> NormalBuilder<T, D>
where
T: Num,
D: DimTrait,
{
/// Creates a new `NormalBuilder`.
pub fn new() -> Self {
NormalBuilder {
mean: None,
std_dev: None,
shape: None,
seed: None,
}
}
/// Sets the mean of the normal distribution.
pub fn mean(mut self, mean: T) -> Self {
self.mean = Some(mean);
self
}
/// Sets the standard deviation of the normal distribution.
pub fn std_dev(mut self, std_dev: T) -> Self {
self.std_dev = Some(std_dev);
self
}
/// Sets the shape of the matrix.
pub fn shape(mut self, shape: D) -> Self {
self.shape = Some(shape);
self
}
/// Sets the seed for the random number generator.
pub fn seed(mut self, seed: u64) -> Self {
self.seed = Some(seed);
self
}
/// Sets the shape of the matrix to be the same as another matrix.
pub fn from_matrix<M: MatrixBase<Dim = D>>(mut self, a: &M) -> Self {
self.shape = Some(a.shape());
self
}
/// Builds the matrix.
pub fn build(self) -> Matrix<OwnedMem<T>, D>
where
StandardNormal: Distribution<T>,
{
if self.mean.is_none() || self.std_dev.is_none() || self.shape.is_none() {
panic!("mean, std_dev, and shape must be set");
}
normal(
self.mean.unwrap(),
self.std_dev.unwrap(),
self.shape.unwrap(),
self.seed,
)
}
}
/// A builder for creating matrices filled with random values from a uniform distribution.
#[derive(Debug, Clone, Default)]
pub struct UniformBuilder<T, D> {
low: Option<T>,
high: Option<T>,
shape: Option<D>,
seed: Option<u64>,
}
impl<T, D> UniformBuilder<T, D>
where
T: Num + Float + SampleUniform,
Uniform<T>: Distribution<T>,
D: DimTrait,
{
/// Creates a new `UniformBuilder`.
pub fn new() -> Self {
UniformBuilder {
low: None,
high: None,
shape: None,
seed: None,
}
}
/// Sets the lower bound of the uniform distribution.
pub fn low(mut self, low: T) -> Self {
self.low = Some(low);
self
}
/// Sets the upper bound of the uniform distribution.
pub fn high(mut self, high: T) -> Self {
self.high = Some(high);
self
}
/// Sets the shape of the matrix.
pub fn shape(mut self, shape: D) -> Self {
self.shape = Some(shape);
self
}
/// Sets the seed for the random number generator.
pub fn seed(mut self, seed: u64) -> Self {
self.seed = Some(seed);
self
}
/// Sets the shape of the matrix to be the same as another matrix.
pub fn from_matrix<M: MatrixBase<Dim = D>>(mut self, a: &M) -> Self {
self.shape = Some(a.shape());
self
}
/// Builds the matrix.
pub fn build(self) -> Matrix<OwnedMem<T>, D> {
if self.low.is_none() || self.high.is_none() || self.shape.is_none() {
panic!("low, high, and shape must be set");
}
uniform(
self.low.unwrap(),
self.high.unwrap(),
self.shape.unwrap(),
self.seed,
)
}
}