constant_pad#
- ivy.constant_pad(x, /, pad_width, *, value=0, out=None)[source]#
Pad an array with a constant value.
- Parameters:
x (
Union
[Array
,NativeArray
]) – Input array to pad.pad_width (
Iterable
[Tuple
[int
]]) – Number of values padded to the edges of each axis. Specified as ((before_1, after_1), … (before_N, after_N)), where N is number of axes of x.value (
Number
, default:0
) – The constant value to pad the array with.out (
Optional
[Array
], default:None
) – optional output array, for writing the result to. It must have a shape that the inputs broadcast to.
- Return type:
- Returns:
ret – Padded array of rank equal to x with shape increased according to pad_width.
Both the description and the type hints above assumes an array input for simplicity, but this function is nestable, and therefore also accepts
ivy.Container
instances in place of any of the arguments.Examples
With
ivy.Array
input:>>> x = ivy.array([1, 2, 3, 4, 5]) >>> y = ivy.constant_pad(x, pad_width = [[2, 3]]) >>> print(y) ivy.array([0, 0, 1, 2, 3, 4, 5, 0, 0, 0])
>>> x = ivy.array([[1, 2], [3, 4]]) >>> y = ivy.constant_pad(x, pad_width=[(2, 3), (2, 3)]) >>> print(y) ivy.array([[0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 2, 0, 0, 0], [0, 0, 3, 4, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0]])
>>> x = ivy.array([[1, 2], [3, 4]]) >>> y = ivy.constant_pad(x, pad_width = [[3, 2], [2, 3]]) >>> print(y) ivy.array([[0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 2, 0, 0, 0], [0, 0, 3, 4, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0]])
>>> x = ivy.array([[2.], [3.]]) >>> y = ivy.zeros((4, 3)) >>> ivy.constant_pad(x, pad_width = [(1, 1), (1, 1)], value = 5.0, out= y) >>> print(y) ivy.array([[5., 5., 5.], [5., 2., 5.], [5., 3., 5.], [5., 5., 5.]])
With
ivy.Container
input:>>> x = ivy.Container(a = ivy.array([1., 2., 3.]), ... b = ivy.array([3., 4., 5.])) >>> y = ivy.constant_pad(x, pad_width = [[2, 3]], value = 5.0) >>> print(y) { a: ivy.array([5., 5., 1., 2., 3., 5., 5., 5.]), b: ivy.array([5., 5., 3., 4., 5., 5., 5., 5.]) }
- Array.constant_pad(self, /, pad_width, *, value=0, out=None)[source]#
ivy.Array instance method variant of ivy.constant_pad. This method simply wraps the function, and so the docstring for ivy.constant_pad also applies to this method with minimal changes.
- Parameters:
self (
Array
) – Input array to pad.pad_width (
Iterable
[Tuple
[int
]]) – Number of values padded to the edges of each axis. Specified as ((before_1, after_1), … (before_N, after_N)), where N is number of axes of x.value (
Number
, default:0
) – The constant value to pad the array with.out (
Optional
[Array
], default:None
) – optional output array, for writing the result to. It must have a shape that the inputs broadcast to.
- Return type:
Array
- Returns:
ret – Padded array of rank equal to x with shape increased according to pad_width.
Examples
>>> x = ivy.array([1., 2., 3.]) >>> y = x.constant_pad(pad_width = [[2, 3]]) >>> print(y) ivy.array([0., 0., 1., 2., 3., 0., 0., 0.])
- Container.constant_pad(self, /, pad_width, *, value=0, key_chains=None, to_apply=True, prune_unapplied=False, map_sequences=False, out=None)[source]#
ivy.Container instance method variant of ivy.constant_pad. This method simply wraps the function, and so the docstring for ivy.constant_pad also applies to this method with minimal changes.
- Parameters:
self (
Container
) – Input container with leaves to pad.pad_width (
Union
[Iterable
[Tuple
[int
]],Container
]) – Number of values padded to the edges of each axis. Specified as ((before_1, after_1), … (before_N, after_N)), where N is number of axes of x.value (
Union
[Number
,Container
], default:0
) – The constant value to pad the array with.out (
Optional
[Container
], default:None
) – optional output array, for writing the result to. It must have a shape that the inputs broadcast to.
- Return type:
Container
- Returns:
ret – Output container with padded array leaves of rank equal to x with shape increased according to pad_width.
Examples
>>> x = ivy.Container(a = ivy.array([1, 2, 3]), b = ivy.array([4, 5, 6])) >>> y = x.constant_pad(pad_width = [[2, 3]]) >>> print(y) { a: ivy.array([0, 0, 1, 2, 3, 0, 0, 0]), b: ivy.array([0, 0, 4, 5, 6, 0, 0, 0]) }