40
40
41
41
import math
42
42
import operator
43
+ import warnings
43
44
44
45
import dpctl .tensor as dpt
45
46
import numpy
@@ -1898,23 +1899,50 @@ def ravel(a, order="C"):
1898
1899
x : {dpnp.ndarray, usm_ndarray}
1899
1900
Input array. The elements in `a` are read in the order specified by
1900
1901
order, and packed as a 1-D array.
1901
- order : {"C", "F"}, optional
1902
+ order : {None, "C", "F", "A "}, optional
1902
1903
The elements of `a` are read using this index order. ``"C"`` means to
1903
1904
index the elements in row-major, C-style order, with the last axis
1904
1905
index changing fastest, back to the first axis index changing slowest.
1905
1906
``"F"`` means to index the elements in column-major, Fortran-style
1906
1907
order, with the first index changing fastest, and the last index
1907
- changing slowest. By default, ``"C"`` index order is used.
1908
+ changing slowest. Note that the "C" and "F" options take no account of
1909
+ the memory layout of the underlying array, and only refer to
1910
+ the order of axis indexing. "A" means to read the elements in
1911
+ Fortran-like index order if `a` is Fortran *contiguous* in
1912
+ memory, C-like order otherwise. ``order=None`` is an alias for
1913
+ ``order="C"``.
1914
+ Default: ``"C"``.
1908
1915
1909
1916
Returns
1910
1917
-------
1911
1918
out : dpnp.ndarray
1912
1919
A contiguous 1-D array of the same subtype as `a`, with shape (a.size,).
1913
1920
1921
+ Limitations
1922
+ -----------
1923
+ `order="K"` is not supported and the function raises `NotImplementedError`
1924
+ exception.
1925
+
1914
1926
See Also
1915
1927
--------
1916
- :obj:`dpnp.reshape` : Change the shape of an array without changing its
1917
- data.
1928
+ :obj:`dpnp.ndarray.flat` : 1-D iterator over an array.
1929
+ :obj:`dpnp.ndarray.flatten` : 1-D array copy of the elements of an array
1930
+ in row-major order.
1931
+ :obj:`dpnp.ndarray.reshape` : Change the shape of an array without
1932
+ changing its data.
1933
+ :obj:`dpnp.reshape` : The same as :obj:`dpnp.ndarray.reshape`.
1934
+
1935
+ Notes
1936
+ -----
1937
+ In row-major, C-style order, in two dimensions, the row index
1938
+ varies the slowest, and the column index the quickest. This can
1939
+ be generalized to multiple dimensions, where row-major order
1940
+ implies that the index along the first axis varies slowest, and
1941
+ the index along the last quickest. The opposite holds for
1942
+ column-major, Fortran-style index ordering.
1943
+
1944
+ When a view is desired in as many cases as possible, ``arr.reshape(-1)``
1945
+ may be preferable.
1918
1946
1919
1947
Examples
1920
1948
--------
@@ -1929,9 +1957,27 @@ def ravel(a, order="C"):
1929
1957
>>> np.ravel(x, order='F')
1930
1958
array([1, 4, 2, 5, 3, 6])
1931
1959
1960
+ When `order` is ``"A"``, it will preserve the array's
1961
+ ``"C"`` or ``"F"`` ordering:
1962
+
1963
+ >>> np.ravel(x.T)
1964
+ array([1, 4, 2, 5, 3, 6])
1965
+ >>> np.ravel(x.T, order='A')
1966
+ array([1, 2, 3, 4, 5, 6])
1967
+
1932
1968
"""
1933
1969
1934
- return dpnp .reshape (a , - 1 , order = order )
1970
+ if order in "kK" :
1971
+ raise NotImplementedError (
1972
+ "Keyword argument `order` is supported only with "
1973
+ f"values None, 'C', 'F', and 'A', but got '{ order } '"
1974
+ )
1975
+
1976
+ result = dpnp .reshape (a , - 1 , order = order )
1977
+ if result .flags .c_contiguous :
1978
+ return result
1979
+
1980
+ return dpnp .ascontiguousarray (result )
1935
1981
1936
1982
1937
1983
def repeat (a , repeats , axis = None ):
@@ -2105,7 +2151,7 @@ def require(a, dtype=None, requirements=None, *, like=None):
2105
2151
return arr
2106
2152
2107
2153
2108
- def reshape (a , / , newshape , order = "C" , copy = None ):
2154
+ def reshape (a , / , shape = None , order = "C" , * , newshape = None , copy = None ):
2109
2155
"""
2110
2156
Gives a new shape to an array without changing its data.
2111
2157
@@ -2115,12 +2161,12 @@ def reshape(a, /, newshape, order="C", copy=None):
2115
2161
----------
2116
2162
a : {dpnp.ndarray, usm_ndarray}
2117
2163
Array to be reshaped.
2118
- newshape : int or tuple of ints
2164
+ shape : int or tuple of ints
2119
2165
The new shape should be compatible with the original shape. If
2120
2166
an integer, then the result will be a 1-D array of that length.
2121
2167
One shape dimension can be -1. In this case, the value is
2122
2168
inferred from the length of the array and remaining dimensions.
2123
- order : {"C", "F"}, optional
2169
+ order : {None, "C", "F", "A "}, optional
2124
2170
Read the elements of `a` using this index order, and place the
2125
2171
elements into the reshaped array using this index order. ``"C"``
2126
2172
means to read / write the elements using C-like index order,
@@ -2130,30 +2176,63 @@ def reshape(a, /, newshape, order="C", copy=None):
2130
2176
changing fastest, and the last index changing slowest. Note that
2131
2177
the ``"C"`` and ``"F"`` options take no account of the memory layout of
2132
2178
the underlying array, and only refer to the order of indexing.
2179
+ ``order=None`` is an alias for ``order="C"``. ``"A"`` means to
2180
+ read / write the elements in Fortran-like index order if ``a`` is
2181
+ Fortran *contiguous* in memory, C-like order otherwise.
2182
+ Default: ``"C"``.
2183
+ newshape : int or tuple of ints
2184
+ Replaced by `shape` argument. Retained for backward compatibility.
2133
2185
copy : {None, bool}, optional
2134
- Boolean indicating whether or not to copy the input array.
2135
- If ``True``, the result array will always be a copy of input `a`.
2136
- If ``False``, the result array can never be a copy
2137
- and a ValueError exception will be raised in case the copy is necessary.
2138
- If ``None``, the result array will reuse existing memory buffer of `a`
2139
- if possible and copy otherwise.
2186
+ If ``True``, then the array data is copied. If ``None``, a copy will
2187
+ only be made if it's required by ``order``. For ``False`` it raises
2188
+ a ``ValueError`` if a copy cannot be avoided.
2140
2189
Default: ``None``.
2141
2190
2142
2191
Returns
2143
2192
-------
2144
2193
out : dpnp.ndarray
2145
2194
This will be a new view object if possible; otherwise, it will
2146
- be a copy. Note there is no guarantee of the *memory layout* (C- or
2195
+ be a copy. Note there is no guarantee of the *memory layout* (C- or
2147
2196
Fortran- contiguous) of the returned array.
2148
2197
2149
- Limitations
2150
- -----------
2151
- Parameter `order` is supported only with values ``"C"`` and ``"F"``.
2152
-
2153
2198
See Also
2154
2199
--------
2155
2200
:obj:`dpnp.ndarray.reshape` : Equivalent method.
2156
2201
2202
+ Notes
2203
+ -----
2204
+ It is not always possible to change the shape of an array without copying
2205
+ the data.
2206
+
2207
+ The `order` keyword gives the index ordering both for *fetching*
2208
+ the values from ``a``, and then *placing* the values into the output
2209
+ array. For example, let's say you have an array:
2210
+
2211
+ >>> import dpnp as np
2212
+ >>> a = np.arange(6).reshape((3, 2))
2213
+ >>> a
2214
+ array([[0, 1],
2215
+ [2, 3],
2216
+ [4, 5]])
2217
+
2218
+ You can think of reshaping as first raveling the array (using the given
2219
+ index order), then inserting the elements from the raveled array into the
2220
+ new array using the same kind of index ordering as was used for the
2221
+ raveling.
2222
+
2223
+ >>> np.reshape(a, (2, 3)) # C-like index ordering
2224
+ array([[0, 1, 2],
2225
+ [3, 4, 5]])
2226
+ >>> np.reshape(np.ravel(a), (2, 3)) # equivalent to C ravel then C reshape
2227
+ array([[0, 1, 2],
2228
+ [3, 4, 5]])
2229
+ >>> np.reshape(a, (2, 3), order='F') # Fortran-like index ordering
2230
+ array([[0, 4, 3],
2231
+ [2, 1, 5]])
2232
+ >>> np.reshape(np.ravel(a, order='F'), (2, 3), order='F')
2233
+ array([[0, 4, 3],
2234
+ [2, 1, 5]])
2235
+
2157
2236
Examples
2158
2237
--------
2159
2238
>>> import dpnp as np
@@ -2170,16 +2249,38 @@ def reshape(a, /, newshape, order="C", copy=None):
2170
2249
2171
2250
"""
2172
2251
2173
- if newshape is None :
2174
- newshape = a .shape
2252
+ if newshape is None and shape is None :
2253
+ raise TypeError (
2254
+ "reshape() missing 1 required positional argument: 'shape'"
2255
+ )
2256
+
2257
+ if newshape is not None :
2258
+ if shape is not None :
2259
+ raise TypeError (
2260
+ "You cannot specify 'newshape' and 'shape' arguments "
2261
+ "at the same time."
2262
+ )
2263
+ # Deprecated in dpnp 0.17.0
2264
+ warnings .warn (
2265
+ "`newshape` keyword argument is deprecated, "
2266
+ "use `shape=...` or pass shape positionally instead. "
2267
+ "(deprecated in dpnp 0.17.0)" ,
2268
+ DeprecationWarning ,
2269
+ stacklevel = 2 ,
2270
+ )
2271
+ shape = newshape
2175
2272
2176
2273
if order is None :
2177
2274
order = "C"
2275
+ elif order in "aA" :
2276
+ order = "F" if a .flags .fnc else "C"
2178
2277
elif order not in "cfCF" :
2179
- raise ValueError (f"order must be one of 'C' or 'F' (got { order } )" )
2278
+ raise ValueError (
2279
+ f"order must be None, 'C', 'F', or 'A' (got '{ order } ')"
2280
+ )
2180
2281
2181
2282
usm_a = dpnp .get_usm_ndarray (a )
2182
- usm_res = dpt .reshape (usm_a , shape = newshape , order = order , copy = copy )
2283
+ usm_res = dpt .reshape (usm_a , shape = shape , order = order , copy = copy )
2183
2284
return dpnp_array ._create_from_usm_ndarray (usm_res )
2184
2285
2185
2286
0 commit comments