Skip to content

Commit

Permalink
shape: Use into_shape_with_order, not into_shape
Browse files Browse the repository at this point in the history
  • Loading branch information
bluss committed Feb 26, 2024
1 parent cfd84e2 commit cd69772
Show file tree
Hide file tree
Showing 33 changed files with 155 additions and 152 deletions.
11 changes: 6 additions & 5 deletions README-quick-start.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ fn main() {
println!("a shape {:?}", &a.shape());
println!("b shape {:?}", &b.shape());

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

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

### Changing the shape of an array
The shape of an array can be changed with `into_shape` method.
The shape of an array can be changed with `into_shape_with_order` method.
(One can also use `to_shape` for this.)

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

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

fn main() {

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

{
Expand Down Expand Up @@ -519,7 +520,7 @@ use ndarray::Array;

fn main() {

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

println!("a = \n{}\n", a);
Expand Down
2 changes: 1 addition & 1 deletion benches/bench1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -915,7 +915,7 @@ const MEAN_SUM_N: usize = 127;
fn range_mat(m: Ix, n: Ix) -> Array2<f32> {
assert!(m * n != 0);
Array::linspace(0., (m * n - 1) as f32, m * n)
.into_shape((m, n))
.into_shape_with_order((m, n))
.unwrap()
}

Expand Down
4 changes: 2 additions & 2 deletions benches/construct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ fn zeros_f64(bench: &mut Bencher) {

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

#[bench]
fn map_stride(bench: &mut test::Bencher) {
let a = Array::linspace(0., 127., 256).into_shape((8, 32)).unwrap();
let a = Array::linspace(0., 127., 256).into_shape_with_order((8, 32)).unwrap();
let av = a.slice(s![.., ..;2]);
bench.iter(|| av.map(|&x| 2. * x));
}
14 changes: 7 additions & 7 deletions benches/higher-order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const Y: usize = 16;

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

Expand All @@ -28,7 +28,7 @@ pub fn double_array(mut a: ArrayViewMut2<'_, f64>) {
#[bench]
fn map_stride_double_f64(bench: &mut Bencher) {
let mut a = Array::linspace(0., 127., N * 2)
.into_shape([X, Y * 2])
.into_shape_with_order([X, Y * 2])
.unwrap();
let mut av = a.slice_mut(s![.., ..;2]);
bench.iter(|| {
Expand All @@ -39,7 +39,7 @@ fn map_stride_double_f64(bench: &mut Bencher) {
#[bench]
fn map_stride_f64(bench: &mut Bencher) {
let a = Array::linspace(0., 127., N * 2)
.into_shape([X, Y * 2])
.into_shape_with_order([X, Y * 2])
.unwrap();
let av = a.slice(s![.., ..;2]);
bench.iter(|| av.map(|&x| 2. * x));
Expand All @@ -48,7 +48,7 @@ fn map_stride_f64(bench: &mut Bencher) {
#[bench]
fn map_stride_u32(bench: &mut Bencher) {
let a = Array::linspace(0., 127., N * 2)
.into_shape([X, Y * 2])
.into_shape_with_order([X, Y * 2])
.unwrap();
let b = a.mapv(|x| x as u32);
let av = b.slice(s![.., ..;2]);
Expand All @@ -58,7 +58,7 @@ fn map_stride_u32(bench: &mut Bencher) {
#[bench]
fn fold_axis(bench: &mut Bencher) {
let a = Array::linspace(0., 127., N * 2)
.into_shape([X, Y * 2])
.into_shape_with_order([X, Y * 2])
.unwrap();
bench.iter(|| a.fold_axis(Axis(0), 0., |&acc, &elt| acc + elt));
}
Expand All @@ -69,15 +69,15 @@ const MASZ: usize = MA * MA;
#[bench]
fn map_axis_0(bench: &mut Bencher) {
let a = Array::from_iter(0..MASZ as i32)
.into_shape([MA, MA])
.into_shape_with_order([MA, MA])
.unwrap();
bench.iter(|| a.map_axis(Axis(0), black_box));
}

#[bench]
fn map_axis_1(bench: &mut Bencher) {
let a = Array::from_iter(0..MASZ as i32)
.into_shape([MA, MA])
.into_shape_with_order([MA, MA])
.unwrap();
bench.iter(|| a.map_axis(Axis(1), black_box));
}
10 changes: 5 additions & 5 deletions benches/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,29 +46,29 @@ fn iter_sum_2d_transpose(bench: &mut Bencher) {

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

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

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

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

bench.iter(|| {
for (i, &_elt) in a.indexed_iter() {
Expand Down
2 changes: 1 addition & 1 deletion benches/numeric.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const Y: usize = 16;
#[bench]
fn clip(bench: &mut Bencher) {
let mut a = Array::linspace(0., 127., N * 2)
.into_shape([X, Y * 2])
.into_shape_with_order([X, Y * 2])
.unwrap();
let min = 2.;
let max = 5.;
Expand Down
2 changes: 1 addition & 1 deletion examples/axis_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ fn main() {
}
regularize(&mut b).unwrap();

let mut b = b.into_shape(a.len()).unwrap();
let mut b = b.into_shape_with_order(a.len()).unwrap();
regularize(&mut b).unwrap();

b.invert_axis(Axis(0));
Expand Down
2 changes: 1 addition & 1 deletion examples/life.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ fn parse(x: &[u8]) -> Board {
_ => None,
}));

let a = a.into_shape((N, N)).unwrap();
let a = a.into_shape_with_order((N, N)).unwrap();
map.slice_mut(s![1..-1, 1..-1]).assign(&a);
map
}
Expand Down
2 changes: 1 addition & 1 deletion examples/sort-axis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ where

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

let perm = a.sort_axis_by(Axis(1), |i, j| a[[i, 0]] > a[[j, 0]]);
Expand Down
4 changes: 2 additions & 2 deletions src/free_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ pub fn aview0<A>(x: &A) -> ArrayView0<'_, A> {
/// let data = [1.0; 1024];
///
/// // Create a 2D array view from borrowed data
/// let a2d = aview1(&data).into_shape((32, 32)).unwrap();
/// let a2d = aview1(&data).into_shape_with_order((32, 32)).unwrap();
///
/// assert_eq!(a2d.sum(), 1024.0);
/// ```
Expand All @@ -99,7 +99,7 @@ pub fn aview2<A, const N: usize>(xs: &[[A; N]]) -> ArrayView2<'_, A> {
/// // Create an array view over some data, then slice it and modify it.
/// let mut data = [0; 1024];
/// {
/// let mut a = aview_mut1(&mut data).into_shape((32, 32)).unwrap();
/// let mut a = aview_mut1(&mut data).into_shape_with_order((32, 32)).unwrap();
/// a.slice_mut(s![.., ..;3]).fill(5);
/// }
/// assert_eq!(&data[..10], [5, 0, 0, 5, 0, 0, 5, 0, 0, 5]);
Expand Down
12 changes: 6 additions & 6 deletions src/impl_constructors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ where
A: Clone,
Sh: ShapeBuilder<Dim = D>,
{
let shape = shape.into_shape();
let shape = shape.into_shape_with_order();
let size = size_of_shape_checked_unwrap!(&shape.dim);
let v = vec![elem; size];
unsafe { Self::from_shape_vec_unchecked(shape, v) }
Expand Down Expand Up @@ -382,7 +382,7 @@ where
Sh: ShapeBuilder<Dim = D>,
F: FnMut() -> A,
{
let shape = shape.into_shape();
let shape = shape.into_shape_with_order();
let len = size_of_shape_checked_unwrap!(&shape.dim);
let v = to_vec_mapped(0..len, move |_| f());
unsafe { Self::from_shape_vec_unchecked(shape, v) }
Expand Down Expand Up @@ -413,7 +413,7 @@ where
Sh: ShapeBuilder<Dim = D>,
F: FnMut(D::Pattern) -> A,
{
let shape = shape.into_shape();
let shape = shape.into_shape_with_order();
let _ = size_of_shape_checked_unwrap!(&shape.dim);
if shape.is_c() {
let v = to_vec_mapped(indices(shape.dim.clone()).into_iter(), f);
Expand Down Expand Up @@ -590,7 +590,7 @@ where
Sh: ShapeBuilder<Dim = D>,
{
unsafe {
let shape = shape.into_shape();
let shape = shape.into_shape_with_order();
let size = size_of_shape_checked_unwrap!(&shape.dim);
let mut v = Vec::with_capacity(size);
v.set_len(size);
Expand Down Expand Up @@ -663,7 +663,7 @@ where
A: Copy,
Sh: ShapeBuilder<Dim = D>,
{
let shape = shape.into_shape();
let shape = shape.into_shape_with_order();
let size = size_of_shape_checked_unwrap!(&shape.dim);
let mut v = Vec::with_capacity(size);
v.set_len(size);
Expand All @@ -686,7 +686,7 @@ where
Sh: ShapeBuilder<Dim = D>,
{
unsafe {
let shape = shape.into_shape();
let shape = shape.into_shape_with_order();
let size = size_of_shape_checked_unwrap!(&shape.dim);
let mut v = Vec::with_capacity(size);
v.set_len(size);
Expand Down
10 changes: 5 additions & 5 deletions src/impl_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1322,7 +1322,7 @@ where
/// use ndarray::Array;
/// use ndarray::{arr3, Axis};
///
/// let a = Array::from_iter(0..28).into_shape((2, 7, 2)).unwrap();
/// let a = Array::from_iter(0..28).into_shape_with_order((2, 7, 2)).unwrap();
/// let mut iter = a.axis_chunks_iter(Axis(1), 2);
///
/// // first iteration yields a 2 × 2 × 2 view
Expand Down Expand Up @@ -1955,7 +1955,7 @@ where
/// elements is accepted, but the source array or view must be in standard
/// or column-major (Fortran) layout.
///
/// **Note** that `.into_shape()` "moves" elements differently depending on if the input array
/// **Note** that `.into_shape_with_order()` "moves" elements differently depending on if the input array
/// is C-contig or F-contig, it follows the index order that corresponds to the memory order.
/// Prefer to use `.to_shape()` or `.into_shape_with_order()`.
///
Expand All @@ -1969,7 +1969,7 @@ where
/// use ndarray::{aview1, aview2};
///
/// assert!(
/// aview1(&[1., 2., 3., 4.]).into_shape((2, 2)).unwrap()
/// aview1(&[1., 2., 3., 4.]).into_shape_with_order((2, 2)).unwrap()
/// == aview2(&[[1., 2.],
/// [3., 4.]])
/// );
Expand Down Expand Up @@ -2049,7 +2049,7 @@ where
}
}

/// *Note: Reshape is for `ArcArray` only. Use `.into_shape()` for
/// *Note: Reshape is for `ArcArray` only. Use `.into_shape_with_order()` for
/// other arrays and array views.*
///
/// Transform the array into `shape`; any shape with the same number of
Expand All @@ -2072,7 +2072,7 @@ where
/// [3., 4.]])
/// );
/// ```
#[deprecated(note="Obsolete, use `to_shape` or `into_shape` instead.", since="0.15.2")]
#[deprecated(note="Obsolete, use `to_shape` or `into_shape_with_order` instead.", since="0.15.2")]
pub fn reshape<E>(&self, shape: E) -> ArrayBase<S, E::Dim>
where
S: DataShared + DataOwned,
Expand Down
6 changes: 3 additions & 3 deletions src/impl_owned_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ impl<A, D> Array<A, D>
/// ```
/// use ndarray::Array;
///
/// let a = Array::from_iter(0..100).into_shape((10, 10)).unwrap();
/// let a = Array::from_iter(0..100).into_shape_with_order((10, 10)).unwrap();
/// let mut b = Array::uninit((10, 10));
/// a.move_into_uninit(&mut b);
/// unsafe {
Expand Down Expand Up @@ -447,8 +447,8 @@ impl<A, D> Array<A, D>
///
/// // create an empty array and append two rows at a time
/// let mut a = Array::zeros((0, 4));
/// let ones = ArrayView::from(&[1.; 8]).into_shape((2, 4)).unwrap();
/// let zeros = ArrayView::from(&[0.; 8]).into_shape((2, 4)).unwrap();
/// let ones = ArrayView::from(&[1.; 8]).into_shape_with_order((2, 4)).unwrap();
/// let zeros = ArrayView::from(&[0.; 8]).into_shape_with_order((2, 4)).unwrap();
/// a.append(Axis(0), ones).unwrap();
/// a.append(Axis(0), zeros).unwrap();
/// a.append(Axis(0), ones).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion src/impl_views/indexing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use crate::NdIndex;
/// let data = [0.; 256];
/// let long_life_ref = {
/// // make a 16 × 16 array view
/// let view = ArrayView::from(&data[..]).into_shape((16, 16)).unwrap();
/// let view = ArrayView::from(&data[..]).into_shape_with_order((16, 16)).unwrap();
///
/// // index the view and with `IndexLonger`.
/// // Note here that we get a reference with a life that is derived from
Expand Down
4 changes: 2 additions & 2 deletions src/parallel/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
//! use ndarray::Axis;
//! use ndarray::parallel::prelude::*;
//!
//! let a = Array::linspace(0., 63., 64).into_shape((4, 16)).unwrap();
//! let a = Array::linspace(0., 63., 64).into_shape_with_order((4, 16)).unwrap();
//! let mut sums = Vec::new();
//! a.axis_iter(Axis(0))
//! .into_par_iter()
Expand All @@ -84,7 +84,7 @@
//! use ndarray::Axis;
//! use ndarray::parallel::prelude::*;
//!
//! let a = Array::linspace(0., 63., 64).into_shape((4, 16)).unwrap();
//! let a = Array::linspace(0., 63., 64).into_shape_with_order((4, 16)).unwrap();
//! let mut shapes = Vec::new();
//! a.axis_chunks_iter(Axis(0), 3)
//! .into_par_iter()
Expand Down
Loading

0 comments on commit cd69772

Please sign in to comment.