Skip to content

New macros: constant, randu and randn #249

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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
70 changes: 70 additions & 0 deletions src/core/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<f32>($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::<f32>($crate::dim4!($($dim),*)) };
($type:ty; $($dim:expr),+) => { $crate::randn::<$type>($crate::dim4!($($dim),*)) };
}

#[cfg(test)]
mod tests {
use super::super::array::Array;
Expand Down Expand Up @@ -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);
}
}