Manipulation#

class ivy.data_classes.array.manipulation._ArrayWithManipulation[source]#

Bases: ABC

_abc_impl = <_abc._abc_data object>#
clip(x_min=None, x_max=None, *, out=None)[source]#

ivy.Array instance method variant of ivy.clip. This method simply wraps the function, and so the docstring for ivy.clip also applies to this method with minimal changes.

Parameters:
  • self (Array) – Input array containing elements to clip.

  • x_min (Optional[Union[Number, Array, NativeArray]], default: None) – Minimum value.

  • x_max (Optional[Union[Number, Array, NativeArray]], default: None) – Maximum value.

  • 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 – An array with the elements of self, but where values < x_min are replaced with x_min, and those > x_max with x_max.

Examples

>>> x = ivy.array([0., 1., 2., 3., 4., 5., 6., 7., 8., 9.])
>>> y = x.clip(1., 5.)
>>> print(y)
ivy.array([1., 1., 2., 3., 4., 5., 5., 5., 5., 5.])
concat(xs, /, *, axis=0, out=None)[source]#

ivy.Array instance method variant of ivy.concat. This method simply wraps the function, and so the docstring for ivy.concat also applies to this method with minimal changes.

Parameters:
  • self (Array) – input array to join with other arrays xs.

  • xs (Union[Tuple[Union[Array, NativeArray], ...], List[Union[Array, NativeArray]]]) – The other arrays to join with. The arrays must have the same shape, except in the dimension specified by axis.

  • axis (int, default: 0) – axis along which the arrays will be joined. If axis is None, arrays must be flattened before concatenation. If axis is negative, axis on which to join arrays is determined by counting from the top. Default: 0.

  • 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 – an output array containing the concatenated values.

constant_pad(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.])
expand_dims(*, copy=None, axis=0, out=None)[source]#

ivy.Array instance method variant of ivy.expand_dims. This method simply wraps the function, and so the docstring for ivy.expand_dims also applies to this method with minimal changes.

Parameters:
  • self (Array) – input array.

  • axis (Union[int, Sequence[int]], default: 0) – position in the expanded array where a new axis (dimension) of size one will be added. If array self has the rank of N, the axis needs to be between [-N-1, N]. Default: 0.

  • copy (Optional[bool], default: None) – boolean indicating whether or not to copy the input array. If True, the function must always copy. If False, the function must never copy. In case copy is False we avoid copying by returning a view of the input array.

  • 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 – An array with the elements of self, but with its dimension added by one in a given axis.

Examples

>>> x = ivy.array([-4.7, -2.3, 0.7]) #x.shape->(3,)
>>> y = x.expand_dims() #y.shape->(1, 3)
>>> print(y)
ivy.array([[-4.7, -2.3,  0.7]])
flip(*, copy=None, axis=None, out=None)[source]#

ivy.Array instance method variant of ivy.flip. This method simply wraps the function, and so the docstring for ivy.flip also applies to this method with minimal changes.

Parameters:
  • self (Array) – input array.

  • axis (Optional[Union[int, Sequence[int]]], default: None) – axis (or axes) along which to flip. If axis is None, all input array axes are flipped. If axis is negative, axis is counted from the last dimension. If provided more than one axis, only the specified axes. Default: None.

  • copy (Optional[bool], default: None) –

    boolean indicating whether or not to copy the input array. If True, the function must always copy. If False, the function must never copy. In case copy is False we avoid copying by returning

    a view of the input array.

  • 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 – an output array having the same data type and shape as``self`` and whose elements, relative to self, are reordered.

Examples

>>> x = ivy.array([1, 2, 3])
>>> y = x.flip()
>>> print(y)
ivy.array([3, 2, 1])
>>> x = ivy.array([4, 5, 6])
>>> y = x.flip(axis=0)
>>> print(y)
ivy.array([6, 5, 4])
permute_dims(axes, *, copy=None, out=None)[source]#

ivy.Array instance method variant of ivy.permute_dims. This method simply wraps the function, and so the docstring for ivy.permute_dims also applies to this method with minimal changes.

Parameters:
  • self (Array) – input array.

  • axes (Tuple[int, ...]) – tuple containing a permutation of (0, 1, …, N-1) where N is the number of axes (dimensions) of x.

  • copy (Optional[bool], default: None) – boolean indicating whether or not to copy the input array. If True, the function must always copy. If False, the function must never copy. In case copy is False we avoid copying by returning a view of the input array.

  • 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 – an array containing the axes permutation. The returned array must have the same data type as x.

Examples

With ivy.Array input:

>>> x = ivy.array([[1, 2, 3], [4, 5, 6]])
>>> y = x.permute_dims(axes=(1, 0))
>>> print(y)
ivy.array([[1, 4],
           [2, 5],
           [3, 6]])
>>> x = ivy.zeros((2, 3))
>>> y = x.permute_dims(axes=(1, 0))
>>> print(y)
ivy.array([[0., 0.],
           [0., 0.],
           [0., 0.]])
repeat(repeats, *, axis=None, out=None)[source]#

ivy.Array instance method variant of ivy.repeat. This method simply wraps the function, and so the docstring for ivy.repeat also applies to this method with minimal changes.

Parameters:
  • self (Array) – Input array.

  • repeats (Union[int, Iterable[int]]) – The number of repetitions for each element. repeats is broadcast to fit the shape of the given axis.

  • axis (Optional[Union[int, Sequence[int]]], default: None) – The axis along which to repeat values. By default, use the flattened input array, and return a flat output array.

  • 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 – The repeated output array.

Examples

>>> x = ivy.array([0., 1., 2.])
>>> y= x.repeat(2)
>>> print(y)
ivy.array([0., 0., 1., 1., 2., 2.])
reshape(shape, *, copy=None, order='C', allowzero=True, out=None)[source]#

ivy.Array instance method variant of ivy.reshape. This method simply wraps the function, and so the docstring for ivy.reshape also applies to this method with minimal changes.

Parameters:
  • self (Array) – input array.

  • shape (Union[Shape, NativeShape, Sequence[int]]) – The new shape should be compatible with the original shape. One shape dimension can be -1. In this case, the value is inferred from the length of the array and remaining dimensions.

  • copy (Optional[bool], default: None) –

    boolean indicating whether or not to copy the input array. If True, the function must always copy. If False, the function must never copy. In case copy is False we avoid copying by returning

    a view of the input array.

  • order (str, default: 'C') – Read the elements of the input array using this index order, and place the elements into the reshaped array using this index order. ‘C’ means to read / write the elements using C-like index order, with the last axis index changing fastest, back to the first axis index changing slowest. ‘F’ means to read / write the elements using Fortran-like index order, with the first index changing fastest, and the last index changing slowest. Note that the ‘C’ and ‘F’ options take no account of the memory layout of the underlying array, and only refer to the order of indexing. Default order is ‘C’

  • 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 – an output array having the same data type as self and elements as self.

Examples

>>> x = ivy.array([[0., 1., 2.],[3., 4., 5.]])
>>> y = x.reshape((3,2))
>>> print(y)
ivy.array([[0., 1.],
           [2., 3.],
           [4., 5.]])
>>> x = ivy.array([[0., 1., 2.],[3., 4., 5.]])
>>> y = x.reshape((3,2), order='F')
>>> print(y)
ivy.array([[0., 4.],
           [3., 2.],
           [1., 5.]])
roll(shift, *, axis=None, out=None)[source]#

ivy.Array instance method variant of ivy.roll. This method simply wraps the function, and so the docstring for ivy.roll also applies to this method with minimal changes.

Parameters:
  • self (Array) – input array.

  • shift (Union[int, Sequence[int]]) – number of places by which the elements are shifted. If shift is a tuple, then axis must be a tuple of the same size, and each of the given axes must be shifted by the corresponding element in shift. If shift is an int and axis a tuple, then the same shift must be used for all specified axes. If a shift is positive, then array elements must be shifted positively (toward larger indices) along the dimension of axis. If a shift is negative, then array elements must be shifted negatively (toward smaller indices) along the dimension of axis.

  • axis (Optional[Union[int, Sequence[int]]], default: None) – axis (or axes) along which elements to shift. If axis is None, the array must be flattened, shifted, and then restored to its original shape. Default None.

  • 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 – an output array having the same data type as self and whose elements, relative to self, are shifted.

Examples

>>> x = ivy.array([0., 1., 2.])
>>> y = x.roll(1)
>>> print(y)
ivy.array([2., 0., 1.])
>>> x = ivy.array([[0., 1., 2.],
...                [3., 4., 5.]])
>>> y = x.roll(2, axis=-1)
>>> print(y)
ivy.array([[1., 2., 0.],
            [4., 5., 3.]])
split(*, copy=None, num_or_size_splits=None, axis=0, with_remainder=False)[source]#

ivy.Array instance method variant of ivy.split. This method simply wraps the function, and so the docstring for ivy.split also applies to this method with minimal changes.

Parameters:
  • self (Array) – array to be divided into sub-arrays.

  • copy (Optional[bool], default: None) – boolean indicating whether or not to copy the input array. If True, the function must always copy. If False, the function must never copy. In case copy is False we avoid copying by returning a view of the input array.

  • num_or_size_splits (Optional[Union[int, Sequence[int], Array, NativeArray]], default: None) – Number of equal arrays to divide the array into along the given axis if an integer. The size of each split element if a sequence of integers or 1-D array. Default is to divide into as many 1-dimensional arrays as the axis dimension.

  • axis (int, default: 0) – The axis along which to split, default is 0.

  • with_remainder (bool, default: False) – If the tensor does not split evenly, then store the last remainder entry. Default is False.

Return type:

List[Array]

Returns:

A list of sub-arrays.

Examples

>>> x = ivy.array([4, 6, 5, 3])
>>> y = x.split()
>>> print(y)
[ivy.array([4]),ivy.array([6]),ivy.array([5]),ivy.array([3])]
squeeze(*, axis, copy=None, out=None)[source]#

ivy.Array instance method variant of ivy.squeeze. This method simply wraps the function, and so the docstring for ivy.squeeze also applies to this method with minimal changes.

Parameters:
  • self (Array) – input array.

  • axis (Optional[Union[int, Sequence[int]]]) – axis (or axes) to squeeze. If a specified axis has a size greater than one, a ValueError is. If None, then all squeezable axes are squeezed. Default: None.

  • copy (Optional[bool], default: None) –

    boolean indicating whether or not to copy the input array. If True, the function must always copy. If False, the function must never copy. In case copy is False we avoid copying by returning

    a view of the input array.

  • 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 – an output array having the same data type and elements as x.

Examples

>>> x = ivy.array([[[0.],[ 1.]]])
>>> y = x.squeeze(axis=2)
>>> print(y)
ivy.array([[0., 1.]])
stack(arrays, *, axis=0, out=None)[source]#

ivy.Array instance method variant of ivy.stack. This method simply wraps the function, and so the docstring for ivy.stack also applies to this method with minimal changes.

Parameters:
  • self (Array) – Array to join with other arrays.

  • arrays (Union[Tuple[Union[Array, NativeArray]], List[Union[Array, NativeArray]]]) – Other arrays to join with. Each array must have the same shape.

  • axis (int, default: 0) – axis along which the arrays will be joined. More details can be found in the ivy.stack documentation.

  • 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 – output array made by joining the input arrays along the specified axis.

Examples

>>> x = ivy.array([1, 2])
>>> y = ivy.array([5, 6])
>>> print(x.stack(y, axis=1))
ivy.array([[1, 5],
        [2, 6]])
>>> x.stack([y],axis=0)
ivy.array([[[1, 2]],
        [[5, 6]]])
swapaxes(axis0, axis1, /, *, copy=None, out=None)[source]#

ivy.Array instance method variant of ivy.swap_axes. This method simply wraps the function, and so the docstring for ivy.split also applies to this method with minimal changes.

Parameters:
  • self (Array) – Input array.

  • axis0 (int) – First axis to be swapped.

  • axis1 (int) – Second axis to be swapped.

  • copy (Optional[bool], default: None) –

    boolean indicating whether or not to copy the input array. If True, the function must always copy. If False, the function must never copy. In case copy is False we avoid copying by returning

    a view of the input array.

  • 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 – x with its axes permuted.

Examples

Using ivy.Array instance method:

>>> x = ivy.array([[0., 1., 2.]])
>>> y = x.swapaxes(0, 1)
>>> print(y)
ivy.array([[0.],
           [1.],
           [2.]])
>>> x = ivy.array([[[0,1],[2,3]],[[4,5],[6,7]]])
>>> y = x.swapaxes(0, 2)
>>> print(y)
ivy.array([[[0, 4],
            [2, 6]],
           [[1, 5],
            [3, 7]]])
tile(repeats, *, out=None)[source]#

ivy.Array instance method variant of ivy.tile. This method simply wraps the function, and so the docstring for ivy.tile also applies to this method with minimal changes.

Parameters:
  • self (Array) – Input array.

  • repeats (Iterable[int]) – The number of repetitions of x along each axis.

  • 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 – The tiled output array.

Examples

>>> x = ivy.array([[0], [1], [2]])
>>> y = x.tile((3,2))
>>> print(y)
ivy.array([[0,0],
           [1,1],
           [2,2],
           [0,0],
           [1,1],
           [2,2],
           [0,0],
           [1,1],
           [2,2]])
unstack(*, copy=None, axis=0, keepdims=False)[source]#

ivy.Array instance method variant of ivy.unstack. This method simply wraps the function, and so the docstring for ivy.unstack also applies to this method with minimal changes.

Parameters:
  • self (Array) – Input array to unstack.

  • copy (Optional[bool], default: None) –

    boolean indicating whether or not to copy the input array. If True, the function must always copy. If False, the function must never copy. In case copy is False we avoid copying by returning

    a view of the input array.

  • axis (int, default: 0) – Axis for which to unpack the array.

  • keepdims (bool, default: False) – Whether to keep dimension 1 in the unstack dimensions. Default is False.

Return type:

Array

Returns:

ret – List of arrays, unpacked along specified dimensions.

Examples

>>> x = ivy.array([[1, 2], [3, 4]])
>>> y = x.unstack(axis=0)
>>> print(y)
[ivy.array([1, 2]), ivy.array([3, 4])]
>>> x = ivy.array([[1, 2], [3, 4]])
>>> y = x.unstack(axis=1, keepdims=True)
>>> print(y)
[ivy.array([[1],
        [3]]), ivy.array([[2],
        [4]])]
view(shape=None)[source]#
Return type:

Array

zero_pad(pad_width, *, out=None)[source]#

ivy.Array instance method variant of ivy.zero_pad. This method simply wraps the function, and so the docstring for ivy.zero_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.

  • 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

With ivy.Array input:

>>> x = ivy.array([1., 2., 3.,4, 5, 6])
>>> y = x.zero_pad(pad_width = [[2, 3]])
>>> print(y)
ivy.array([0., 0., 1., 2., 3., 4., 5., 6., 0., 0., 0.])