|
42 | 42 | ///
|
43 | 43 | /// type M = Array2<f32>;
|
44 | 44 | ///
|
45 |
| -/// fn main() { |
46 |
| -/// // Setup example arrays |
47 |
| -/// let mut a = M::zeros((16, 16)); |
48 |
| -/// let mut b = M::zeros(a.dim()); |
49 |
| -/// let mut c = M::zeros(a.dim()); |
50 |
| -/// |
51 |
| -/// // assign values |
52 |
| -/// b.fill(1.); |
53 |
| -/// for ((i, j), elt) in c.indexed_iter_mut() { |
54 |
| -/// *elt = (i + 10 * j) as f32; |
55 |
| -/// } |
56 |
| -/// |
57 |
| -/// // Example 1: Compute a simple ternary operation: |
58 |
| -/// // elementwise addition of b and c, stored in a |
59 |
| -/// azip!((a in &mut a, &b in &b, &c in &c) *a = b + c); |
| 45 | +/// // Setup example arrays |
| 46 | +/// let mut a = M::zeros((16, 16)); |
| 47 | +/// let mut b = M::zeros(a.dim()); |
| 48 | +/// let mut c = M::zeros(a.dim()); |
| 49 | +/// |
| 50 | +/// // assign values |
| 51 | +/// b.fill(1.); |
| 52 | +/// for ((i, j), elt) in c.indexed_iter_mut() { |
| 53 | +/// *elt = (i + 10 * j) as f32; |
| 54 | +/// } |
60 | 55 | ///
|
61 |
| -/// assert_eq!(a, &b + &c); |
| 56 | +/// // Example 1: Compute a simple ternary operation: |
| 57 | +/// // elementwise addition of b and c, stored in a |
| 58 | +/// azip!((a in &mut a, &b in &b, &c in &c) *a = b + c); |
62 | 59 | ///
|
63 |
| -/// // Example 2: azip!() with index |
64 |
| -/// azip!((index (i, j), &b in &b, &c in &c) { |
65 |
| -/// a[[i, j]] = b - c; |
66 |
| -/// }); |
| 60 | +/// assert_eq!(a, &b + &c); |
67 | 61 | ///
|
68 |
| -/// assert_eq!(a, &b - &c); |
| 62 | +/// // Example 2: azip!() with index |
| 63 | +/// azip!((index (i, j), &b in &b, &c in &c) { |
| 64 | +/// a[[i, j]] = b - c; |
| 65 | +/// }); |
69 | 66 | ///
|
| 67 | +/// assert_eq!(a, &b - &c); |
70 | 68 | ///
|
71 |
| -/// // Example 3: azip!() on references |
72 |
| -/// // See the definition of the function below |
73 |
| -/// borrow_multiply(&mut a, &b, &c); |
74 | 69 | ///
|
75 |
| -/// assert_eq!(a, &b * &c); |
| 70 | +/// // Example 3: azip!() on references |
| 71 | +/// // See the definition of the function below |
| 72 | +/// borrow_multiply(&mut a, &b, &c); |
76 | 73 | ///
|
| 74 | +/// assert_eq!(a, &b * &c); |
77 | 75 | ///
|
78 |
| -/// // Since this function borrows its inputs, the `IntoNdProducer` |
79 |
| -/// // expressions don't need to explicitly include `&mut` or `&`. |
80 |
| -/// fn borrow_multiply(a: &mut M, b: &M, c: &M) { |
81 |
| -/// azip!((a in a, &b in b, &c in c) *a = b * c); |
82 |
| -/// } |
83 | 76 | ///
|
| 77 | +/// // Since this function borrows its inputs, the `IntoNdProducer` |
| 78 | +/// // expressions don't need to explicitly include `&mut` or `&`. |
| 79 | +/// fn borrow_multiply(a: &mut M, b: &M, c: &M) { |
| 80 | +/// azip!((a in a, &b in b, &c in c) *a = b * c); |
| 81 | +/// } |
84 | 82 | ///
|
85 |
| -/// // Example 4: using azip!() without dereference in pattern. |
86 |
| -/// // |
87 |
| -/// // Create a new array `totals` with one entry per row of `a`. |
88 |
| -/// // Use azip to traverse the rows of `a` and assign to the corresponding |
89 |
| -/// // entry in `totals` with the sum across each row. |
90 |
| -/// // |
91 |
| -/// // The row is an array view; it doesn't need to be dereferenced. |
92 |
| -/// let mut totals = Array1::zeros(a.nrows()); |
93 |
| -/// azip!((totals in &mut totals, row in a.rows()) *totals = row.sum()); |
94 | 83 | ///
|
95 |
| -/// // Check the result against the built in `.sum_axis()` along axis 1. |
96 |
| -/// assert_eq!(totals, a.sum_axis(Axis(1))); |
97 |
| -/// } |
| 84 | +/// // Example 4: using azip!() without dereference in pattern. |
| 85 | +/// // |
| 86 | +/// // Create a new array `totals` with one entry per row of `a`. |
| 87 | +/// // Use azip to traverse the rows of `a` and assign to the corresponding |
| 88 | +/// // entry in `totals` with the sum across each row. |
| 89 | +/// // |
| 90 | +/// // The row is an array view; it doesn't need to be dereferenced. |
| 91 | +/// let mut totals = Array1::zeros(a.nrows()); |
| 92 | +/// azip!((totals in &mut totals, row in a.rows()) *totals = row.sum()); |
98 | 93 | ///
|
| 94 | +/// // Check the result against the built in `.sum_axis()` along axis 1. |
| 95 | +/// assert_eq!(totals, a.sum_axis(Axis(1))); |
99 | 96 | /// ```
|
100 | 97 | #[macro_export]
|
101 | 98 | macro_rules! azip {
|
|
0 commit comments