Skip to content

Commit cd69772

Browse files
committed
shape: Use into_shape_with_order, not into_shape
1 parent cfd84e2 commit cd69772

33 files changed

+155
-152
lines changed

README-quick-start.md

Lines changed: 6 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,8 @@ 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 `into_shape_with_order` method.
299+
(One can also use `to_shape` for this.)
299300

300301
````rust
301302
use ndarray::prelude::*;
@@ -319,7 +320,7 @@ fn main() {
319320
let b = Array::from_iter(a.iter());
320321
println!("b = \n{:?}\n", b);
321322

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

460461
fn main() {
461462

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

465466
{
@@ -519,7 +520,7 @@ use ndarray::Array;
519520

520521
fn main() {
521522

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

525526
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
@@ -76,7 +76,7 @@ pub fn aview0<A>(x: &A) -> ArrayView0<'_, A> {
7676
/// let data = [1.0; 1024];
7777
///
7878
/// // Create a 2D array view from borrowed data
79-
/// let a2d = aview1(&data).into_shape((32, 32)).unwrap();
79+
/// let a2d = aview1(&data).into_shape_with_order((32, 32)).unwrap();
8080
///
8181
/// assert_eq!(a2d.sum(), 1024.0);
8282
/// ```
@@ -99,7 +99,7 @@ pub fn aview2<A, const N: usize>(xs: &[[A; N]]) -> ArrayView2<'_, A> {
9999
/// // Create an array view over some data, then slice it and modify it.
100100
/// let mut data = [0; 1024];
101101
/// {
102-
/// let mut a = aview_mut1(&mut data).into_shape((32, 32)).unwrap();
102+
/// let mut a = aview_mut1(&mut data).into_shape_with_order((32, 32)).unwrap();
103103
/// a.slice_mut(s![.., ..;3]).fill(5);
104104
/// }
105105
/// 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
@@ -328,7 +328,7 @@ where
328328
A: Clone,
329329
Sh: ShapeBuilder<Dim = D>,
330330
{
331-
let shape = shape.into_shape();
331+
let shape = shape.into_shape_with_order();
332332
let size = size_of_shape_checked_unwrap!(&shape.dim);
333333
let v = vec![elem; size];
334334
unsafe { Self::from_shape_vec_unchecked(shape, v) }
@@ -382,7 +382,7 @@ where
382382
Sh: ShapeBuilder<Dim = D>,
383383
F: FnMut() -> A,
384384
{
385-
let shape = shape.into_shape();
385+
let shape = shape.into_shape_with_order();
386386
let len = size_of_shape_checked_unwrap!(&shape.dim);
387387
let v = to_vec_mapped(0..len, move |_| f());
388388
unsafe { Self::from_shape_vec_unchecked(shape, v) }
@@ -413,7 +413,7 @@ where
413413
Sh: ShapeBuilder<Dim = D>,
414414
F: FnMut(D::Pattern) -> A,
415415
{
416-
let shape = shape.into_shape();
416+
let shape = shape.into_shape_with_order();
417417
let _ = size_of_shape_checked_unwrap!(&shape.dim);
418418
if shape.is_c() {
419419
let v = to_vec_mapped(indices(shape.dim.clone()).into_iter(), f);
@@ -590,7 +590,7 @@ where
590590
Sh: ShapeBuilder<Dim = D>,
591591
{
592592
unsafe {
593-
let shape = shape.into_shape();
593+
let shape = shape.into_shape_with_order();
594594
let size = size_of_shape_checked_unwrap!(&shape.dim);
595595
let mut v = Vec::with_capacity(size);
596596
v.set_len(size);
@@ -663,7 +663,7 @@ where
663663
A: Copy,
664664
Sh: ShapeBuilder<Dim = D>,
665665
{
666-
let shape = shape.into_shape();
666+
let shape = shape.into_shape_with_order();
667667
let size = size_of_shape_checked_unwrap!(&shape.dim);
668668
let mut v = Vec::with_capacity(size);
669669
v.set_len(size);
@@ -686,7 +686,7 @@ where
686686
Sh: ShapeBuilder<Dim = D>,
687687
{
688688
unsafe {
689-
let shape = shape.into_shape();
689+
let shape = shape.into_shape_with_order();
690690
let size = size_of_shape_checked_unwrap!(&shape.dim);
691691
let mut v = Vec::with_capacity(size);
692692
v.set_len(size);

src/impl_methods.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1322,7 +1322,7 @@ where
13221322
/// use ndarray::Array;
13231323
/// use ndarray::{arr3, Axis};
13241324
///
1325-
/// let a = Array::from_iter(0..28).into_shape((2, 7, 2)).unwrap();
1325+
/// let a = Array::from_iter(0..28).into_shape_with_order((2, 7, 2)).unwrap();
13261326
/// let mut iter = a.axis_chunks_iter(Axis(1), 2);
13271327
///
13281328
/// // first iteration yields a 2 × 2 × 2 view
@@ -1955,7 +1955,7 @@ where
19551955
/// elements is accepted, but the source array or view must be in standard
19561956
/// or column-major (Fortran) layout.
19571957
///
1958-
/// **Note** that `.into_shape()` "moves" elements differently depending on if the input array
1958+
/// **Note** that `.into_shape_with_order()` "moves" elements differently depending on if the input array
19591959
/// is C-contig or F-contig, it follows the index order that corresponds to the memory order.
19601960
/// Prefer to use `.to_shape()` or `.into_shape_with_order()`.
19611961
///
@@ -1969,7 +1969,7 @@ where
19691969
/// use ndarray::{aview1, aview2};
19701970
///
19711971
/// assert!(
1972-
/// aview1(&[1., 2., 3., 4.]).into_shape((2, 2)).unwrap()
1972+
/// aview1(&[1., 2., 3., 4.]).into_shape_with_order((2, 2)).unwrap()
19731973
/// == aview2(&[[1., 2.],
19741974
/// [3., 4.]])
19751975
/// );
@@ -2049,7 +2049,7 @@ where
20492049
}
20502050
}
20512051

2052-
/// *Note: Reshape is for `ArcArray` only. Use `.into_shape()` for
2052+
/// *Note: Reshape is for `ArcArray` only. Use `.into_shape_with_order()` for
20532053
/// other arrays and array views.*
20542054
///
20552055
/// Transform the array into `shape`; any shape with the same number of
@@ -2072,7 +2072,7 @@ where
20722072
/// [3., 4.]])
20732073
/// );
20742074
/// ```
2075-
#[deprecated(note="Obsolete, use `to_shape` or `into_shape` instead.", since="0.15.2")]
2075+
#[deprecated(note="Obsolete, use `to_shape` or `into_shape_with_order` instead.", since="0.15.2")]
20762076
pub fn reshape<E>(&self, shape: E) -> ArrayBase<S, E::Dim>
20772077
where
20782078
S: DataShared + DataOwned,

src/impl_owned_array.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ impl<A, D> Array<A, D>
231231
/// ```
232232
/// use ndarray::Array;
233233
///
234-
/// let a = Array::from_iter(0..100).into_shape((10, 10)).unwrap();
234+
/// let a = Array::from_iter(0..100).into_shape_with_order((10, 10)).unwrap();
235235
/// let mut b = Array::uninit((10, 10));
236236
/// a.move_into_uninit(&mut b);
237237
/// unsafe {
@@ -447,8 +447,8 @@ impl<A, D> Array<A, D>
447447
///
448448
/// // create an empty array and append two rows at a time
449449
/// let mut a = Array::zeros((0, 4));
450-
/// let ones = ArrayView::from(&[1.; 8]).into_shape((2, 4)).unwrap();
451-
/// let zeros = ArrayView::from(&[0.; 8]).into_shape((2, 4)).unwrap();
450+
/// let ones = ArrayView::from(&[1.; 8]).into_shape_with_order((2, 4)).unwrap();
451+
/// let zeros = ArrayView::from(&[0.; 8]).into_shape_with_order((2, 4)).unwrap();
452452
/// a.append(Axis(0), ones).unwrap();
453453
/// a.append(Axis(0), zeros).unwrap();
454454
/// a.append(Axis(0), ones).unwrap();

src/impl_views/indexing.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ use crate::NdIndex;
3333
/// let data = [0.; 256];
3434
/// let long_life_ref = {
3535
/// // make a 16 × 16 array view
36-
/// let view = ArrayView::from(&data[..]).into_shape((16, 16)).unwrap();
36+
/// let view = ArrayView::from(&data[..]).into_shape_with_order((16, 16)).unwrap();
3737
///
3838
/// // index the view and with `IndexLonger`.
3939
/// // Note here that we get a reference with a life that is derived from

src/parallel/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
//! use ndarray::Axis;
6666
//! use ndarray::parallel::prelude::*;
6767
//!
68-
//! let a = Array::linspace(0., 63., 64).into_shape((4, 16)).unwrap();
68+
//! let a = Array::linspace(0., 63., 64).into_shape_with_order((4, 16)).unwrap();
6969
//! let mut sums = Vec::new();
7070
//! a.axis_iter(Axis(0))
7171
//! .into_par_iter()
@@ -84,7 +84,7 @@
8484
//! use ndarray::Axis;
8585
//! use ndarray::parallel::prelude::*;
8686
//!
87-
//! let a = Array::linspace(0., 63., 64).into_shape((4, 16)).unwrap();
87+
//! let a = Array::linspace(0., 63., 64).into_shape_with_order((4, 16)).unwrap();
8888
//! let mut shapes = Vec::new();
8989
//! a.axis_chunks_iter(Axis(0), 3)
9090
//! .into_par_iter()

0 commit comments

Comments
 (0)