mindspore.ops.triu

View Source On Gitee
mindspore.ops.triu(input, diagonal=0)[source]

Returns the upper triangle part of ‘input’ (elements that contain the diagonal and below), and set the other elements to zeros.

Warning

This is an experimental API that is subject to change or deletion.

Parameters
  • input (Tensor) – The input tensor with shape \((M, N, *)\) where * means any number of additional dimensions.

  • diagonal (int, optional) – An optional attribute indicates the diagonal to consider, default: 0, indicating the main diagonal.

Returns

Tensor, a tensor has the same shape and data type as input.

Raises
Supported Platforms:

Ascend GPU CPU

Examples

>>> import numpy as np
>>> from mindspore import Tensor, ops
>>> x = Tensor(np.array([[ 1,  2,  3,  4],
...                      [ 5,  6,  7,  8],
...                      [10, 11, 12, 13],
...                      [14, 15, 16, 17]]))
>>> result = ops.triu(x)
>>> print(result)
[[ 1  2  3  4]
 [ 0  6  7  8]
 [ 0  0 12 13]
 [ 0  0  0 17]]
>>> x = Tensor(np.array([[ 1,  2,  3,  4],
...                      [ 5,  6,  7,  8],
...                      [10, 11, 12, 13],
...                      [14, 15, 16, 17]]))
>>> result = ops.triu(x, diagonal=1)
>>> print(result)
[[ 0  2  3  4]
 [ 0  0  7  8]
 [ 0  0  0 13]
 [ 0  0  0  0]]
>>> x = Tensor(np.array([[ 1,  2,  3,  4],
...                      [ 5,  6,  7,  8],
...                      [10, 11, 12, 13],
...                      [14, 15, 16, 17]]))
>>> result = ops.triu(x, diagonal=-1)
>>> print(result)
[[ 1  2  3  4]
 [ 5  6  7  8]
 [ 0 11 12 13]
 [ 0  0 16 17]]