Skip to content

Commit 8259f82

Browse files
authored
add indices (#486)
1 parent 03d7a98 commit 8259f82

File tree

5 files changed

+72
-8
lines changed

5 files changed

+72
-8
lines changed

dpnp/dpnp_algo/dpnp_algo_indexing.pyx

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ __all__ += [
4242
"dpnp_diag_indices",
4343
"dpnp_diagonal",
4444
"dpnp_fill_diagonal",
45+
"dpnp_indices",
4546
"dpnp_nonzero",
4647
"dpnp_place",
4748
"dpnp_put",
@@ -143,6 +144,40 @@ cpdef dpnp_fill_diagonal(dparray input, val):
143144
input[ind] = val
144145

145146

147+
cpdef dparray dpnp_indices(dimensions):
148+
len_dimensions = len(dimensions)
149+
res_shape = []
150+
res_shape.append(len_dimensions)
151+
for i in range(len_dimensions):
152+
res_shape.append(dimensions[i])
153+
154+
result = []
155+
if len_dimensions == 1:
156+
res = []
157+
for i in range(dimensions[0]):
158+
res.append(i)
159+
result.append(res)
160+
else:
161+
res1 = []
162+
for i in range(dimensions[0]):
163+
res = []
164+
for j in range(dimensions[1]):
165+
res.append(i)
166+
res1.append(res)
167+
result.append(res1)
168+
169+
res2 = []
170+
for i in range(dimensions[0]):
171+
res = []
172+
for j in range(dimensions[1]):
173+
res.append(j)
174+
res2.append(res)
175+
result.append(res2)
176+
177+
dpnp_result = dpnp.array(result)
178+
return dpnp_result
179+
180+
146181
cpdef tuple dpnp_nonzero(dparray in_array1):
147182
res_count = in_array1.ndim
148183

dpnp/dpnp_iface_indexing.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
"diag_indices_from",
5656
"diagonal",
5757
"fill_diagonal",
58+
"indices",
5859
"nonzero",
5960
"place",
6061
"put",
@@ -220,6 +221,32 @@ def fill_diagonal(input, val, wrap=False):
220221
return call_origin(numpy.fill_diagonal, input, val, wrap)
221222

222223

224+
def indices(dimensions, dtype=int, sparse=False):
225+
"""
226+
Return an array representing the indices of a grid.
227+
228+
For full documentation refer to :obj:`numpy.indices`.
229+
230+
Limitations
231+
-----------
232+
Parameters ``dtype`` and ``sparse`` are supported only with default values.
233+
Parameter ``dimensions`` is supported with len <=2.
234+
"""
235+
236+
if not isinstance(dimensions, (tuple, list)):
237+
pass
238+
elif len(dimensions) > 2 or len(dimensions) == 0:
239+
pass
240+
elif dtype != int:
241+
pass
242+
elif sparse:
243+
pass
244+
else:
245+
return dpnp_indices(dimensions)
246+
247+
return call_origin(numpy.indices, dimensions, dtype, sparse)
248+
249+
223250
def nonzero(a):
224251
"""
225252
Return the indices of the elements that are non-zero.

tests/skipped_tests.tbl

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -522,10 +522,6 @@ tests/third_party/cupy/fft_tests/test_fft.py::TestRfft_param_12_{n=15, norm=None
522522
tests/third_party/cupy/fft_tests/test_fft.py::TestRfft_param_13_{n=15, norm=None, shape=(10, 10)}::test_rfft
523523
tests/third_party/cupy/fft_tests/test_fft.py::TestRfft_param_14_{n=15, norm='ortho', shape=(10,)}::test_rfft
524524
tests/third_party/cupy/fft_tests/test_fft.py::TestRfft_param_15_{n=15, norm='ortho', shape=(10, 10)}::test_rfft
525-
tests/third_party/cupy/indexing_tests/test_generate.py::TestIndices::test_indices_list0
526-
tests/third_party/cupy/indexing_tests/test_generate.py::TestIndices::test_indices_list1
527-
tests/third_party/cupy/indexing_tests/test_generate.py::TestIndices::test_indices_list2
528-
tests/third_party/cupy/indexing_tests/test_generate.py::TestIndices::test_indices_list3
529525
tests/third_party/cupy/indexing_tests/test_generate.py::TestIX_::test_ix_bool_ndarray
530526
tests/third_party/cupy/indexing_tests/test_generate.py::TestIX_::test_ix_empty_ndarray
531527
tests/third_party/cupy/indexing_tests/test_generate.py::TestIX_::test_ix_list

tests/skipped_tests_gpu.tbl

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -675,10 +675,6 @@ tests/third_party/cupy/fft_tests/test_fft.py::TestRfft_param_8_{n=10, norm=None,
675675
tests/third_party/cupy/fft_tests/test_fft.py::TestRfft_param_8_{n=10, norm=None, shape=(10,)}::test_rfft
676676
tests/third_party/cupy/fft_tests/test_fft.py::TestRfft_param_9_{n=10, norm=None, shape=(10, 10)}::test_irfft
677677
tests/third_party/cupy/fft_tests/test_fft.py::TestRfft_param_9_{n=10, norm=None, shape=(10, 10)}::test_rfft
678-
tests/third_party/cupy/indexing_tests/test_generate.py::TestIndices::test_indices_list0
679-
tests/third_party/cupy/indexing_tests/test_generate.py::TestIndices::test_indices_list1
680-
tests/third_party/cupy/indexing_tests/test_generate.py::TestIndices::test_indices_list2
681-
tests/third_party/cupy/indexing_tests/test_generate.py::TestIndices::test_indices_list3
682678
tests/third_party/cupy/indexing_tests/test_generate.py::TestIX_::test_ix_bool_ndarray
683679
tests/third_party/cupy/indexing_tests/test_generate.py::TestIX_::test_ix_empty_ndarray
684680
tests/third_party/cupy/indexing_tests/test_generate.py::TestIX_::test_ix_list

tests/test_indexing.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,16 @@ def test_fill_diagonal(array, val):
6060
numpy.testing.assert_array_equal(expected, result)
6161

6262

63+
@pytest.mark.parametrize("dimension",
64+
[(1, ), (2, ), (1, 2), (2, 3), (3, 2), [1], [2], [1, 2], [2, 3], [3, 2]],
65+
ids=['(1, )', '(2, )', '(1, 2)', '(2, 3)', '(3, 2)',
66+
'[1]', '[2]', '[1, 2]', '[2, 3]', '[3, 2]'])
67+
def test_indices(dimension):
68+
expected = numpy.indices(dimension)
69+
result = dpnp.indices(dimension)
70+
numpy.testing.assert_array_equal(expected, result)
71+
72+
6373
@pytest.mark.parametrize("vals",
6474
[[100, 200],
6575
(100, 200)],

0 commit comments

Comments
 (0)