slicetensor.RdIndexing of tensors allows beside the ordinary selection of ranges of indices the renaming of indices. The functions are mainly here to keep the the tensor property of the results.
slice.tensor(X,i,what,drop=FALSE)
## Methods for class tensor
# X[...,drop=TRUE]
# X[...,drop=TRUE] <- value
# X[[...,drop=TRUE]]
# X[[...,drop=TRUE]] <- valueA tensor
an index given as number or character
levels of the index, a number or a character from dimnames
a boolean, if true, indices with only a single level are removed
arguments of the form name=indices, and for the
[[ ]] functions it also allowed to give names from the
corresponding dimnames name=c("a","b") to select indices by
names or name=~newname to rename dimensions, the first use
makes a usual array access in the given dimension, where [[ ]]
only supports a single index, while [ ] allows vectors. The
other type changes the names.
a new tensor with dimensions renamed or individual levels selected
The functions allow to rename dimensions and to take select a part of the tensor.
A <- to.tensor(1:20,c(A=2,B=2,C=5))
A[C=1]
#> [1] 1
#> attr(,"class")
#> [1] "tensor"
A[C=1:3]
#> [1] 1 2 3
#> attr(,"class")
#> [1] "tensor"
A[[B=~b]] # renaming dimensions
#> , , 1
#>
#> b
#> A [,1] [,2]
#> [1,] 1 3
#> [2,] 2 4
#>
#> , , 2
#>
#> b
#> A [,1] [,2]
#> [1,] 5 7
#> [2,] 6 8
#>
#> , , 3
#>
#> b
#> A [,1] [,2]
#> [1,] 9 11
#> [2,] 10 12
#>
#> , , 4
#>
#> b
#> A [,1] [,2]
#> [1,] 13 15
#> [2,] 14 16
#>
#> , , 5
#>
#> b
#> A [,1] [,2]
#> [1,] 17 19
#> [2,] 18 20
#>
#> attr(,"class")
#> [1] "tensor"
A[[B=~b,A=~aaa]]
#> , , 1
#>
#> b
#> aaa [,1] [,2]
#> [1,] 1 3
#> [2,] 2 4
#>
#> , , 2
#>
#> b
#> aaa [,1] [,2]
#> [1,] 5 7
#> [2,] 6 8
#>
#> , , 3
#>
#> b
#> aaa [,1] [,2]
#> [1,] 9 11
#> [2,] 10 12
#>
#> , , 4
#>
#> b
#> aaa [,1] [,2]
#> [1,] 13 15
#> [2,] 14 16
#>
#> , , 5
#>
#> b
#> aaa [,1] [,2]
#> [1,] 17 19
#> [2,] 18 20
#>
#> attr(,"class")
#> [1] "tensor"
A[[B=~b,A=~aaa,aaa=1]]
#> C
#> b [,1] [,2] [,3] [,4] [,5]
#> [1,] 1 5 9 13 17
#> [2,] 3 7 11 15 19
#> attr(,"class")
#> [1] "tensor"
A[[A=1,B=~gamma]][C=1:2]
#> [1] 1 3
#> attr(,"class")
#> [1] "tensor"
A
#> , , 1
#>
#> B
#> A [,1] [,2]
#> [1,] 1 3
#> [2,] 2 4
#>
#> , , 2
#>
#> B
#> A [,1] [,2]
#> [1,] 5 7
#> [2,] 6 8
#>
#> , , 3
#>
#> B
#> A [,1] [,2]
#> [1,] 9 11
#> [2,] 10 12
#>
#> , , 4
#>
#> B
#> A [,1] [,2]
#> [1,] 13 15
#> [2,] 14 16
#>
#> , , 5
#>
#> B
#> A [,1] [,2]
#> [1,] 17 19
#> [2,] 18 20
#>
#> attr(,"class")
#> [1] "tensor"