If input is a vector (1-D tensor), then returns a 2-D square tensor with the elements of input as the diagonal.

tch_diag(x, diagonal = 0)

Arguments

x

tensor object

diagonal

the diagonal to consider

Details

If input is a matrix (2-D tensor), then returns a 1-D tensor with the diagonal elements of input.

  • If diagonal = 0, it is the main diagonal.

  • If diagonal > 0, it is above the main diagonal.

  • If diagonal < 0, it is below the main diagonal.

See also

Examples

tch_diag(tensor(1:10))
#> tensor #> 1 0 0 0 0 0 0 0 0 0 #> 0 2 0 0 0 0 0 0 0 0 #> 0 0 3 0 0 0 0 0 0 0 #> 0 0 0 4 0 0 0 0 0 0 #> 0 0 0 0 5 0 0 0 0 0 #> 0 0 0 0 0 6 0 0 0 0 #> 0 0 0 0 0 0 7 0 0 0 #> 0 0 0 0 0 0 0 8 0 0 #> 0 0 0 0 0 0 0 0 9 0 #> 0 0 0 0 0 0 0 0 0 10 #> [ Variable[CPUIntType]{10,10} ]
tch_diag(tensor(matrix(4, 2, 2)))
#> tensor #> 4 #> 4 #> [ Variable[CPUFloatType]{2} ]