The diagonal tensor is the tensor equivalent of the diagonal matrix.

diag.tensor(X,mark="'",dn=NULL,by=NULL)

Arguments

X

a tensor containing the diagonal entries.

mark

a character to be concatenated to the names of the row dimensions to get the column dimension names

dn

dimnames which are used twice

by

The diagonal tensor is created for each level of the indices in by.

Value

a tensor with dimension c(dim(X),mark(dim(X),mark))

Details

$$E_{i_1\ldots i_n j_1\ldots j_n}=\delta_{i_1j_1}\ldots\delta_{i_nj_n}$$

See also

Author

K. Gerald van den Boogaart

Examples

A <- to.tensor(1:4,c(a=2,b=2))
diag.tensor(A)
#> , , 1, 1
#> 
#>       b
#> a      [,1] [,2]
#>   [1,]    1    0
#>   [2,]    0    0
#> 
#> , , 2, 1
#> 
#>       b
#> a      [,1] [,2]
#>   [1,]    0    0
#>   [2,]    2    0
#> 
#> , , 1, 2
#> 
#>       b
#> a      [,1] [,2]
#>   [1,]    0    3
#>   [2,]    0    0
#> 
#> , , 2, 2
#> 
#>       b
#> a      [,1] [,2]
#>   [1,]    0    0
#>   [2,]    0    4
#> 
#> attr(,"class")
#> [1] "tensor"
diag.tensor(A,by="b")
#> , , 1
#> 
#>       a'
#> a      [,1] [,2]
#>   [1,]    1    0
#>   [2,]    0    2
#> 
#> , , 2
#> 
#>       a'
#> a      [,1] [,2]
#>   [1,]    3    0
#>   [2,]    0    4
#> 
#> attr(,"class")
#> [1] "tensor"