triu(x: array, /, *, k: int = 0) array

Returns the upper triangular part of a matrix (or a stack of matrices) x.


The upper triangular part of the matrix is defined as the elements on and above the specified diagonal k.

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

  • k (int) –

    diagonal below which to zero elements. If k = 0, the diagonal is the main diagonal. If k < 0, the diagonal is below the main diagonal. If k > 0, the diagonal is above the main diagonal. Default: 0.


    The main diagonal is defined as the set of indices {(i, i)} for i on the interval [0, min(M, N) - 1].


out (array) – an array containing the upper triangular part(s). The returned array must have the same shape and data type as x. All elements below the specified diagonal k must be zeroed. The returned array should be allocated on the same device as x.