diag#
- ivy.diag(x, /, *, k=0, out=None)[source]#
Return the specified diagonals of the input array, or an array with the input array’s elements as diagonals.
- Parameters:
x (
Union
[Array
,NativeArray
]) – An array with rank >= 1.k (
int
, default:0
) – An integer that controls which diagonal to consider. Positive value means superdiagonal, 0 refers to the main diagonal, and negative value means subdiagonal.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:
- Returns:
ret – If x is a 1-D array, the function returns a 2-D square array with the elements of input as diagonals. If x is a 2-D array, the function returns a 1-D array with the diagonal elements of 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([[0, 1, 2], >>> [3, 4, 5], >>> [6, 7, 8]]) >>> ivy.diag(x) ivy.array([0, 4, 8])
>>> x = ivy.array([[0, 1, 2], >>> [3, 4, 5], >>> [6, 7, 8]]) >>> ivy.diag(x, k=1) ivy.array([1, 5])
>>> x = ivy.array([[0, 1, 2], >>> [3, 4, 5], >>> [6, 7, 8]]) >>> ivy.diag(x, k=-1) ivy.array([3, 7])
>>> x = ivy.array([[0, 1, 2], >>> [3, 4, 5], >>> [6, 7, 8]]) >>> ivy.diag(ivy.diag(x)) ivy.array([[0, 0, 0], [0, 4, 0], [0, 0, 8]])
- Array.diag(self, /, *, k=0, out=None)[source]#
ivy.Array instance method variant of ivy.diag. This method simply wraps the function, and so the docstring for ivy.diag also applies to this method with minimal changes.
- Return type:
Array
Examples
>>> x = ivy.array([[0, 1, 2], >>> [3, 4, 5], >>> [6, 7, 8]]) >>> x.diag(k=1) ivy.array([1, 5])
- Container.diag(self, /, *, k=0, key_chains=None, to_apply=True, prune_unapplied=False, map_sequences=False, out=None)[source]#
ivy.Container instance method variant of ivy.diag. This method simply wraps the function, and so the docstring for ivy.diag also applies to this method with minimal changes.
- Return type:
Container
Examples
>>> x = ivy.Container(a=[[0, 1, 2], >>> [3, 4, 5], >>> [6, 7, 8]]) >>> ivy.diag(x, k=1) { a: ivy.array([1, 5]) }