Skip to content
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

Add dpnp.iterable function #2208

Merged
merged 3 commits into from
Dec 4, 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
42 changes: 42 additions & 0 deletions dpnp/dpnp_iface_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
"fill_diagonal",
"flatnonzero",
"indices",
"iterable",
"ix_",
"mask_indices",
"ndindex",
Expand Down Expand Up @@ -1033,6 +1034,47 @@ def indices(
return res


def iterable(y):
"""
Check whether or not an object can be iterated over.

For full documentation refer to :obj:`numpy.iterable`.

Parameters
----------
y : object
Input object.

Returns
-------
out : bool
Return ``True`` if the object has an iterator method or is a sequence
and ``False`` otherwise.

Examples
--------
>>> import dpnp as np
>>> np.iterable([1, 2, 3])
True
>>> np.iterable(2)
False

In most cases, the results of ``np.iterable(obj)`` are consistent with
``isinstance(obj, collections.abc.Iterable)``. One notable exception is
the treatment of 0-dimensional arrays:

>>> from collections.abc import Iterable
>>> a = np.array(1.0) # 0-dimensional array
>>> isinstance(a, Iterable)
True
>>> np.iterable(a)
False

"""

return numpy.iterable(y)


def ix_(*args):
"""Construct an open mesh from multiple sequences.

Expand Down
8 changes: 8 additions & 0 deletions dpnp/tests/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,14 @@ def test_ix_error(self, xp, shape):
assert_raises(ValueError, xp.ix_, xp.ones(shape))


class TestIterable:
@pytest.mark.parametrize("data", [[1.0], [2, 3]])
def test_basic(self, data):
a = numpy.array(data)
ia = dpnp.array(a)
assert dpnp.iterable(ia) == numpy.iterable(a)


@pytest.mark.parametrize(
"shape", [[1, 2, 3], [(1, 2, 3)], [(3,)], [3], [], [()], [0]]
)
Expand Down
Loading