@@ -1849,23 +1849,50 @@ def ravel(a, order="C"):
1849
1849
x : {dpnp.ndarray, usm_ndarray}
1850
1850
Input array. The elements in `a` are read in the order specified by
1851
1851
order, and packed as a 1-D array.
1852
- order : {"C", "F"}, optional
1852
+ order : {None, "C", "F", "A "}, optional
1853
1853
The elements of `a` are read using this index order. ``"C"`` means to
1854
1854
index the elements in row-major, C-style order, with the last axis
1855
1855
index changing fastest, back to the first axis index changing slowest.
1856
1856
``"F"`` means to index the elements in column-major, Fortran-style
1857
1857
order, with the first index changing fastest, and the last index
1858
- changing slowest. By default, ``"C"`` index order is used.
1858
+ changing slowest. Note that the "C" and "F" options take no account of
1859
+ the memory layout of the underlying array, and only refer to
1860
+ the order of axis indexing. "A" means to read the elements in
1861
+ Fortran-like index order if `a` is Fortran *contiguous* in
1862
+ memory, C-like order otherwise. ``order=None`` is an alias for
1863
+ ``order="C"``.
1864
+ Default: ``"C"``.
1859
1865
1860
1866
Returns
1861
1867
-------
1862
1868
out : dpnp.ndarray
1863
1869
A contiguous 1-D array of the same subtype as `a`, with shape (a.size,).
1864
1870
1871
+ Limitations
1872
+ -----------
1873
+ `order="K"` is not supported and the function raises `NotImplementedError`
1874
+ exception.
1875
+
1865
1876
See Also
1866
1877
--------
1867
- :obj:`dpnp.reshape` : Change the shape of an array without changing its
1868
- data.
1878
+ :obj:`dpnp.ndarray.flat` : 1-D iterator over an array.
1879
+ :obj:`dpnp.ndarray.flatten` : 1-D array copy of the elements of an array
1880
+ in row-major order.
1881
+ :obj:`dpnp.ndarray.reshape` : Change the shape of an array without
1882
+ changing its data.
1883
+ :obj:`dpnp.reshape` : The same as :obj:`dpnp.ndarray.reshape`.
1884
+
1885
+ Notes
1886
+ -----
1887
+ In row-major, C-style order, in two dimensions, the row index
1888
+ varies the slowest, and the column index the quickest. This can
1889
+ be generalized to multiple dimensions, where row-major order
1890
+ implies that the index along the first axis varies slowest, and
1891
+ the index along the last quickest. The opposite holds for
1892
+ column-major, Fortran-style index ordering.
1893
+
1894
+ When a view is desired in as many cases as possible, ``arr.reshape(-1)``
1895
+ may be preferable.
1869
1896
1870
1897
Examples
1871
1898
--------
@@ -1880,9 +1907,26 @@ def ravel(a, order="C"):
1880
1907
>>> np.ravel(x, order='F')
1881
1908
array([1, 4, 2, 5, 3, 6])
1882
1909
1910
+ When ``order`` is 'A', it will preserve the array's 'C' or 'F' ordering:
1911
+
1912
+ >>> np.ravel(x.T)
1913
+ array([1, 4, 2, 5, 3, 6])
1914
+ >>> np.ravel(x.T, order='A')
1915
+ array([1, 2, 3, 4, 5, 6])
1916
+
1883
1917
"""
1884
1918
1885
- return dpnp .reshape (a , - 1 , order = order )
1919
+ if order in "kK" :
1920
+ raise NotImplementedError (
1921
+ "Keyword argument `order` is supported only with "
1922
+ f"values None, 'C', 'F', and 'A', but got '{ order } '"
1923
+ )
1924
+
1925
+ result = dpnp .reshape (a , - 1 , order = order )
1926
+ if result .flags .c_contiguous :
1927
+ return result
1928
+
1929
+ return dpnp .ascontiguousarray (result )
1886
1930
1887
1931
1888
1932
def repeat (a , repeats , axis = None ):
@@ -2071,8 +2115,6 @@ def reshape(a, /, shape=None, order="C", *, newshape=None, copy=None):
2071
2115
an integer, then the result will be a 1-D array of that length.
2072
2116
One shape dimension can be -1. In this case, the value is
2073
2117
inferred from the length of the array and remaining dimensions.
2074
- newshape : int or tuple of ints
2075
- Replaced by `shape` argument. Retained for backward compatibility.
2076
2118
order : {None, "C", "F", "A"}, optional
2077
2119
Read the elements of `a` using this index order, and place the
2078
2120
elements into the reshaped array using this index order. ``"C"``
@@ -2087,6 +2129,8 @@ def reshape(a, /, shape=None, order="C", *, newshape=None, copy=None):
2087
2129
read / write the elements in Fortran-like index order if ``a`` is
2088
2130
Fortran *contiguous* in memory, C-like order otherwise.
2089
2131
Default: ``"C"``.
2132
+ newshape : int or tuple of ints
2133
+ Replaced by `shape` argument. Retained for backward compatibility.
2090
2134
copy : {None, bool}, optional
2091
2135
If ``True``, then the array data is copied. If ``None``, a copy will
2092
2136
only be made if it's required by ``order``. For ``False`` it raises
@@ -2109,7 +2153,7 @@ def reshape(a, /, shape=None, order="C", *, newshape=None, copy=None):
2109
2153
It is not always possible to change the shape of an array without copying
2110
2154
the data.
2111
2155
2112
- The `` order` ` keyword gives the index ordering both for *fetching*
2156
+ The `order` keyword gives the index ordering both for *fetching*
2113
2157
the values from ``a``, and then *placing* the values into the output
2114
2158
array. For example, let's say you have an array:
2115
2159
0 commit comments