swapaxes#

ivy.swapaxes(x, axis0, axis1, /, *, copy=None, out=None)[source]#

Interchange two axes of an array.

Parameters:
  • x (Union[Array, NativeArray]) – 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.

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([[0, 1, 2]])
>>> y = ivy.swapaxes(x, 0, 1)
>>> print(y)
ivy.array([[0],
           [1],
           [2]])
>>> x = ivy.array([[[0,1],[2,3]],[[4,5],[6,7]]])
>>> y = ivy.swapaxes(x, 0, 1)
>>> print(y)
ivy.array([[[0, 1],
            [4, 5]],
           [[2, 3],
            [6, 7]]])
>>> x = ivy.array([[[0,1],[2,3]],[[4,5],[6,7]]])
>>> y = ivy.swapaxes(x, 0, 2)
>>> print(y)
ivy.array([[[0, 4],
            [2, 6]],
           [[1, 5],
            [3, 7]]])
>>> x = ivy.array([[[0,1],[2,3]],[[4,5],[6,7]]])
>>> y = ivy.swapaxes(x, 1, 2)
>>> print(y)
ivy.array([[[0, 2],
            [1, 3]],
           [[4, 6],
            [5, 7]]])
>>> x = ivy.array([[0, 1, 2]])
>>> y = ivy.swapaxes(x, 0, 1)
>>> print(y)
ivy.array([[0],
           [1],
           [2]])

With ivy.Container input:

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

# 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.

Array.swapaxes(self, 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]]])
Container.swapaxes(self, axis0, axis1, /, *, copy=None, key_chains=None, to_apply=True, prune_unapplied=False, map_sequences=False, out=None)[source]#

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

Parameters:
  • self (Container) – Input container.

  • axis0 (Union[int, Container]) – First axis to be swapped.

  • axis1 (Union[int, Container]) – Second axis to be swapped.

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

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

Examples

>>> a = ivy.array([[1, 2, 3], [4, 5, 6]])
>>> b = ivy.array([[7, 8, 9], [10, 11, 12]])
>>> x = ivy.Container(a = a, b = b)
>>> y = x.swapaxes(0, 1)
>>> print(y)
{
    a: ivy.array([[1, 4],
                  [2, 5],
                  [3, 6]]),
    b: ivy.array([[7, 10],
                  [8, 11],
                  [9, 12]])
}