diff --git a/README.md b/README.md index d33bb61fc..e13021a19 100644 --- a/README.md +++ b/README.md @@ -82,6 +82,14 @@ Create a 5-by-3 matrix of random floats on the GPU ... ``` +### Troubleshooting + +If the build command fails with undefined references errors even after taking care of environment +variables, we recommend doing a `cargo clean` and re-running `cargo build` or `cargo test`. + +You can also use some environment variables mentioned in our [book][23], such as `AF_PRINT_ERRORS` +to print more elaborate error messages to console. + ## Acknowledgements The ArrayFire library is written by developers at [ArrayFire][14] LLC with [contributions][15] @@ -118,3 +126,4 @@ the DARPA SBIR Program Office. [20]: https://img.shields.io/badge/arrayfire-google--groups-orange [21]: http://arrayfire.org/arrayfire-rust/book/index.html [22]: https://img.shields.io/badge/arrayfire-mdbook-073763?logo=readthedocs +[23]: http://arrayfire.org/arrayfire-rust/book/configuring_arrayfire_environment.html diff --git a/src/core/macros.rs b/src/core/macros.rs index 69c08a8d5..c06018a1f 100644 --- a/src/core/macros.rs +++ b/src/core/macros.rs @@ -298,6 +298,57 @@ macro_rules! eval { }; } +/// Create an array of given shape filled with a single value a.k.a constant array +/// +/// # Examples +/// +/// ```rust +/// # use arrayfire::{constant}; +/// let _zeros_1d = constant!(0.0f32; 10); +/// let _ones_3d = constant!(1u32; 3, 3, 3); +/// +/// let dim = 10; +/// let mix_shape = constant!(42.0f32; dim, 10); +/// ``` +#[macro_export] +macro_rules! constant { + ($value:expr; $($dim:expr),+) => { + $crate::constant($value, $crate::dim4!($($dim),*)) + }; +} + +/// Create an array of given shape sampled from uniform distribution +/// +/// If no type argument is specified, the data type defaults to 32 bit floats. +/// +/// # Examples +/// +/// ```rust +/// # use arrayfire::{randu}; +/// let mat10x10 = randu!(10, 10); +/// ``` +#[macro_export] +macro_rules! randu { + ($($dim:expr),+) => { $crate::randu::($crate::dim4!($($dim),*)) }; + ($type:ty; $($dim:expr),+) => { $crate::randu::<$type>($crate::dim4!($($dim),*)) }; +} + +/// Create an array of given shape sampled from normal distribution +/// +/// If no type argument is specified, the data type defaults to 32 bit floats. +/// +/// # Examples +/// +/// ```rust +/// # use arrayfire::{randn}; +/// let mat10x10 = randn!(10, 10); +/// ``` +#[macro_export] +macro_rules! randn { + ($($dim:expr),+) => { $crate::randn::($crate::dim4!($($dim),*)) }; + ($type:ty; $($dim:expr),+) => { $crate::randn::<$type>($crate::dim4!($($dim),*)) }; +} + #[cfg(test)] mod tests { use super::super::array::Array; @@ -425,4 +476,23 @@ mod tests { eval!(a[indices, seq4gen] = b); // ANCHOR_END: macro_seq_array_assign } + + #[test] + fn constant_macro() { + let _zeros_1d = constant!(0.0f32; 10); + let _zeros_2d = constant!(0.0f64; 5, 5); + let _ones_3d = constant!(1u32; 3, 3, 3); + let _twos_4d = constant!(2u16; 2, 2, 2, 2); + + let dim = 10; + let _mix_shape = constant!(42.0f32; dim, 10); + } + + #[test] + fn rand_macro() { + let _ru5x5 = randu!(5, 5); + let _rn5x5 = randn!(5, 5); + let _ruu32_5x5 = randu!(u32; 5, 5); + let _ruu8_5x5 = randu!(u8; 5, 5); + } }