Skip to content

Commit ba8f45c

Browse files
authored
Merge pull request #1310 from rust-ndarray/better-into-shape
Better shape: Deprecate reshape, into_shape
2 parents cd0a956 + d32248d commit ba8f45c

34 files changed

+456
-199
lines changed

README-quick-start.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ fn main() {
169169
println!("a shape {:?}", &a.shape());
170170
println!("b shape {:?}", &b.shape());
171171

172-
let b = b.into_shape((4,1)).unwrap(); // reshape b to shape [4, 1]
172+
let b = b.into_shape_with_order((4,1)).unwrap(); // reshape b to shape [4, 1]
173173
println!("b shape after reshape {:?}", &b.shape());
174174

175175
println!("{}", a.dot(&b)); // [1, 4] x [4, 1] -> [1, 1]
@@ -295,7 +295,7 @@ row: [[100, 101, 102],
295295
## Shape Manipulation
296296

297297
### Changing the shape of an array
298-
The shape of an array can be changed with `into_shape` method.
298+
The shape of an array can be changed with the `into_shape_with_order` or `to_shape` method.
299299

300300
````rust
301301
use ndarray::prelude::*;
@@ -319,7 +319,7 @@ fn main() {
319319
let b = Array::from_iter(a.iter());
320320
println!("b = \n{:?}\n", b);
321321

322-
let c = b.into_shape([6, 2]).unwrap(); // consume b and generate c with new shape
322+
let c = b.into_shape_with_order([6, 2]).unwrap(); // consume b and generate c with new shape
323323
println!("c = \n{:?}", c);
324324
}
325325
````
@@ -459,7 +459,7 @@ use ndarray::{Array, Axis};
459459

460460
fn main() {
461461

462-
let mut a = Array::range(0., 12., 1.).into_shape([3 ,4]).unwrap();
462+
let mut a = Array::range(0., 12., 1.).into_shape_with_order([3 ,4]).unwrap();
463463
println!("a = \n{}\n", a);
464464

465465
{
@@ -519,7 +519,7 @@ use ndarray::Array;
519519

520520
fn main() {
521521

522-
let mut a = Array::range(0., 4., 1.).into_shape([2 ,2]).unwrap();
522+
let mut a = Array::range(0., 4., 1.).into_shape_with_order([2 ,2]).unwrap();
523523
let b = a.clone();
524524

525525
println!("a = \n{}\n", a);

benches/bench1.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -915,7 +915,7 @@ const MEAN_SUM_N: usize = 127;
915915
fn range_mat(m: Ix, n: Ix) -> Array2<f32> {
916916
assert!(m * n != 0);
917917
Array::linspace(0., (m * n - 1) as f32, m * n)
918-
.into_shape((m, n))
918+
.into_shape_with_order((m, n))
919919
.unwrap()
920920
}
921921

benches/construct.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ fn zeros_f64(bench: &mut Bencher) {
2222

2323
#[bench]
2424
fn map_regular(bench: &mut test::Bencher) {
25-
let a = Array::linspace(0., 127., 128).into_shape((8, 16)).unwrap();
25+
let a = Array::linspace(0., 127., 128).into_shape_with_order((8, 16)).unwrap();
2626
bench.iter(|| a.map(|&x| 2. * x));
2727
}
2828

2929
#[bench]
3030
fn map_stride(bench: &mut test::Bencher) {
31-
let a = Array::linspace(0., 127., 256).into_shape((8, 32)).unwrap();
31+
let a = Array::linspace(0., 127., 256).into_shape_with_order((8, 32)).unwrap();
3232
let av = a.slice(s![.., ..;2]);
3333
bench.iter(|| av.map(|&x| 2. * x));
3434
}

benches/higher-order.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const Y: usize = 16;
1717

1818
#[bench]
1919
fn map_regular(bench: &mut Bencher) {
20-
let a = Array::linspace(0., 127., N).into_shape((X, Y)).unwrap();
20+
let a = Array::linspace(0., 127., N).into_shape_with_order((X, Y)).unwrap();
2121
bench.iter(|| a.map(|&x| 2. * x));
2222
}
2323

@@ -28,7 +28,7 @@ pub fn double_array(mut a: ArrayViewMut2<'_, f64>) {
2828
#[bench]
2929
fn map_stride_double_f64(bench: &mut Bencher) {
3030
let mut a = Array::linspace(0., 127., N * 2)
31-
.into_shape([X, Y * 2])
31+
.into_shape_with_order([X, Y * 2])
3232
.unwrap();
3333
let mut av = a.slice_mut(s![.., ..;2]);
3434
bench.iter(|| {
@@ -39,7 +39,7 @@ fn map_stride_double_f64(bench: &mut Bencher) {
3939
#[bench]
4040
fn map_stride_f64(bench: &mut Bencher) {
4141
let a = Array::linspace(0., 127., N * 2)
42-
.into_shape([X, Y * 2])
42+
.into_shape_with_order([X, Y * 2])
4343
.unwrap();
4444
let av = a.slice(s![.., ..;2]);
4545
bench.iter(|| av.map(|&x| 2. * x));
@@ -48,7 +48,7 @@ fn map_stride_f64(bench: &mut Bencher) {
4848
#[bench]
4949
fn map_stride_u32(bench: &mut Bencher) {
5050
let a = Array::linspace(0., 127., N * 2)
51-
.into_shape([X, Y * 2])
51+
.into_shape_with_order([X, Y * 2])
5252
.unwrap();
5353
let b = a.mapv(|x| x as u32);
5454
let av = b.slice(s![.., ..;2]);
@@ -58,7 +58,7 @@ fn map_stride_u32(bench: &mut Bencher) {
5858
#[bench]
5959
fn fold_axis(bench: &mut Bencher) {
6060
let a = Array::linspace(0., 127., N * 2)
61-
.into_shape([X, Y * 2])
61+
.into_shape_with_order([X, Y * 2])
6262
.unwrap();
6363
bench.iter(|| a.fold_axis(Axis(0), 0., |&acc, &elt| acc + elt));
6464
}
@@ -69,15 +69,15 @@ const MASZ: usize = MA * MA;
6969
#[bench]
7070
fn map_axis_0(bench: &mut Bencher) {
7171
let a = Array::from_iter(0..MASZ as i32)
72-
.into_shape([MA, MA])
72+
.into_shape_with_order([MA, MA])
7373
.unwrap();
7474
bench.iter(|| a.map_axis(Axis(0), black_box));
7575
}
7676

7777
#[bench]
7878
fn map_axis_1(bench: &mut Bencher) {
7979
let a = Array::from_iter(0..MASZ as i32)
80-
.into_shape([MA, MA])
80+
.into_shape_with_order([MA, MA])
8181
.unwrap();
8282
bench.iter(|| a.map_axis(Axis(1), black_box));
8383
}

benches/iter.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,29 +46,29 @@ fn iter_sum_2d_transpose(bench: &mut Bencher) {
4646

4747
#[bench]
4848
fn iter_filter_sum_2d_u32(bench: &mut Bencher) {
49-
let a = Array::linspace(0., 1., 256).into_shape((16, 16)).unwrap();
49+
let a = Array::linspace(0., 1., 256).into_shape_with_order((16, 16)).unwrap();
5050
let b = a.mapv(|x| (x * 100.) as u32);
5151
bench.iter(|| b.iter().filter(|&&x| x < 75).sum::<u32>());
5252
}
5353

5454
#[bench]
5555
fn iter_filter_sum_2d_f32(bench: &mut Bencher) {
56-
let a = Array::linspace(0., 1., 256).into_shape((16, 16)).unwrap();
56+
let a = Array::linspace(0., 1., 256).into_shape_with_order((16, 16)).unwrap();
5757
let b = a * 100.;
5858
bench.iter(|| b.iter().filter(|&&x| x < 75.).sum::<f32>());
5959
}
6060

6161
#[bench]
6262
fn iter_filter_sum_2d_stride_u32(bench: &mut Bencher) {
63-
let a = Array::linspace(0., 1., 256).into_shape((16, 16)).unwrap();
63+
let a = Array::linspace(0., 1., 256).into_shape_with_order((16, 16)).unwrap();
6464
let b = a.mapv(|x| (x * 100.) as u32);
6565
let b = b.slice(s![.., ..;2]);
6666
bench.iter(|| b.iter().filter(|&&x| x < 75).sum::<u32>());
6767
}
6868

6969
#[bench]
7070
fn iter_filter_sum_2d_stride_f32(bench: &mut Bencher) {
71-
let a = Array::linspace(0., 1., 256).into_shape((16, 16)).unwrap();
71+
let a = Array::linspace(0., 1., 256).into_shape_with_order((16, 16)).unwrap();
7272
let b = a * 100.;
7373
let b = b.slice(s![.., ..;2]);
7474
bench.iter(|| b.iter().filter(|&&x| x < 75.).sum::<f32>());
@@ -321,7 +321,7 @@ fn indexed_iter_3d_dyn(bench: &mut Bencher) {
321321
for ((i, j, k), elt) in a.indexed_iter_mut() {
322322
*elt = (i + 100 * j + 10000 * k) as _;
323323
}
324-
let a = a.into_shape(&[ISZ; 3][..]).unwrap();
324+
let a = a.into_shape_with_order(&[ISZ; 3][..]).unwrap();
325325

326326
bench.iter(|| {
327327
for (i, &_elt) in a.indexed_iter() {

benches/numeric.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const Y: usize = 16;
1212
#[bench]
1313
fn clip(bench: &mut Bencher) {
1414
let mut a = Array::linspace(0., 127., N * 2)
15-
.into_shape([X, Y * 2])
15+
.into_shape_with_order([X, Y * 2])
1616
.unwrap();
1717
let min = 2.;
1818
let max = 5.;

examples/axis_ops.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ fn main() {
7474
}
7575
regularize(&mut b).unwrap();
7676

77-
let mut b = b.into_shape(a.len()).unwrap();
77+
let mut b = b.into_shape_with_order(a.len()).unwrap();
7878
regularize(&mut b).unwrap();
7979

8080
b.invert_axis(Axis(0));

examples/life.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ fn parse(x: &[u8]) -> Board {
2222
_ => None,
2323
}));
2424

25-
let a = a.into_shape((N, N)).unwrap();
25+
let a = a.into_shape_with_order((N, N)).unwrap();
2626
map.slice_mut(s![1..-1, 1..-1]).assign(&a);
2727
map
2828
}

examples/sort-axis.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ where
166166

167167
#[cfg(feature = "std")]
168168
fn main() {
169-
let a = Array::linspace(0., 63., 64).into_shape((8, 8)).unwrap();
169+
let a = Array::linspace(0., 63., 64).into_shape_with_order((8, 8)).unwrap();
170170
let strings = a.map(|x| x.to_string());
171171

172172
let perm = a.sort_axis_by(Axis(1), |i, j| a[[i, 0]] > a[[j, 0]]);

src/free_functions.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ pub const fn aview0<A>(x: &A) -> ArrayView0<'_, A> {
8888
/// let data = [1.0; 1024];
8989
///
9090
/// // Create a 2D array view from borrowed data
91-
/// let a2d = aview1(&data).into_shape((32, 32)).unwrap();
91+
/// let a2d = aview1(&data).into_shape_with_order((32, 32)).unwrap();
9292
///
9393
/// assert_eq!(a2d.sum(), 1024.0);
9494
///
@@ -174,7 +174,7 @@ pub const fn aview2<A, const N: usize>(xs: &[[A; N]]) -> ArrayView2<'_, A> {
174174
/// // Create an array view over some data, then slice it and modify it.
175175
/// let mut data = [0; 1024];
176176
/// {
177-
/// let mut a = aview_mut1(&mut data).into_shape((32, 32)).unwrap();
177+
/// let mut a = aview_mut1(&mut data).into_shape_with_order((32, 32)).unwrap();
178178
/// a.slice_mut(s![.., ..;3]).fill(5);
179179
/// }
180180
/// assert_eq!(&data[..10], [5, 0, 0, 5, 0, 0, 5, 0, 0, 5]);

src/impl_constructors.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ where
329329
A: Clone,
330330
Sh: ShapeBuilder<Dim = D>,
331331
{
332-
let shape = shape.into_shape();
332+
let shape = shape.into_shape_with_order();
333333
let size = size_of_shape_checked_unwrap!(&shape.dim);
334334
let v = vec![elem; size];
335335
unsafe { Self::from_shape_vec_unchecked(shape, v) }
@@ -383,7 +383,7 @@ where
383383
Sh: ShapeBuilder<Dim = D>,
384384
F: FnMut() -> A,
385385
{
386-
let shape = shape.into_shape();
386+
let shape = shape.into_shape_with_order();
387387
let len = size_of_shape_checked_unwrap!(&shape.dim);
388388
let v = to_vec_mapped(0..len, move |_| f());
389389
unsafe { Self::from_shape_vec_unchecked(shape, v) }
@@ -414,7 +414,7 @@ where
414414
Sh: ShapeBuilder<Dim = D>,
415415
F: FnMut(D::Pattern) -> A,
416416
{
417-
let shape = shape.into_shape();
417+
let shape = shape.into_shape_with_order();
418418
let _ = size_of_shape_checked_unwrap!(&shape.dim);
419419
if shape.is_c() {
420420
let v = to_vec_mapped(indices(shape.dim.clone()).into_iter(), f);
@@ -591,7 +591,7 @@ where
591591
Sh: ShapeBuilder<Dim = D>,
592592
{
593593
unsafe {
594-
let shape = shape.into_shape();
594+
let shape = shape.into_shape_with_order();
595595
let size = size_of_shape_checked_unwrap!(&shape.dim);
596596
let mut v = Vec::with_capacity(size);
597597
v.set_len(size);
@@ -664,7 +664,7 @@ where
664664
A: Copy,
665665
Sh: ShapeBuilder<Dim = D>,
666666
{
667-
let shape = shape.into_shape();
667+
let shape = shape.into_shape_with_order();
668668
let size = size_of_shape_checked_unwrap!(&shape.dim);
669669
let mut v = Vec::with_capacity(size);
670670
v.set_len(size);
@@ -687,7 +687,7 @@ where
687687
Sh: ShapeBuilder<Dim = D>,
688688
{
689689
unsafe {
690-
let shape = shape.into_shape();
690+
let shape = shape.into_shape_with_order();
691691
let size = size_of_shape_checked_unwrap!(&shape.dim);
692692
let mut v = Vec::with_capacity(size);
693693
v.set_len(size);

0 commit comments

Comments
 (0)