Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* Removed the use of class template argument deduction for alias template to conform to the C++17 standard [#2517](https://github.com/IntelPython/dpnp/pull/2517)
* Changed th order of individual FFTs over `axes` for `dpnp.fft.irfftn` to be in forward order [#2524](https://github.com/IntelPython/dpnp/pull/2524)
* Replaced the use of `numpy.testing.suppress_warnings` with appropriate calls from the warnings module [#2529](https://github.com/IntelPython/dpnp/pull/2529)
* Updated `dpnp.size` to accept tuple of ints for `axes` argument [#2536](https://github.com/IntelPython/dpnp/pull/2536)

### Deprecated

Expand Down
24 changes: 13 additions & 11 deletions dpnp/dpnp_iface_manipulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@
import dpctl
import dpctl.tensor as dpt
import numpy
from dpctl.tensor._numpy_helper import AxisError, normalize_axis_index
from dpctl.tensor._numpy_helper import (
AxisError,
normalize_axis_index,
normalize_axis_tuple,
)

import dpnp

Expand Down Expand Up @@ -3528,8 +3532,8 @@ def size(a, axis=None):
----------
a : array_like
Input data.
axis : {None, int}, optional
Axis along which the elements are counted.
axis : {None, int, tuple of ints}, optional
Axis or axes along which the elements are counted.
By default, give the total number of elements.

Default: ``None``.
Expand All @@ -3551,23 +3555,21 @@ def size(a, axis=None):
>>> a = [[1, 2, 3], [4, 5, 6]]
>>> np.size(a)
6
>>> np.size(a, 1)
>>> np.size(a, axis=1)
3
>>> np.size(a, 0)
>>> np.size(a, axis=0)
2

>>> a = np.asarray(a)
>>> np.size(a)
>>> np.size(a, axis=(0, 1))
6
>>> np.size(a, 1)
3

"""

if dpnp.is_supported_array_type(a):
if axis is None:
return a.size
return a.shape[axis]
_shape = a.shape
_axis = normalize_axis_tuple(axis, a.ndim)
return math.prod(_shape[ax] for ax in _axis)

return numpy.size(a, axis)

Expand Down
8 changes: 8 additions & 0 deletions dpnp/tests/test_manipulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ def test_ndim():
assert dpnp.ndim(ia) == exp


# TODO: include commented code in the test when numpy-2.4 is released
# @testing.with_requires("numpy>=2.4")
def test_size():
a = [[1, 2, 3], [4, 5, 6]]
ia = dpnp.array(a)
Expand All @@ -87,6 +89,12 @@ def test_size():
assert dpnp.size(a, 0) == exp
assert dpnp.size(ia, 0) == exp

assert dpnp.size(ia, 1) == numpy.size(a, 1)
assert dpnp.size(ia, ()) == 1 # numpy.size(a, ())
assert dpnp.size(ia, (0,)) == 2 # numpy.size(a, (0,))
assert dpnp.size(ia, (1,)) == 3 # numpy.size(a, (1,))
assert dpnp.size(ia, (0, 1)) == 6 # numpy.size(a, (0, 1))


class TestAppend:
@pytest.mark.parametrize(
Expand Down
Loading