@@ -1884,10 +1884,84 @@ where
1884
1884
}
1885
1885
}
1886
1886
1887
+ /// Transform the array into `shape`; any shape with the same number of
1888
+ /// elements is accepted, but the source array must be contiguous.
1889
+ ///
1890
+ /// If an index ordering is not specified, the default is `RowMajor`.
1891
+ /// The operation will only succeed if the array's memory layout is compatible with
1892
+ /// the index ordering.
1893
+ ///
1894
+ /// Use `.to_shape()` instead for more flexible reshaping of arrays, which
1895
+ /// allows copying elements if required.
1896
+ ///
1897
+ /// **Errors** if the shapes don't have the same number of elements.<br>
1898
+ /// **Errors** if order RowMajor is given but input is not c-contiguous.
1899
+ /// **Errors** if order ColumnMajor is given but input is not f-contiguous.
1900
+ ///
1901
+ /// If shape is not given: use memory layout of incoming array. Row major arrays are
1902
+ /// reshaped using row major index ordering, column major arrays with column major index
1903
+ /// ordering.
1904
+ ///
1905
+ /// ```
1906
+ /// use ndarray::{aview1, aview2};
1907
+ /// use ndarray::Order;
1908
+ ///
1909
+ /// assert!(
1910
+ /// aview1(&[1., 2., 3., 4.]).into_shape_with_order((2, 2)).unwrap()
1911
+ /// == aview2(&[[1., 2.],
1912
+ /// [3., 4.]])
1913
+ /// );
1914
+ ///
1915
+ /// assert!(
1916
+ /// aview1(&[1., 2., 3., 4.]).into_shape_with_order(((2, 2), Order::ColumnMajor)).unwrap()
1917
+ /// == aview2(&[[1., 3.],
1918
+ /// [2., 4.]])
1919
+ /// );
1920
+ /// ```
1921
+ pub fn into_shape_with_order < E > ( self , shape : E ) -> Result < ArrayBase < S , E :: Dim > , ShapeError >
1922
+ where
1923
+ E : ShapeArg ,
1924
+ {
1925
+ let ( shape, order) = shape. into_shape_and_order ( ) ;
1926
+ self . into_shape_with_order_impl ( shape, order. unwrap_or ( Order :: RowMajor ) )
1927
+ }
1928
+
1929
+ fn into_shape_with_order_impl < E > ( self , shape : E , order : Order )
1930
+ -> Result < ArrayBase < S , E > , ShapeError >
1931
+ where
1932
+ E : Dimension ,
1933
+ {
1934
+ let shape = shape. into_dimension ( ) ;
1935
+ if size_of_shape_checked ( & shape) != Ok ( self . dim . size ( ) ) {
1936
+ return Err ( error:: incompatible_shapes ( & self . dim , & shape) ) ;
1937
+ }
1938
+
1939
+ // Check if contiguous, then we can change shape
1940
+ unsafe {
1941
+ // safe because arrays are contiguous and len is unchanged
1942
+ match order {
1943
+ Order :: RowMajor if self . is_standard_layout ( ) => {
1944
+ Ok ( self . with_strides_dim ( shape. default_strides ( ) , shape) )
1945
+ }
1946
+ Order :: ColumnMajor if self . raw_view ( ) . reversed_axes ( ) . is_standard_layout ( ) => {
1947
+ Ok ( self . with_strides_dim ( shape. fortran_strides ( ) , shape) )
1948
+ }
1949
+ _otherwise => Err ( error:: from_kind ( error:: ErrorKind :: IncompatibleLayout ) )
1950
+ }
1951
+ }
1952
+ }
1953
+
1887
1954
/// Transform the array into `shape`; any shape with the same number of
1888
1955
/// elements is accepted, but the source array or view must be in standard
1889
1956
/// or column-major (Fortran) layout.
1890
1957
///
1958
+ /// **Note** that `.into_shape()` "moves" elements differently depending on if the input array
1959
+ /// is C-contig or F-contig, it follows the index order that corresponds to the memory order.
1960
+ /// Prefer to use `.to_shape()` or `.into_shape_with_order()`.
1961
+ ///
1962
+ /// Because of this, the method is deprecated. That reshapes depend on memory order is not
1963
+ /// intuitive.
1964
+ ///
1891
1965
/// **Errors** if the shapes don't have the same number of elements.<br>
1892
1966
/// **Errors** if the input array is not c- or f-contiguous.
1893
1967
///
@@ -1900,6 +1974,7 @@ where
1900
1974
/// [3., 4.]])
1901
1975
/// );
1902
1976
/// ```
1977
+ #[ deprecated = "Use `.into_shape_with_order()` or `.to_shape()`" ]
1903
1978
pub fn into_shape < E > ( self , shape : E ) -> Result < ArrayBase < S , E :: Dim > , ShapeError >
1904
1979
where
1905
1980
E : IntoDimension ,
@@ -1932,7 +2007,7 @@ where
1932
2007
self . into_shape_clone_order ( shape, order)
1933
2008
}
1934
2009
1935
- pub fn into_shape_clone_order < E > ( self , shape : E , order : Order )
2010
+ fn into_shape_clone_order < E > ( self , shape : E , order : Order )
1936
2011
-> Result < ArrayBase < S , E > , ShapeError >
1937
2012
where
1938
2013
S : DataOwned ,
@@ -2004,7 +2079,7 @@ where
2004
2079
A : Clone ,
2005
2080
E : IntoDimension ,
2006
2081
{
2007
- return self . clone ( ) . into_shape_clone ( shape) . unwrap ( ) ;
2082
+ // return self.clone().into_shape_clone(shape).unwrap();
2008
2083
let shape = shape. into_dimension ( ) ;
2009
2084
if size_of_shape_checked ( & shape) != Ok ( self . dim . size ( ) ) {
2010
2085
panic ! (
0 commit comments