Skip to content

Commit 474100b

Browse files
committed
Update the docstring and add test
1 parent 4a5992c commit 474100b

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

dpnp/dpnp_iface_manipulation.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2504,14 +2504,17 @@ def pad(array, pad_width, mode="constant", **kwargs):
25042504
----------
25052505
array : {dpnp.ndarray, usm_ndarray}
25062506
The array of rank ``N`` to pad.
2507-
pad_width : {sequence, array_like, int}
2507+
pad_width : {sequence, array_like, int, dict}
25082508
Number of values padded to the edges of each axis.
25092509
``((before_1, after_1), ... (before_N, after_N))`` unique pad widths
25102510
for each axis.
25112511
``(before, after)`` or ``((before, after),)`` yields same before
25122512
and after pad for each axis.
25132513
``(pad,)`` or ``int`` is a shortcut for ``before = after = pad`` width
25142514
for all axes.
2515+
If a dictionary, each key is an axis and its corresponding value is an
2516+
integer or a pair of integers describing the padding ``(before, after)``
2517+
or ``pad`` width for that axis.
25152518
mode : {str, function}, optional
25162519
One of the following string values or a user supplied function.
25172520
@@ -2694,6 +2697,26 @@ def pad(array, pad_width, mode="constant", **kwargs):
26942697
[100, 100, 100, 100, 100, 100, 100],
26952698
[100, 100, 100, 100, 100, 100, 100]])
26962699
2700+
>>> a = np.arange(1, 7).reshape(2, 3)
2701+
>>> np.pad(a, {1: (1, 2)})
2702+
array([[0, 1, 2, 3, 0, 0],
2703+
[0, 4, 5, 6, 0, 0]])
2704+
>>> np.pad(a, {-1: 2})
2705+
array([[0, 0, 1, 2, 3, 0, 0],
2706+
[0, 0, 4, 5, 6, 0, 0]])
2707+
>>> np.pad(a, {0: (3, 0)})
2708+
array([[0, 0, 0],
2709+
[0, 0, 0],
2710+
[0, 0, 0],
2711+
[1, 2, 3],
2712+
[4, 5, 6]])
2713+
>>> np.pad(a, {0: (3, 0), 1: 2})
2714+
array([[0, 0, 0, 0, 0, 0, 0],
2715+
[0, 0, 0, 0, 0, 0, 0],
2716+
[0, 0, 0, 0, 0, 0, 0],
2717+
[0, 0, 1, 2, 3, 0, 0],
2718+
[0, 0, 4, 5, 6, 0, 0]])
2719+
26972720
"""
26982721

26992722
dpnp.check_supported_arrays_type(array)

dpnp/tests/test_arraypad.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,3 +539,21 @@ def test_as_pairs_exceptions(self):
539539
dpnp_as_pairs([[1, 2], [3, 4]], 3)
540540
with pytest.raises(ValueError, match="could not be broadcast"):
541541
dpnp_as_pairs(dpnp.ones((2, 3)), 3)
542+
543+
@testing.with_requires("numpy>=2.4")
544+
@pytest.mark.parametrize(
545+
"sh, pad_width",
546+
[
547+
((3, 4, 5), {-2: (1, 3)}),
548+
((3, 4, 5), {0: (5, 2)}),
549+
((3, 4, 5), {0: (5, 2), -1: (3, 4)}),
550+
((3, 4, 5), {1: 5}),
551+
],
552+
)
553+
def test_dict_pad_width(self, sh, pad_width):
554+
a = numpy.zeros(sh)
555+
ia = dpnp.array(a)
556+
557+
result = dpnp.pad(ia, pad_width)
558+
expected = numpy.pad(a, pad_width)
559+
assert_equal(result, expected)

0 commit comments

Comments
 (0)