The function removes dimensions from a tensor by summing all entries which only differ in these dimensions.

margin.tensor(X,i=NULL,by=NULL)

Arguments

X

the tensor

i

the dimensions to be removed

by

instead of i the dimensions to be kept

Value

The tensor with all elements only differing only in the dimensions specified added up and only the other dimensions left over.

Details

This is a tensor multiplication with the \(1_i\) tensor.

See also

Author

K. Gerald van den Boogaart

Examples

A <- diag(1:5)
A
#>      [,1] [,2] [,3] [,4] [,5]
#> [1,]    1    0    0    0    0
#> [2,]    0    2    0    0    0
#> [3,]    0    0    3    0    0
#> [4,]    0    0    0    4    0
#> [5,]    0    0    0    0    5
margin.tensor(A,1)
#> [1] 1 2 3 4 5
#> attr(,"class")
#> [1] "tensor"

A <- to.tensor(1:30,dim=c(i=3,j=5,k=2))
ftable(A)
#>       k k1 k2
#> i  j         
#> i1 j1    1 16
#>    j2    4 19
#>    j3    7 22
#>    j4   10 25
#>    j5   13 28
#> i2 j1    2 17
#>    j2    5 20
#>    j3    8 23
#>    j4   11 26
#>    j5   14 29
#> i3 j1    3 18
#>    j2    6 21
#>    j3    9 24
#>    j4   12 27
#>    j5   15 30
margin.tensor(A,"j")
#>       k
#> i      [,1] [,2]
#>   [1,]   35  110
#>   [2,]   40  115
#>   [3,]   45  120
#> attr(,"class")
#> [1] "tensor" "matrix"