Skip to content

Commit fd47618

Browse files
authored
Merge branch 'master' into impl-frexp
2 parents a1b2e51 + 904cbd6 commit fd47618

File tree

6 files changed

+138
-6
lines changed

6 files changed

+138
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Also, that release drops support for Python 3.9, making Python 3.10 the minimum
1818
* Added implementation of `dpnp.scipy.special.erfcx` [#2596](https://github.com/IntelPython/dpnp/pull/2596)
1919
* Added implementation of `dpnp.scipy.special.erfinv` and `dpnp.scipy.special.erfcinv` [#2624](https://github.com/IntelPython/dpnp/pull/2624)
2020
* Enabled support of Python 3.14 [#2631](https://github.com/IntelPython/dpnp/pull/2631)
21+
* Added implementation of `dpnp.ndarray.tolist` method [#2652](https://github.com/IntelPython/dpnp/pull/2652)
2122
* Added implementation of `dpnp.frexp` [#2635](https://github.com/IntelPython/dpnp/pull/2635)
2223

2324
### Changed

doc/reference/io.rst

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
.. currentmodule:: dpnp
2+
3+
Input and output
4+
================
5+
6+
.. hint:: `NumPy API Reference: Input and output <https://numpy.org/doc/stable/reference/routines.io.html>`_
7+
8+
.. NumPy binary files (npy, npz)
9+
.. -----------------------------
10+
.. .. autosummary::
11+
.. :toctree: generated/
12+
.. :nosignatures:
13+
14+
.. load
15+
.. save
16+
.. savez
17+
.. savez_compressed
18+
.. lib.npyio.NpzFile
19+
20+
.. The format of these binary file types is documented in
21+
.. :py:mod:`numpy.lib.format`
22+
23+
Text files
24+
----------
25+
.. autosummary::
26+
:toctree: generated/
27+
:nosignatures:
28+
29+
loadtxt
30+
savetxt
31+
genfromtxt
32+
fromregex
33+
fromstring
34+
ndarray.tofile
35+
ndarray.tolist
36+
37+
Raw binary files
38+
----------------
39+
40+
.. autosummary::
41+
:toctree: generated/
42+
:nosignatures:
43+
44+
fromfile
45+
ndarray.tofile
46+
47+
.. String formatting
48+
.. -----------------
49+
.. .. autosummary::
50+
.. :toctree: generated/
51+
.. :nosignatures:
52+
53+
.. array2string
54+
.. array_repr
55+
.. array_str
56+
.. format_float_positional
57+
.. format_float_scientific
58+
59+
.. Text formatting options
60+
.. -----------------------
61+
.. .. autosummary::
62+
.. :toctree: generated/
63+
.. :nosignatures:
64+
65+
.. set_printoptions
66+
.. get_printoptions
67+
.. printoptions
68+
69+
Base-n representations
70+
----------------------
71+
.. autosummary::
72+
:toctree: generated/
73+
:nosignatures:
74+
75+
binary_repr
76+
base_repr

doc/reference/routines.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ These functions cover a subset of
1818
exceptions
1919
fft
2020
functional
21+
io
2122
indexing
2223
linalg
2324
logic

dpnp/dpnp_array.py

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2014,7 +2014,51 @@ def to_device(self, device, /, *, stream=None):
20142014

20152015
# 'tobytes',
20162016
# 'tofile',
2017-
# 'tolist',
2017+
2018+
def tolist(self):
2019+
"""
2020+
Converts the array to a (possibly nested) Python list.
2021+
2022+
For full documentation refer to :obj:`numpy.ndarray.tolist`.
2023+
2024+
Returns
2025+
-------
2026+
out : list
2027+
The possibly nested Python list of array elements.
2028+
2029+
Examples
2030+
--------
2031+
For a 1D array, ``a.tolist()`` is almost the same as ``list(a)``,
2032+
except that ``tolist`` changes 0D arrays to Python scalars:
2033+
2034+
>>> import dpnp as np
2035+
>>> a = np.array([1, 2])
2036+
>>> list(a)
2037+
[array(1), array(2)]
2038+
>>> a.tolist()
2039+
[1, 2]
2040+
2041+
Additionally, for a 2D array, ``tolist`` applies recursively:
2042+
2043+
>>> a = np.array([[1, 2], [3, 4]])
2044+
>>> list(a)
2045+
[array([1, 2]), array([3, 4])]
2046+
>>> a.tolist()
2047+
[[1, 2], [3, 4]]
2048+
2049+
The base case for this recursion is a 0D array:
2050+
2051+
>>> a = np.array(1)
2052+
>>> list(a)
2053+
Traceback (most recent call last):
2054+
...
2055+
TypeError: iteration over a 0-d array
2056+
>>> a.tolist()
2057+
1
2058+
2059+
"""
2060+
2061+
return self.asnumpy().tolist()
20182062

20192063
def trace(self, offset=0, axis1=0, axis2=1, dtype=None, *, out=None):
20202064
"""

dpnp/tests/test_ndarray.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,16 @@ def test_strides(self):
107107
assert xp.full_like(a, fill_value=6) not in a
108108

109109

110+
class TestToList:
111+
@pytest.mark.parametrize(
112+
"data", [[1, 2], [[1, 2], [3, 4]]], ids=["1d", "2d"]
113+
)
114+
def test_basic(self, data):
115+
a = numpy.array(data)
116+
ia = dpnp.array(a)
117+
assert_array_equal(ia.tolist(), a.tolist())
118+
119+
110120
class TestView:
111121
def test_none_dtype(self):
112122
a = numpy.ones((1, 2, 4), dtype=numpy.int32)

dpnp/tests/third_party/cupy/logic_tests/test_type_test.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
from __future__ import annotations
2+
13
import unittest
24

35
import numpy
46
import pytest
57

6-
import dpnp as cupy
78
from dpnp.tests.third_party.cupy import testing
89

910

@@ -122,7 +123,6 @@ def test_scalar(self, xp, dtype):
122123
@testing.for_all_dtypes()
123124
@testing.numpy_cupy_equal()
124125
def test_list(self, xp, dtype):
125-
a = testing.shaped_arange((2, 3), xp, dtype)
126-
if xp == cupy:
127-
a = a.asnumpy()
128-
return getattr(xp, self.func)(a.tolist())
126+
return getattr(xp, self.func)(
127+
testing.shaped_arange((2, 3), xp, dtype).tolist()
128+
)

0 commit comments

Comments
 (0)