Skip to content

Commit 4850e53

Browse files
authored
Merge pull request #1586 from IntelPython/implement_tile
Implemented dpnp.tile function.
2 parents a73d959 + aae4cff commit 4850e53

File tree

3 files changed

+80
-17
lines changed

3 files changed

+80
-17
lines changed

dpnp/dpnp_iface_manipulation.py

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
"squeeze",
7474
"stack",
7575
"swapaxes",
76+
"tile",
7677
"transpose",
7778
"unique",
7879
"vstack",
@@ -945,7 +946,7 @@ def reshape(a, /, newshape, order="C", copy=None):
945946
946947
Parameters
947948
----------
948-
a : {dpnp_array, usm_ndarray}
949+
a : {dpnp.ndarray, usm_ndarray}
949950
Array to be reshaped.
950951
newshape : int or tuple of ints
951952
The new shape should be compatible with the original shape. If
@@ -1387,6 +1388,84 @@ def swapaxes(a, axis1, axis2):
13871388
)
13881389

13891390

1391+
def tile(A, reps):
1392+
"""
1393+
Construct an array by repeating `A` the number of times given by reps.
1394+
1395+
If `reps` has length ``d``, the result will have dimension of
1396+
``max(d, A.ndim)``.
1397+
1398+
If ``A.ndim < d``, `A` is promoted to be d-dimensional by prepending new
1399+
axes. So a shape (3,) array is promoted to (1, 3) for 2-D replication,
1400+
or shape (1, 1, 3) for 3-D replication. If this is not the desired
1401+
behavior, promote `A` to d-dimensions manually before calling this
1402+
function.
1403+
1404+
If ``A.ndim > d``, `reps` is promoted to `A`.ndim by prepending 1's to it.
1405+
Thus for an `A` of shape (2, 3, 4, 5), a `reps` of (2, 2) is treated as
1406+
(1, 1, 2, 2).
1407+
1408+
Note : Although tile may be used for broadcasting, it is strongly
1409+
recommended to use dpnp's broadcasting operations and functions.
1410+
1411+
For full documentation refer to :obj:`numpy.tile`.
1412+
1413+
Parameters
1414+
----------
1415+
A : dpnp.ndarray
1416+
The input array.
1417+
reps : array_like
1418+
The number of repetitions of `A` along each axis.
1419+
1420+
Returns
1421+
-------
1422+
c : dpnp.ndarray
1423+
The tiled output array.
1424+
1425+
See Also
1426+
--------
1427+
:obj:`dpnp.repeat` : Repeat elements of an array.
1428+
:obj:`dpnp.broadcast_to` : Broadcast an array to a new shape
1429+
1430+
Examples
1431+
--------
1432+
>>> import dpnp as np
1433+
>>> a = np.array([0, 1, 2])
1434+
>>> np.tile(a, 2)
1435+
array([0, 1, 2, 0, 1, 2])
1436+
1437+
>>> np.tile(a, (2, 2))
1438+
array([[0, 1, 2, 0, 1, 2],
1439+
[0, 1, 2, 0, 1, 2]])
1440+
1441+
>>> np.tile(a, (2, 1, 2))
1442+
array([[[0, 1, 2, 0, 1, 2]],
1443+
[[0, 1, 2, 0, 1, 2]]])
1444+
1445+
>>> b = np.array([[1, 2], [3, 4]])
1446+
>>> np.tile(b, 2)
1447+
array([[1, 2, 1, 2],
1448+
[3, 4, 3, 4]])
1449+
1450+
>>> np.tile(b, (2, 1))
1451+
array([[1, 2],
1452+
[3, 4],
1453+
[1, 2],
1454+
[3, 4]])
1455+
1456+
>>> c = np.array([1, 2, 3, 4])
1457+
>>> np.tile(c, (4, 1))
1458+
array([[1, 2, 3, 4],
1459+
[1, 2, 3, 4],
1460+
[1, 2, 3, 4],
1461+
[1, 2, 3, 4]])
1462+
1463+
"""
1464+
1465+
dpt_array = dpnp.get_usm_ndarray(A)
1466+
return dpnp_array._create_from_usm_ndarray(dpt.tile(dpt_array, reps))
1467+
1468+
13901469
def transpose(a, axes=None):
13911470
"""
13921471
Returns an array with axes transposed.

tests/skipped_tests.tbl

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -468,14 +468,6 @@ tests/third_party/cupy/manipulation_tests/test_shape.py::TestReshape::test_resha
468468

469469
tests/third_party/cupy/manipulation_tests/test_tiling.py::TestRepeatRepeatsNdarray::test_func
470470
tests/third_party/cupy/manipulation_tests/test_tiling.py::TestRepeatRepeatsNdarray::test_method
471-
tests/third_party/cupy/manipulation_tests/test_tiling.py::TestTileFailure_param_0_{reps=-1}::test_tile_failure
472-
tests/third_party/cupy/manipulation_tests/test_tiling.py::TestTileFailure_param_1_{reps=(-1, -2)}::test_tile_failure
473-
tests/third_party/cupy/manipulation_tests/test_tiling.py::TestTile_param_0_{reps=0}::test_array_tile
474-
tests/third_party/cupy/manipulation_tests/test_tiling.py::TestTile_param_1_{reps=1}::test_array_tile
475-
tests/third_party/cupy/manipulation_tests/test_tiling.py::TestTile_param_2_{reps=2}::test_array_tile
476-
tests/third_party/cupy/manipulation_tests/test_tiling.py::TestTile_param_3_{reps=(0, 1)}::test_array_tile
477-
tests/third_party/cupy/manipulation_tests/test_tiling.py::TestTile_param_4_{reps=(2, 3)}::test_array_tile
478-
tests/third_party/cupy/manipulation_tests/test_tiling.py::TestTile_param_5_{reps=(2, 3, 4, 5)}::test_array_tile
479471
tests/third_party/cupy/math_tests/test_arithmetic.py::TestArithmeticBinary2_param_457_{arg1=array([[1, 2, 3], [4, 5, 6]], dtype=int32), arg2=array([[0, 1, 2], [3, 4, 5]], dtype=int32), dtype=float64, name='fmod', use_dtype=False}::test_binary
480472
tests/third_party/cupy/math_tests/test_arithmetic.py::TestArithmeticBinary2_param_465_{arg1=array([[1, 2, 3], [4, 5, 6]], dtype=int32), arg2=array([[0, 1, 2], [3, 4, 5]]), dtype=float64, name='fmod', use_dtype=False}::test_binary
481473
tests/third_party/cupy/math_tests/test_arithmetic.py::TestArithmeticBinary2_param_537_{arg1=array([[1, 2, 3], [4, 5, 6]]), arg2=array([[0, 1, 2], [3, 4, 5]], dtype=int32), dtype=float64, name='fmod', use_dtype=False}::test_binary

tests/skipped_tests_gpu.tbl

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -612,14 +612,6 @@ tests/third_party/cupy/manipulation_tests/test_shape.py::TestReshape::test_resha
612612

613613
tests/third_party/cupy/manipulation_tests/test_tiling.py::TestRepeatRepeatsNdarray::test_func
614614
tests/third_party/cupy/manipulation_tests/test_tiling.py::TestRepeatRepeatsNdarray::test_method
615-
tests/third_party/cupy/manipulation_tests/test_tiling.py::TestTileFailure_param_0_{reps=-1}::test_tile_failure
616-
tests/third_party/cupy/manipulation_tests/test_tiling.py::TestTileFailure_param_1_{reps=(-1, -2)}::test_tile_failure
617-
tests/third_party/cupy/manipulation_tests/test_tiling.py::TestTile_param_0_{reps=0}::test_array_tile
618-
tests/third_party/cupy/manipulation_tests/test_tiling.py::TestTile_param_1_{reps=1}::test_array_tile
619-
tests/third_party/cupy/manipulation_tests/test_tiling.py::TestTile_param_2_{reps=2}::test_array_tile
620-
tests/third_party/cupy/manipulation_tests/test_tiling.py::TestTile_param_3_{reps=(0, 1)}::test_array_tile
621-
tests/third_party/cupy/manipulation_tests/test_tiling.py::TestTile_param_4_{reps=(2, 3)}::test_array_tile
622-
tests/third_party/cupy/manipulation_tests/test_tiling.py::TestTile_param_5_{reps=(2, 3, 4, 5)}::test_array_tile
623615

624616
tests/third_party/cupy/math_tests/test_arithmetic.py::TestArithmeticRaisesWithNumpyInput_param_3_{name='angle', nargs=1}::test_raises_with_numpy_input
625617

0 commit comments

Comments
 (0)