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
@@ -1848,23 +1849,50 @@ def ravel(a, order="C"):
1848
1849
x : {dpnp.ndarray, usm_ndarray}
1849
1850
Input array. The elements in `a` are read in the order specified by
1850
1851
order, and packed as a 1-D array.
1851
- order : {"C", "F"}, optional
1852
+ order : {None, "C", "F", "A "}, optional
1852
1853
The elements of `a` are read using this index order. ``"C"`` means to
1853
1854
index the elements in row-major, C-style order, with the last axis
1854
1855
index changing fastest, back to the first axis index changing slowest.
1855
1856
``"F"`` means to index the elements in column-major, Fortran-style
1856
1857
order, with the first index changing fastest, and the last index
1857
- 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"``.
1858
1865
1859
1866
Returns
1860
1867
-------
1861
1868
out : dpnp.ndarray
1862
1869
A contiguous 1-D array of the same subtype as `a`, with shape (a.size,).
1863
1870
1871
+ Limitations
1872
+ -----------
1873
+ `order="K"` is not supported and the function raises `NotImplementedError`
1874
+ exception.
1875
+
1864
1876
See Also
1865
1877
--------
1866
- :obj:`dpnp.reshape` : Change the shape of an array without changing its
1867
- 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.
1868
1896
1869
1897
Examples
1870
1898
--------
@@ -1879,9 +1907,27 @@ def ravel(a, order="C"):
1879
1907
>>> np.ravel(x, order='F')
1880
1908
array([1, 4, 2, 5, 3, 6])
1881
1909
1910
+ When `order` is ``"A"``, it will preserve the array's
1911
+ ``"C"`` or ``"F"`` ordering:
1912
+
1913
+ >>> np.ravel(x.T)
1914
+ array([1, 4, 2, 5, 3, 6])
1915
+ >>> np.ravel(x.T, order='A')
1916
+ array([1, 2, 3, 4, 5, 6])
1917
+
1882
1918
"""
1883
1919
1884
- return dpnp .reshape (a , - 1 , order = order )
1920
+ if order in "kK" :
1921
+ raise NotImplementedError (
1922
+ "Keyword argument `order` is supported only with "
1923
+ f"values None, 'C', 'F', and 'A', but got '{ order } '"
1924
+ )
1925
+
1926
+ result = dpnp .reshape (a , - 1 , order = order )
1927
+ if result .flags .c_contiguous :
1928
+ return result
1929
+
1930
+ return dpnp .ascontiguousarray (result )
1885
1931
1886
1932
1887
1933
def repeat (a , repeats , axis = None ):
@@ -2055,7 +2101,7 @@ def require(a, dtype=None, requirements=None, *, like=None):
2055
2101
return arr
2056
2102
2057
2103
2058
- def reshape (a , / , newshape , order = "C" , copy = None ):
2104
+ def reshape (a , / , shape = None , order = "C" , * , newshape = None , copy = None ):
2059
2105
"""
2060
2106
Gives a new shape to an array without changing its data.
2061
2107
@@ -2065,12 +2111,12 @@ def reshape(a, /, newshape, order="C", copy=None):
2065
2111
----------
2066
2112
a : {dpnp.ndarray, usm_ndarray}
2067
2113
Array to be reshaped.
2068
- newshape : int or tuple of ints
2114
+ shape : int or tuple of ints
2069
2115
The new shape should be compatible with the original shape. If
2070
2116
an integer, then the result will be a 1-D array of that length.
2071
2117
One shape dimension can be -1. In this case, the value is
2072
2118
inferred from the length of the array and remaining dimensions.
2073
- order : {"C", "F"}, optional
2119
+ order : {None, "C", "F", "A "}, optional
2074
2120
Read the elements of `a` using this index order, and place the
2075
2121
elements into the reshaped array using this index order. ``"C"``
2076
2122
means to read / write the elements using C-like index order,
@@ -2080,30 +2126,63 @@ def reshape(a, /, newshape, order="C", copy=None):
2080
2126
changing fastest, and the last index changing slowest. Note that
2081
2127
the ``"C"`` and ``"F"`` options take no account of the memory layout of
2082
2128
the underlying array, and only refer to the order of indexing.
2129
+ ``order=None`` is an alias for ``order="C"``. ``"A"`` means to
2130
+ read / write the elements in Fortran-like index order if ``a`` is
2131
+ Fortran *contiguous* in memory, C-like order otherwise.
2132
+ Default: ``"C"``.
2133
+ newshape : int or tuple of ints
2134
+ Replaced by `shape` argument. Retained for backward compatibility.
2083
2135
copy : {None, bool}, optional
2084
- Boolean indicating whether or not to copy the input array.
2085
- If ``True``, the result array will always be a copy of input `a`.
2086
- If ``False``, the result array can never be a copy
2087
- and a ValueError exception will be raised in case the copy is necessary.
2088
- If ``None``, the result array will reuse existing memory buffer of `a`
2089
- if possible and copy otherwise.
2136
+ If ``True``, then the array data is copied. If ``None``, a copy will
2137
+ only be made if it's required by ``order``. For ``False`` it raises
2138
+ a ``ValueError`` if a copy cannot be avoided.
2090
2139
Default: ``None``.
2091
2140
2092
2141
Returns
2093
2142
-------
2094
2143
out : dpnp.ndarray
2095
2144
This will be a new view object if possible; otherwise, it will
2096
- be a copy. Note there is no guarantee of the *memory layout* (C- or
2145
+ be a copy. Note there is no guarantee of the *memory layout* (C- or
2097
2146
Fortran- contiguous) of the returned array.
2098
2147
2099
- Limitations
2100
- -----------
2101
- Parameter `order` is supported only with values ``"C"`` and ``"F"``.
2102
-
2103
2148
See Also
2104
2149
--------
2105
2150
:obj:`dpnp.ndarray.reshape` : Equivalent method.
2106
2151
2152
+ Notes
2153
+ -----
2154
+ It is not always possible to change the shape of an array without copying
2155
+ the data.
2156
+
2157
+ The `order` keyword gives the index ordering both for *fetching*
2158
+ the values from ``a``, and then *placing* the values into the output
2159
+ array. For example, let's say you have an array:
2160
+
2161
+ >>> import dpnp as np
2162
+ >>> a = np.arange(6).reshape((3, 2))
2163
+ >>> a
2164
+ array([[0, 1],
2165
+ [2, 3],
2166
+ [4, 5]])
2167
+
2168
+ You can think of reshaping as first raveling the array (using the given
2169
+ index order), then inserting the elements from the raveled array into the
2170
+ new array using the same kind of index ordering as was used for the
2171
+ raveling.
2172
+
2173
+ >>> np.reshape(a, (2, 3)) # C-like index ordering
2174
+ array([[0, 1, 2],
2175
+ [3, 4, 5]])
2176
+ >>> np.reshape(np.ravel(a), (2, 3)) # equivalent to C ravel then C reshape
2177
+ array([[0, 1, 2],
2178
+ [3, 4, 5]])
2179
+ >>> np.reshape(a, (2, 3), order='F') # Fortran-like index ordering
2180
+ array([[0, 4, 3],
2181
+ [2, 1, 5]])
2182
+ >>> np.reshape(np.ravel(a, order='F'), (2, 3), order='F')
2183
+ array([[0, 4, 3],
2184
+ [2, 1, 5]])
2185
+
2107
2186
Examples
2108
2187
--------
2109
2188
>>> import dpnp as np
@@ -2120,16 +2199,38 @@ def reshape(a, /, newshape, order="C", copy=None):
2120
2199
2121
2200
"""
2122
2201
2123
- if newshape is None :
2124
- newshape = a .shape
2202
+ if newshape is None and shape is None :
2203
+ raise TypeError (
2204
+ "reshape() missing 1 required positional argument: 'shape'"
2205
+ )
2206
+
2207
+ if newshape is not None :
2208
+ if shape is not None :
2209
+ raise TypeError (
2210
+ "You cannot specify 'newshape' and 'shape' arguments "
2211
+ "at the same time."
2212
+ )
2213
+ # Deprecated in dpnp 0.17.0
2214
+ warnings .warn (
2215
+ "`newshape` keyword argument is deprecated, "
2216
+ "use `shape=...` or pass shape positionally instead. "
2217
+ "(deprecated in dpnp 0.17.0)" ,
2218
+ DeprecationWarning ,
2219
+ stacklevel = 2 ,
2220
+ )
2221
+ shape = newshape
2125
2222
2126
2223
if order is None :
2127
2224
order = "C"
2225
+ elif order in "aA" :
2226
+ order = "F" if a .flags .fnc else "C"
2128
2227
elif order not in "cfCF" :
2129
- raise ValueError (f"order must be one of 'C' or 'F' (got { order } )" )
2228
+ raise ValueError (
2229
+ f"order must be None, 'C', 'F', or 'A' (got '{ order } ')"
2230
+ )
2130
2231
2131
2232
usm_a = dpnp .get_usm_ndarray (a )
2132
- usm_res = dpt .reshape (usm_a , shape = newshape , order = order , copy = copy )
2233
+ usm_res = dpt .reshape (usm_a , shape = shape , order = order , copy = copy )
2133
2234
return dpnp_array ._create_from_usm_ndarray (usm_res )
2134
2235
2135
2236
0 commit comments