diagonal#

ivy.diagonal(x, /, *, offset=0, axis1=-2, axis2=-1, out=None)[source]#

Return the specified diagonals of a matrix (or a stack of matrices) x.

Parameters:
  • x (Union[Array, NativeArray]) – input array having shape (..., M, N) and whose innermost two dimensions form MxN matrices.

  • offset (int, default: 0) – offset specifying the off-diagonal relative to the main diagonal. - offset = 0: the main diagonal. - offset > 0: off-diagonal above the main diagonal. - offset < 0: off-diagonal below the main diagonal. Default: 0.

  • axis1 (int, default: -2) – axis to be used as the first axis of the 2-D sub-arrays from which the diagonals should be taken. Defaults to first axis (-2).

  • axis2 (int, default: -1) – axis to be used as the second axis of the 2-D sub-arrays from which the diagonals should be taken. Defaults to second axis (-1).

  • 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 diagonals and whose shape is determined by removing the last two dimensions and appending a dimension equal to the size of the resulting diagonals. The returned array must have the same data type as x.

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 inputs:

>>> x = ivy.array([[1., 2.],
...                [3., 4.]])
>>> d = ivy.diagonal(x)
>>> print(d)
ivy.array([1., 4.])
>>> x = ivy.array([[[1., 2.],
...                 [3., 4.]],
...                [[5., 6.],
...                 [7., 8.]]])
>>> d = ivy.diagonal(x)
>>> print(d)
ivy.array([[1., 4.],
           [5., 8.]])
>>> x = ivy.array([[1., 2.],
...                [3., 4.]])
>>> d = ivy.diagonal(x, offset=1)
>>> print(d)
ivy.array([2.])
>>> x = ivy.array([[0, 1, 2],
...                   [3, 4, 5],
...                   [6, 7, 8]])
>>> d = ivy.diagonal(x, offset=-1, axis1=0)
>>> print(d)
ivy.array([3, 7])
>>> x = ivy.array([[[ 0,  1,  2],
...                 [ 3,  4,  5],
...                 [ 6,  7,  8]],
...                [[ 9, 10, 11],
...                 [12, 13, 14],
...                 [15, 16, 17]],
...                [[18, 19, 20],
...                 [21, 22, 23],
...                 [24, 25, 26]]])
>>> d = ivy.diagonal(x, offset=1, axis1=-3)
>>> print(d)
ivy.array([[1, 11],
           [4, 14],
           [7, 17]])
>>> x = ivy.array([[[0, 1],
...                 [2, 3]],
...                [[4, 5],
...                 [6, 7]]])
>>> d = ivy.diagonal(x, offset=0, axis1=0, axis2=1)
>>> print(d)
ivy.array([[0, 6],
           [1, 7]])
>>> x = ivy.array([[[1., 2.],
...                 [3., 4.]],
...                [[5., 6.],
...                 [7., 8.]]])
>>> d = ivy.diagonal(x, offset=1, axis1=0, axis2=1)
>>> print(d)
ivy.array([[3.],
           [4.]])
>>> x = ivy.array([[1., 2.],
...                [3., 4.]])
>>> d = ivy.diagonal(x)
>>> print(d)
ivy.array([1., 4.])
>>> x = ivy.array([[[ 0,  1,  2],
...                 [ 3,  4,  5],
...                 [ 6,  7,  8]],
...                [[ 9, 10, 11],
...                 [12, 13, 14],
...                 [15, 16, 17]],
...                [[18, 19, 20],
...                 [21, 22, 23],
...                 [24, 25, 26]]])
>>> d = ivy.diagonal(x, offset=1, axis1=1, axis2=-1)
>>> print(d)
ivy.array([[ 1,  5],
           [10, 14],
           [19, 23]])
>>> x = ivy.array([[0, 1, 2],
...                [3, 4, 5],
...                [6, 7, 8]])
>>> d = ivy.diagonal(x)
>>> print(d)
ivy.array([0, 4, 8])

With ivy.Container inputs:

>>> x = ivy.Container(
...        a = ivy.array([[7, 1, 2],
...                       [1, 3, 5],
...                       [0, 7, 4]]),
...        b = ivy.array([[4, 3, 2],
...                       [1, 9, 5],
...                       [7, 0, 6]])
...    )
>>> d = ivy.diagonal(x)
>>> print(d)
{
    a: ivy.array([7, 3, 4]),
    b: ivy.array([4, 9, 6])
}
Array.diagonal(self, /, *, offset=0, axis1=-2, axis2=-1, out=None)[source]#

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

Parameters:
  • self (Array) – input array having shape (..., M, N) and whose innermost two dimensions form MxN matrices.

  • offset (int, default: 0) – offset specifying the off-diagonal relative to the main diagonal. - offset = 0: the main diagonal. - offset > 0: off-diagonal above the main diagonal. - offset < 0: off-diagonal below the main diagonal. Default: 0.

  • axis1 (int, default: -2) – axis to be used as the first axis of the 2-D sub-arrays from which the diagonals should be taken. Defaults to first axis (-2).

  • axis2 (int, default: -1) – axis to be used as the second axis of the 2-D sub-arrays from which the diagonals should be taken. Defaults to second axis (-1).

  • 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 diagonals and whose shape is determined by removing the last two dimensions and appending a dimension equal to the size of the resulting diagonals. The returned array must have the same data type as x.

Examples

With ivy.Array inputs:

>>> x = ivy.array([[1., 2.],
...                [3., 4.]])
>>> d = x.diagonal()
>>> print(d)
ivy.array([1., 4.])
>>> x = ivy.array([[[1., 2.],
...                 [3., 4.]],
...                [[5., 6.],
...                 [7., 8.]]])
>>> d = x.diagonal()
>>> print(d)
ivy.array([[1., 4.],
           [5., 8.]])
>>> x = ivy.array([[1., 2.],
...                [3., 4.]])
>>> d = x.diagonal(offset=1)
>>> print(d)
ivy.array([2.])
>>> x = ivy.array([[0, 1, 2],
...                [3, 4, 5],
...                [6, 7, 8]])
>>> d = x.diagonal(offset=-1, axis1=0)
>>> print(d)
ivy.array([3, 7])
Container.diagonal(self, /, *, offset=0, axis1=-2, axis2=-1, key_chains=None, to_apply=True, prune_unapplied=False, map_sequences=False, out=None)[source]#

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

Parameters:
  • self (Container) –

    input Container with leave arrays having shape

    (..., M, N) and whose innermost two dimensions form

    MxN matrices.

  • offset (Union[int, Container], default: 0) – offset specifying the off-diagonal relative to the main diagonal. - offset = 0: the main diagonal. - offset > 0: off-diagonal above the main diagonal. - offset < 0: off-diagonal below the main diagonal. Default: 0.

  • axis1 (Union[int, Container], default: -2) – axis to be used as the first axis of the 2-D sub-arrays from which the diagonals should be taken. Defaults to first axis (-2).

  • axis2 (Union[int, Container], default: -1) – axis to be used as the second axis of the 2-D sub-arrays from which the diagonals should be taken. Defaults to second axis (-1).

  • 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 – A container with the diagonals. More details can be found in the docstring for ivy.diagonal.

Examples

With ivy.Container inputs:

>>> x = ivy.Container(a=ivy.array([[1., 2.], [3., 4.]]),
...                   b=ivy.array([[5., 6.], [7., 8.]]))
>>> d = x.diagonal()
>>> print(d)
{
    a:ivy.array([1., 4.]),
    b:ivy.array([5., 8.])
}
>>> a = ivy.array([[0, 1, 2],
...                [3, 4, 5],
...                [6, 7, 8]])
>>> b = ivy.array([[-1., -2., -3.],
...                 [-3., 4., 5.],
...                 [5., 6., 7.]]),
>>> x = ivy.Container(a=a, b=b)
>>> d = x.diagonal(offset=-1)
>>> print(d)
{
    a: ivy.array([3, 7]),
    b: ivy.array([[-3., 6.]])
}