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
/// Macro for creating vectors and matrices. /// /// To use this macro, import Numeric as follows: /// /// ```text /// #[macro_use(tensor)] /// extern crate numeric; /// ``` /// /// # Examples /// /// 1D tensor (vector): /// /// ``` /// # #[macro_use] extern crate numeric; fn main() { /// let x = tensor![1.0, 2.0, 3.0]; /// # assert!(x == numeric::Tensor::new(vec![1.0, 2.0, 3.0])); /// # } /// ``` /// /// 2D tensor (matrix): /// /// ``` /// # #[macro_use] extern crate numeric; use numeric::tensor; fn main() { /// let x = tensor![1, 0; 3, 2; 5, 4]; /// # assert!(x == numeric::Tensor::new(vec![1, 0, 3, 2, 5, 4]).reshape(&[3, 2])); /// # } /// ``` /// /// 1D tensor filled with a single value: /// /// ``` /// # #[macro_use] extern crate numeric; use numeric::tensor; fn main() { /// let x = tensor![2.0; 5]; /// # assert!(x == numeric::Tensor::new(vec![2.0, 2.0, 2.0, 2.0, 2.0])); /// # } /// ``` #[macro_export] macro_rules! tensor { (@count) => (0); (@count $head:tt $($tail:tt)*) => (1 + tensor!(@count $($tail)*)); ($elem:expr; $n:expr) => ( $crate::Tensor::new(vec![$elem; $n]) ); ($($x:expr),*) => ( $crate::Tensor::new(vec![$($x),*]) ); ($($x:expr,)*) => ( tensor![$($x),*] ); ($($($x:expr),*;)*) => ( $crate::Tensor::new(vec![$($($x),*),*]).reshape(&[tensor!(@count $([$($x),*])*), -1]) ); ($($($x:expr),*);*) => ( tensor![$($($x),*;)*] ); }