Skip to content

Commit 716221c

Browse files
authored
Merge pull request #850 from andrei-papou/stack-concatenate-renaming
Make stack and concatenate compliant with numpy naming.
2 parents 2bae4cc + 13dbaf1 commit 716221c

File tree

3 files changed

+54
-75
lines changed

3 files changed

+54
-75
lines changed

src/lib.rs

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

138-
#[allow(deprecated)]
139138
pub use crate::stacking::{concatenate, stack, stack_new_axis};
140139

141140
pub use crate::impl_views::IndexLonger;

src/stacking.rs

Lines changed: 47 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,19 @@ 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+
pub fn concatenate<A, D>(axis: Axis, arrays: &[ArrayView<A, D>]) -> Result<Array<A, D>, ShapeError>
3769
where
3870
A: Copy,
3971
D: RemoveAxis,
@@ -77,35 +109,6 @@ where
77109
Ok(res)
78110
}
79111

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-
109112
/// Stack arrays along the new axis.
110113
///
111114
/// ***Errors*** if the arrays have mismatching shapes.
@@ -173,7 +176,7 @@ where
173176
Ok(res)
174177
}
175178

176-
/// Concatenate arrays along the given axis.
179+
/// Stack arrays along the new axis.
177180
///
178181
/// Uses the [`stack`][1] function, calling `ArrayView::from(&a)` on each
179182
/// argument `a`.
@@ -183,25 +186,23 @@ where
183186
/// ***Panics*** if the `stack` function would return an error.
184187
///
185188
/// ```
186-
/// use ndarray::{arr2, stack, Axis};
189+
/// extern crate ndarray;
190+
///
191+
/// use ndarray::{arr2, arr3, stack, Axis};
187192
///
188193
/// # fn main() {
189194
///
190195
/// let a = arr2(&[[2., 2.],
191196
/// [3., 3.]]);
192197
/// assert!(
193198
/// stack![Axis(0), a, a]
194-
/// == arr2(&[[2., 2.],
195-
/// [3., 3.],
196-
/// [2., 2.],
197-
/// [3., 3.]])
199+
/// == arr3(&[[[2., 2.],
200+
/// [3., 3.]],
201+
/// [[2., 2.],
202+
/// [3., 3.]]])
198203
/// );
199204
/// # }
200205
/// ```
201-
#[deprecated(
202-
since = "0.13.2",
203-
note = "Please use the `concatenate!` macro instead"
204-
)]
205206
#[macro_export]
206207
macro_rules! stack {
207208
($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)