Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 21 additions & 7 deletions dpnp/dpnp_iface_manipulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2055,7 +2055,7 @@ def require(a, dtype=None, requirements=None, *, like=None):
return arr


def reshape(a, /, newshape, order="C", copy=None):
def reshape(a, /, shape=None, *, newshape=None, order="C", copy=None):
"""
Gives a new shape to an array without changing its data.

Expand All @@ -2065,12 +2065,14 @@ def reshape(a, /, newshape, order="C", copy=None):
----------
a : {dpnp.ndarray, usm_ndarray}
Array to be reshaped.
newshape : int or tuple of ints
shape : int or tuple of ints
The new shape should be compatible with the original shape. If
an integer, then the result will be a 1-D array of that length.
One shape dimension can be -1. In this case, the value is
inferred from the length of the array and remaining dimensions.
order : {"C", "F"}, optional
newshape : int or tuple of ints
Replaced by shape argument. Retained for backward compatibility.
order : {None, "C", "F"}, optional
Read the elements of `a` using this index order, and place the
elements into the reshaped array using this index order. ``"C"``
means to read / write the elements using C-like index order,
Expand All @@ -2080,6 +2082,8 @@ def reshape(a, /, newshape, order="C", copy=None):
changing fastest, and the last index changing slowest. Note that
the ``"C"`` and ``"F"`` options take no account of the memory layout of
the underlying array, and only refer to the order of indexing.
``order=None`` is an alias for ``order="C"``.
Default: ``"C"``.
copy : {None, bool}, optional
Boolean indicating whether or not to copy the input array.
If ``True``, the result array will always be a copy of input `a`.
Expand All @@ -2093,7 +2097,7 @@ def reshape(a, /, newshape, order="C", copy=None):
-------
out : dpnp.ndarray
This will be a new view object if possible; otherwise, it will
be a copy. Note there is no guarantee of the *memory layout* (C- or
be a copy. Note there is no guarantee of the *memory layout* (C- or
Fortran- contiguous) of the returned array.

Limitations
Expand All @@ -2120,16 +2124,26 @@ def reshape(a, /, newshape, order="C", copy=None):

"""

if newshape is None:
newshape = a.shape
if newshape is None and shape is None:
raise TypeError(
"reshape() missing 1 required positional argument: 'shape'"
)

if newshape is not None:
if shape is not None:
raise TypeError(
"You cannot specify 'newshape' and 'shape' arguments "
"at the same time."
)
shape = newshape

if order is None:
order = "C"
elif order not in "cfCF":
raise ValueError(f"order must be one of 'C' or 'F' (got {order})")

usm_a = dpnp.get_usm_ndarray(a)
usm_res = dpt.reshape(usm_a, shape=newshape, order=order, copy=copy)
usm_res = dpt.reshape(usm_a, shape=shape, order=order, copy=copy)
return dpnp_array._create_from_usm_ndarray(usm_res)


Expand Down
26 changes: 26 additions & 0 deletions tests/test_manipulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -790,6 +790,32 @@ def test_negative_resize(self, xp):
xp.resize(a, new_shape=new_shape)


class TestReshape:
def test_error(self):
ia = dpnp.arange(10)
assert_raises(TypeError, dpnp.reshape, ia)
assert_raises(
TypeError, dpnp.reshape, ia, shape=(2, 5), newshape=(2, 5)
)

def test_newshape(self):
a = numpy.arange(10)
ia = dpnp.array(a)
expected = numpy.reshape(a, (2, 5))
result = dpnp.reshape(ia, newshape=(2, 5))
assert_array_equal(result, expected)

@pytest.mark.parametrize("order", [None, "C", "F"])
def test_order(self, order):
a = numpy.arange(10)
ia = dpnp.array(a)
expected = numpy.reshape(a, (2, 5), order=order)
result = dpnp.reshape(ia, newshape=(2, 5), order=order)
assert result.flags["C_CONTIGUOUS"] == expected.flags["C_CONTIGUOUS"]
assert result.flags["F_CONTIGUOUS"] == expected.flags["F_CONTIGUOUS"]
assert_array_equal(result, expected)


class TestRot90:
@pytest.mark.parametrize("xp", [numpy, dpnp])
def test_error(self, xp):
Expand Down
Loading