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.

  • out (Optional[Array]) – optional output array, for writing the result to. It must have a shape that the (default: None) 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.

Functional 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)#

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.

  • out (Optional[Array]) – optional output array, for writing the result to. It must have a (default: None) 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)#

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 (int) – First axis to be swapped.

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

  • out (Optional[Container]) – optional output array, for writing the result to. It must have a shape (default: None) 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]])
}