@@ -1885,38 +1885,71 @@ where
1885
1885
}
1886
1886
1887
1887
/// Transform the array into `shape`; any shape with the same number of
1888
- /// elements is accepted, but the source array or view must be in standard
1889
- /// or column-major (Fortran) layout.
1888
+ /// elements is accepted, but the source array must be in contiguous row-major (C) or
1889
+ /// column-major (F) layout.
1890
+ ///
1891
+ /// **Note** that `.into_shape()` "moves" elements differently depending on if the input array
1892
+ /// is C-contig or F-contig, it follows the index order that corresponds to the memory
1893
+ /// order. If this is not wanted, use `.to_shape()`.
1894
+ ///
1895
+ /// If a memory ordering is specified (optional) in the shape argument, the operation
1896
+ /// will only succeed if the input has this memory order.
1890
1897
///
1891
1898
/// **Errors** if the shapes don't have the same number of elements.<br>
1892
- /// **Errors** if the input array is not c- or f-contiguous.
1899
+ /// **Errors** if the input array is not c- or f-contiguous.<br>
1900
+ /// **Errors** if a memory ordering is requested that is not compatible with the array.<br>
1901
+ ///
1902
+ /// If shape is not given: use memory layout of incoming array. Row major arrays are
1903
+ /// reshaped using row major index ordering, column major arrays with column major index
1904
+ /// ordering.
1893
1905
///
1894
1906
/// ```
1895
1907
/// use ndarray::{aview1, aview2};
1908
+ /// use ndarray::Order;
1896
1909
///
1897
1910
/// assert!(
1898
1911
/// aview1(&[1., 2., 3., 4.]).into_shape((2, 2)).unwrap()
1899
1912
/// == aview2(&[[1., 2.],
1900
1913
/// [3., 4.]])
1901
1914
/// );
1915
+ ///
1916
+ /// assert!(
1917
+ /// aview1(&[1., 2., 3., 4.]).into_shape(((2, 2), Order::ColumnMajor)).unwrap()
1918
+ /// == aview2(&[[1., 3.],
1919
+ /// [2., 4.]])
1920
+ /// );
1902
1921
/// ```
1903
1922
pub fn into_shape < E > ( self , shape : E ) -> Result < ArrayBase < S , E :: Dim > , ShapeError >
1904
1923
where
1905
- E : IntoDimension ,
1924
+ E : ShapeArg ,
1925
+ {
1926
+ let ( shape, order) = shape. into_shape_and_order ( ) ;
1927
+ self . into_shape_order ( shape, order)
1928
+ }
1929
+
1930
+ fn into_shape_order < E > ( self , shape : E , order : Option < Order > ) -> Result < ArrayBase < S , E > , ShapeError >
1931
+ where
1932
+ E : Dimension ,
1906
1933
{
1907
1934
let shape = shape. into_dimension ( ) ;
1908
1935
if size_of_shape_checked ( & shape) != Ok ( self . dim . size ( ) ) {
1909
1936
return Err ( error:: incompatible_shapes ( & self . dim , & shape) ) ;
1910
1937
}
1911
- // Check if contiguous, if not => copy all, else just adapt strides
1938
+
1939
+ // Check if contiguous, then we can change shape
1940
+ let require_order = order. is_some ( ) ;
1912
1941
unsafe {
1913
1942
// safe because arrays are contiguous and len is unchanged
1914
- if self . is_standard_layout ( ) {
1915
- Ok ( self . with_strides_dim ( shape. default_strides ( ) , shape) )
1916
- } else if self . ndim ( ) > 1 && self . raw_view ( ) . reversed_axes ( ) . is_standard_layout ( ) {
1917
- Ok ( self . with_strides_dim ( shape. fortran_strides ( ) , shape) )
1918
- } else {
1919
- Err ( error:: from_kind ( error:: ErrorKind :: IncompatibleLayout ) )
1943
+ match order {
1944
+ None | Some ( Order :: RowMajor ) if self . is_standard_layout ( ) => {
1945
+ Ok ( self . with_strides_dim ( shape. default_strides ( ) , shape) )
1946
+ }
1947
+ None | Some ( Order :: ColumnMajor ) if ( require_order || self . ndim ( ) > 1 ) &&
1948
+ self . raw_view ( ) . reversed_axes ( ) . is_standard_layout ( ) =>
1949
+ {
1950
+ Ok ( self . with_strides_dim ( shape. fortran_strides ( ) , shape) )
1951
+ }
1952
+ _otherwise => Err ( error:: from_kind ( error:: ErrorKind :: IncompatibleLayout ) )
1920
1953
}
1921
1954
}
1922
1955
}
@@ -1932,7 +1965,7 @@ where
1932
1965
self . into_shape_clone_order ( shape, order)
1933
1966
}
1934
1967
1935
- pub fn into_shape_clone_order < E > ( self , shape : E , order : Order )
1968
+ fn into_shape_clone_order < E > ( self , shape : E , order : Order )
1936
1969
-> Result < ArrayBase < S , E > , ShapeError >
1937
1970
where
1938
1971
S : DataOwned ,
@@ -2004,7 +2037,7 @@ where
2004
2037
A : Clone ,
2005
2038
E : IntoDimension ,
2006
2039
{
2007
- return self . clone ( ) . into_shape_clone ( shape) . unwrap ( ) ;
2040
+ // return self.clone().into_shape_clone(shape).unwrap();
2008
2041
let shape = shape. into_dimension ( ) ;
2009
2042
if size_of_shape_checked ( & shape) != Ok ( self . dim . size ( ) ) {
2010
2043
panic ! (
0 commit comments