flip#

ivy.flip(x, /, *, copy=None, axis=None, out=None)[source]#

Reverses the order of elements in an array along the given axis. The shape of the array must be preserved.

Parameters:
  • x (Union[Array, NativeArray]) – input array.

  • 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 (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.

  • 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`x and whose elements, relative to x, are reordered.

This function conforms to the Array API Standard. This docstring is an extension of the docstring in the standard.

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([3, 4, 5])
>>> y = ivy.flip(x)
>>> print(y)
ivy.array([5, 4, 3])
>>> x = ivy.array([[1, 2, 3], [4, 5, 6]])
>>> y = ivy.zeros((2, 3))
>>> ivy.flip(x, out=y)
>>> print(y)
ivy.array([[6, 5, 4],
           [3, 2, 1]])
>>> x = ivy.array([[1, 2, 3], [4, 5, 6]])
>>> y = ivy.zeros((2, 3))
>>> ivy.flip(x, axis=0, out=y)
>>> print(y)
ivy.array([[4, 5, 6],
           [1, 2, 3]])
>>> x = ivy.array([[[1, 2, 3], [4, 5, 6]],[[7, 8, 9], [10, 11, 12]]])
>>> ivy.flip(x, axis=[0, 1], out=x)
>>> print(x)
ivy.array([[[10,11,12],[7,8,9]],[[4,5,6],[1,2,3]]])
>>> x = ivy.array([[[1, 2, 3], [4, 5, 6]],[[7, 8, 9], [10, 11, 12]]])
>>> ivy.flip(x, axis=(2, 1), out=x)
>>> print(x)
ivy.array([[[ 6,  5,  4],
            [ 3,  2,  1]],
           [[12, 11, 10],
            [ 9,  8,  7]]])
Array.flip(self, /, *, 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])
Container.flip(self, /, *, copy=None, axis=None, key_chains=None, to_apply=True, prune_unapplied=False, map_sequences=False, out=None)[source]#

ivy.Container 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 (Container) – input container.

  • copy (Optional[Union[bool, Container]], 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 and must raise a ValueError in case a copy would be necessary. If None, the function must reuse existing memory buffer if possible and copy otherwise. Default: None.

  • axis (Optional[Union[int, Sequence[int], Container]], 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.

  • key_chains (Optional[Union[List[str], Dict[str, str], Container]], default: None) – The key-chains to apply or not apply the method to. Default is None.

  • to_apply (Union[bool, Container], default: True) – If True, the method will be applied to key_chains, otherwise key_chains will be skipped. Default is True.

  • prune_unapplied (Union[bool, Container], default: False) – Whether to prune key_chains for which the function was not applied. Default is False.

  • map_sequences (Union[bool, Container], default: False) – Whether to also map method to sequences (lists, tuples). Default is False.

  • out (Optional[Container], default: None) – optional output container, for writing the result to. It must have a shape that the inputs broadcast to.

Return type:

Container

Returns:

ret – an output container having the same data type and shape as self and whose elements, relative to self, are reordered.

Examples

With one ivy.Container input:

>>> x = ivy.Container(a=ivy.array([-1, 0, 1]),
...                   b=ivy.array([2, 3, 4]))
>>> y = x.flip()
>>> print(y)
{
    a: ivy.array([1, 0, -1]),
    b: ivy.array([4, 3, 2])
}
>>> x = ivy.Container(a=ivy.array([-1, 0, 1]),
...                   b=ivy.array([2, 3, 4]))
>>> y = x.flip(axis=0)
>>> print(y)
{
    a: ivy.array([1, 0, -1]),
    b: ivy.array([4, 3, 2])
}