Skip to content

Leverage on dpctl implementation of shape.setter #1975

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
62 changes: 51 additions & 11 deletions dpnp/dpnp_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -1284,12 +1284,45 @@ def searchsorted(self, v, side="left", sorter=None):
@property
def shape(self):
"""
Lengths of axes. A tuple of numbers represents size of each dimension.
Tuple of array dimensions.

Setter of this property involves reshaping without copy. If the array
cannot be reshaped without copy, it raises an exception.
The shape property is usually used to get the current shape of an array,
but may also be used to reshape the array in-place by assigning a tuple
of array dimensions to it. Unlike :obj:`dpnp.reshape`, only non-negative
values are supported to be set as new shape. Reshaping an array in-place
will fail if a copy is required.

.. seealso: :attr:`numpy.ndarray.shape`
For full documentation refer to :obj:`numpy.ndarray.shape`.

Note
----
Using :obj:`dpnp.ndarray.reshape` or :obj:`dpnp.reshape` is the
preferred approach to set new shape of an array.

See Also
--------
:obj:`dpnp.shape` : Equivalent getter function.
:obj:`dpnp.reshape` : Function similar to setting `shape`.
:obj:`dpnp.ndarray.reshape` : Method similar to setting `shape`.

Examples
--------
>>> import dpnp as np
>>> x = np.array([1, 2, 3, 4])
>>> x.shape
(4,)
>>> y = np.zeros((2, 3, 4))
>>> y.shape
(2, 3, 4)

>>> y.shape = (3, 8)
>>> y
array([[ 0., 0., 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0., 0., 0.]])
>>> y.shape = (3, 6)
...
TypeError: Can not reshape array of size 24 into (3, 6)

"""

Expand All @@ -1300,16 +1333,23 @@ def shape(self, newshape):
"""
Set new lengths of axes.

A tuple of numbers represents size of each dimension.
It involves reshaping without copy. If the array cannot be reshaped without copy,
it raises an exception.
Modifies array instance in-place by changing its metadata about the
shape and the strides of the array, or raises `AttributeError`
exception if in-place change is not possible.

.. seealso: :attr:`numpy.ndarray.shape`
Whether the array can be reshape in-place depends on its strides. Use
:obj:`dpnp.reshape` function which always succeeds to reshape the array
by performing a copy if necessary.

"""
For full documentation refer to :obj:`numpy.ndarray.shape`.

if not isinstance(newshape, (list, tuple)):
newshape = (newshape,)
Parameters
----------
newshape : {tuple, int}
New shape. Only non-negative values are supported. The new shape
may not lead to the change in the number of elements in the array.

"""

self._array_obj.shape = newshape

Expand Down
2 changes: 0 additions & 2 deletions dpnp/dpnp_iface_nanfunctions.py
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,6 @@ def nanmean(a, axis=None, dtype=None, out=None, keepdims=False, *, where=True):
raise TypeError("If input is inexact, then out must be inexact.")

cnt_dtype = a.real.dtype if dtype is None else dtype
# pylint: disable=invalid-unary-operand-type
cnt = dpnp.sum(
~mask, axis=axis, dtype=cnt_dtype, keepdims=keepdims, where=where
)
Expand Down Expand Up @@ -1062,7 +1061,6 @@ def nanvar(

# Compute mean
var_dtype = a.real.dtype if dtype is None else dtype
# pylint: disable=invalid-unary-operand-type
cnt = dpnp.sum(
~mask, axis=axis, dtype=var_dtype, keepdims=True, where=where
)
Expand Down
Loading