Returns the upper triangular part of the matrix (2-D tensor) x, the other elements of the result tensor out are set to 0. The upper triangular part of the matrix is defined as the elements on and above the diagonal.

tch_triu(x, diagonal = 0)

Arguments

x

tensor object

diagonal

integer. the diagonal to consider

Details

The argument diagonal controls which diagonal to consider. If diagonal = 0, all elements on and above the main diagonal are retained. A positive value excludes just as many diagonals above the main diagonal, and similarly a negative value includes just as many diagonals below the main diagonal.

Examples

x <- array(1:20, c(4, 5)) tch_triu(tensor(x))
#> tensor #> 1 5 9 13 17 #> 0 6 10 14 18 #> 0 0 11 15 19 #> 0 0 0 16 20 #> [ Variable[CPUIntType]{4,5} ]
tch_triu(tensor(x), 1)
#> tensor #> 0 5 9 13 17 #> 0 0 10 14 18 #> 0 0 0 15 19 #> 0 0 0 0 20 #> [ Variable[CPUIntType]{4,5} ]
tch_triu(tensor(x), 2)
#> tensor #> 0 0 9 13 17 #> 0 0 0 14 18 #> 0 0 0 0 19 #> 0 0 0 0 0 #> [ Variable[CPUIntType]{4,5} ]
tch_triu(tensor(x), -1)
#> tensor #> 1 5 9 13 17 #> 2 6 10 14 18 #> 0 7 11 15 19 #> 0 0 12 16 20 #> [ Variable[CPUIntType]{4,5} ]
tch_triu(tensor(x), -2)
#> tensor #> 1 5 9 13 17 #> 2 6 10 14 18 #> 3 7 11 15 19 #> 0 8 12 16 20 #> [ Variable[CPUIntType]{4,5} ]