mindspore.mint.triu
- mindspore.mint.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
- Returns
Tensor, a tensor has the same shape and data type as input.
- Raises
TypeError – If diagonal is not an int.
TypeError – If input is not a Tensor.
ValueError – If the dimension of input is less than 2.
- Supported Platforms:
Ascend
Examples
>>> import numpy as np >>> from mindspore import Tensor, mint >>> x = Tensor(np.array([[ 1, 2, 3, 4], ... [ 5, 6, 7, 8], ... [10, 11, 12, 13], ... [14, 15, 16, 17]])) >>> result = mint.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 = mint.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 = mint.triu(x, diagonal=-1) >>> print(result) [[ 1 2 3 4] [ 5 6 7 8] [ 0 11 12 13] [ 0 0 16 17]]