Skip to content

Commit a27855c

Browse files
author
andrei
committed
Renamed stack and concatenate to be compliant with numpy naming.
1 parent e808e2b commit a27855c

File tree

3 files changed

+55
-75
lines changed

3 files changed

+55
-75
lines changed

src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,6 @@ use crate::iterators::{ElementsBase, ElementsBaseMut, Iter, IterMut, Lanes, Lane
132132
pub use crate::arraytraits::AsArray;
133133
pub use crate::linalg_traits::{LinalgScalar, NdFloat};
134134

135-
#[allow(deprecated)]
136135
pub use crate::stacking::{concatenate, stack, stack_new_axis};
137136

138137
pub use crate::impl_views::IndexLonger;

src/stacking.rs

Lines changed: 48 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,42 @@
99
use crate::error::{from_kind, ErrorKind, ShapeError};
1010
use crate::imp_prelude::*;
1111

12+
/// Stack arrays along the new axis.
13+
///
14+
/// ***Errors*** if the arrays have mismatching shapes.
15+
/// ***Errors*** if `arrays` is empty, if `axis` is out of bounds,
16+
/// if the result is larger than is possible to represent.
17+
///
18+
/// ```
19+
/// extern crate ndarray;
20+
///
21+
/// use ndarray::{arr2, arr3, stack, Axis};
22+
///
23+
/// # fn main() {
24+
///
25+
/// let a = arr2(&[[2., 2.],
26+
/// [3., 3.]]);
27+
/// assert!(
28+
/// stack(Axis(0), &[a.view(), a.view()])
29+
/// == Ok(arr3(&[[[2., 2.],
30+
/// [3., 3.]],
31+
/// [[2., 2.],
32+
/// [3., 3.]]]))
33+
/// );
34+
/// # }
35+
/// ```
36+
pub fn stack<A, D>(
37+
axis: Axis,
38+
arrays: &[ArrayView<A, D>],
39+
) -> Result<Array<A, D::Larger>, ShapeError>
40+
where
41+
A: Copy,
42+
D: Dimension,
43+
D::Larger: RemoveAxis,
44+
{
45+
stack_new_axis(axis, arrays)
46+
}
47+
1248
/// Concatenate arrays along the given axis.
1349
///
1450
/// ***Errors*** if the arrays have mismatching shapes, apart from along `axis`.
@@ -17,23 +53,20 @@ use crate::imp_prelude::*;
1753
/// if the result is larger than is possible to represent.
1854
///
1955
/// ```
20-
/// use ndarray::{arr2, Axis, stack};
56+
/// use ndarray::{arr2, Axis, concatenate};
2157
///
2258
/// let a = arr2(&[[2., 2.],
2359
/// [3., 3.]]);
2460
/// assert!(
25-
/// stack(Axis(0), &[a.view(), a.view()])
61+
/// concatenate(Axis(0), &[a.view(), a.view()])
2662
/// == Ok(arr2(&[[2., 2.],
2763
/// [3., 3.],
2864
/// [2., 2.],
2965
/// [3., 3.]]))
3066
/// );
3167
/// ```
32-
#[deprecated(
33-
since = "0.13.2",
34-
note = "Please use the `concatenate` function instead"
35-
)]
36-
pub fn stack<A, D>(axis: Axis, arrays: &[ArrayView<A, D>]) -> Result<Array<A, D>, ShapeError>
68+
#[allow(deprecated)]
69+
pub fn concatenate<A, D>(axis: Axis, arrays: &[ArrayView<A, D>]) -> Result<Array<A, D>, ShapeError>
3770
where
3871
A: Copy,
3972
D: RemoveAxis,
@@ -77,35 +110,6 @@ where
77110
Ok(res)
78111
}
79112

80-
/// Concatenate arrays along the given axis.
81-
///
82-
/// ***Errors*** if the arrays have mismatching shapes, apart from along `axis`.
83-
/// (may be made more flexible in the future).<br>
84-
/// ***Errors*** if `arrays` is empty, if `axis` is out of bounds,
85-
/// if the result is larger than is possible to represent.
86-
///
87-
/// ```
88-
/// use ndarray::{arr2, Axis, concatenate};
89-
///
90-
/// let a = arr2(&[[2., 2.],
91-
/// [3., 3.]]);
92-
/// assert!(
93-
/// concatenate(Axis(0), &[a.view(), a.view()])
94-
/// == Ok(arr2(&[[2., 2.],
95-
/// [3., 3.],
96-
/// [2., 2.],
97-
/// [3., 3.]]))
98-
/// );
99-
/// ```
100-
#[allow(deprecated)]
101-
pub fn concatenate<A, D>(axis: Axis, arrays: &[ArrayView<A, D>]) -> Result<Array<A, D>, ShapeError>
102-
where
103-
A: Copy,
104-
D: RemoveAxis,
105-
{
106-
stack(axis, arrays)
107-
}
108-
109113
/// Stack arrays along the new axis.
110114
///
111115
/// ***Errors*** if the arrays have mismatching shapes.
@@ -173,7 +177,7 @@ where
173177
Ok(res)
174178
}
175179

176-
/// Concatenate arrays along the given axis.
180+
/// Stack arrays along the new axis.
177181
///
178182
/// Uses the [`stack`][1] function, calling `ArrayView::from(&a)` on each
179183
/// argument `a`.
@@ -183,25 +187,23 @@ where
183187
/// ***Panics*** if the `stack` function would return an error.
184188
///
185189
/// ```
186-
/// use ndarray::{arr2, stack, Axis};
190+
/// extern crate ndarray;
191+
///
192+
/// use ndarray::{arr2, arr3, stack, Axis};
187193
///
188194
/// # fn main() {
189195
///
190196
/// let a = arr2(&[[2., 2.],
191197
/// [3., 3.]]);
192198
/// assert!(
193199
/// stack![Axis(0), a, a]
194-
/// == arr2(&[[2., 2.],
195-
/// [3., 3.],
196-
/// [2., 2.],
197-
/// [3., 3.]])
200+
/// == arr3(&[[[2., 2.],
201+
/// [3., 3.]],
202+
/// [[2., 2.],
203+
/// [3., 3.]]])
198204
/// );
199205
/// # }
200206
/// ```
201-
#[deprecated(
202-
since = "0.13.2",
203-
note = "Please use the `concatenate!` macro instead"
204-
)]
205207
#[macro_export]
206208
macro_rules! stack {
207209
($axis:expr, $( $array:expr ),+ ) => {

tests/stacking.rs

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,7 @@
1-
#![allow(deprecated)]
2-
31
use ndarray::{arr2, arr3, aview1, concatenate, stack, Array2, Axis, ErrorKind, Ix1};
42

53
#[test]
64
fn concatenating() {
7-
let a = arr2(&[[2., 2.], [3., 3.]]);
8-
let b = ndarray::stack(Axis(0), &[a.view(), a.view()]).unwrap();
9-
assert_eq!(b, arr2(&[[2., 2.], [3., 3.], [2., 2.], [3., 3.]]));
10-
11-
let c = stack![Axis(0), a, b];
12-
assert_eq!(
13-
c,
14-
arr2(&[[2., 2.], [3., 3.], [2., 2.], [3., 3.], [2., 2.], [3., 3.]])
15-
);
16-
17-
let d = stack![Axis(0), a.row(0), &[9., 9.]];
18-
assert_eq!(d, aview1(&[2., 2., 9., 9.]));
19-
20-
let res = ndarray::stack(Axis(1), &[a.view(), c.view()]);
21-
assert_eq!(res.unwrap_err().kind(), ErrorKind::IncompatibleShape);
22-
23-
let res = ndarray::stack(Axis(2), &[a.view(), c.view()]);
24-
assert_eq!(res.unwrap_err().kind(), ErrorKind::OutOfBounds);
25-
26-
let res: Result<Array2<f64>, _> = ndarray::stack(Axis(0), &[]);
27-
assert_eq!(res.unwrap_err().kind(), ErrorKind::Unsupported);
28-
295
let a = arr2(&[[2., 2.], [3., 3.]]);
306
let b = ndarray::concatenate(Axis(0), &[a.view(), a.view()]).unwrap();
317
assert_eq!(b, arr2(&[[2., 2.], [3., 3.], [2., 2.], [3., 3.]]));
@@ -52,16 +28,19 @@ fn concatenating() {
5228
#[test]
5329
fn stacking() {
5430
let a = arr2(&[[2., 2.], [3., 3.]]);
55-
let b = ndarray::stack_new_axis(Axis(0), &[a.view(), a.view()]).unwrap();
31+
let b = ndarray::stack(Axis(0), &[a.view(), a.view()]).unwrap();
5632
assert_eq!(b, arr3(&[[[2., 2.], [3., 3.]], [[2., 2.], [3., 3.]]]));
5733

34+
let c = stack![Axis(0), a, a];
35+
assert_eq!(c, arr3(&[[[2., 2.], [3., 3.]], [[2., 2.], [3., 3.]]]));
36+
5837
let c = arr2(&[[3., 2., 3.], [2., 3., 2.]]);
59-
let res = ndarray::stack_new_axis(Axis(1), &[a.view(), c.view()]);
38+
let res = ndarray::stack(Axis(1), &[a.view(), c.view()]);
6039
assert_eq!(res.unwrap_err().kind(), ErrorKind::IncompatibleShape);
6140

62-
let res = ndarray::stack_new_axis(Axis(3), &[a.view(), a.view()]);
41+
let res = ndarray::stack(Axis(3), &[a.view(), a.view()]);
6342
assert_eq!(res.unwrap_err().kind(), ErrorKind::OutOfBounds);
6443

65-
let res: Result<Array2<f64>, _> = ndarray::stack_new_axis::<_, Ix1>(Axis(0), &[]);
44+
let res: Result<Array2<f64>, _> = ndarray::stack::<_, Ix1>(Axis(0), &[]);
6645
assert_eq!(res.unwrap_err().kind(), ErrorKind::Unsupported);
6746
}

0 commit comments

Comments
 (0)