Skip to content

Commit 9a837d9

Browse files
committed
Add random number generator macros for ease of use
1 parent 848713a commit 9a837d9

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

src/core/macros.rs

+40
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,38 @@ macro_rules! constant {
317317
};
318318
}
319319

320+
/// Create an array of given shape sampled from uniform distribution
321+
///
322+
/// If no type argument is specified, the data type defaults to 32 bit floats.
323+
///
324+
/// # Examples
325+
///
326+
/// ```rust
327+
/// # use arrayfire::{randu};
328+
/// let mat10x10 = randu!(10, 10);
329+
/// ```
330+
#[macro_export]
331+
macro_rules! randu {
332+
($($dim:expr),+) => { $crate::randu::<f32>($crate::dim4!($($dim),*)) };
333+
($type:ty; $($dim:expr),+) => { $crate::randu::<$type>($crate::dim4!($($dim),*)) };
334+
}
335+
336+
/// Create an array of given shape sampled from normal distribution
337+
///
338+
/// If no type argument is specified, the data type defaults to 32 bit floats.
339+
///
340+
/// # Examples
341+
///
342+
/// ```rust
343+
/// # use arrayfire::{randn};
344+
/// let mat10x10 = randn!(10, 10);
345+
/// ```
346+
#[macro_export]
347+
macro_rules! randn {
348+
($($dim:expr),+) => { $crate::randn::<f32>($crate::dim4!($($dim),*)) };
349+
($type:ty; $($dim:expr),+) => { $crate::randn::<$type>($crate::dim4!($($dim),*)) };
350+
}
351+
320352
#[cfg(test)]
321353
mod tests {
322354
use super::super::array::Array;
@@ -455,4 +487,12 @@ mod tests {
455487
let dim = 10;
456488
let _mix_shape = constant!(42.0f32; dim, 10);
457489
}
490+
491+
#[test]
492+
fn rand_macro() {
493+
let _ru5x5 = randu!(5, 5);
494+
let _rn5x5 = randn!(5, 5);
495+
let _ruu32_5x5 = randu!(u32; 5, 5);
496+
let _ruu8_5x5 = randu!(u8; 5, 5);
497+
}
458498
}

0 commit comments

Comments
 (0)