sequencing.RdIn typical tensor notation the indices are not identified by names but by positions. The operators allow to identify names and positions transparently during calculation.
## Methods for class tensor
# x $ y
# x ^ y
# x | y
renamefirst.tensor(x,y)A tensor
Typically a character vector specifying a sequence of names for the
tensor. The names can be specified in various ways:
The following specifications are equal and specify a sequence of the
names i,j and k:x$ijk, x$i.j.k, i.j.k., x"$ijk",
x^"i.j.k", x^c("i","j","k"),x^c("i.j","k"),
x^c("$i.j","k"),x^c("$ij","k"),
x^c("$","ijk")
In general names are separated by dots.
All notations with \$ either as operator or as the first
character of the first string allow to omit the dots assuming that
all names are single character. If any dot is present all dots must
be given. The difference of \$ and \^ is that the
first accepts a name and the second an character valued expression.
Multi letter indices like "alpha","beta","gamma"
can only be given in the dot-free version of the notation
making the following
specifications equal:
x$alpha.beta.gamma, alpha.beta.gamma.,
x^"$alpha.beta.gamma",
x^"alpha.beta.gamma", x^c("alpha","beta","gamma"),
x^c("alpha.beta","gamma"),
x^c("$alpha.beta","k"),
x^c("$","alpha.beta.gammak")
The specification for | is equal to that for ^.
A tensor of the same shape as x but with reordered dimensions (for
|) or renamed dimensions (for the others)
These functions are used to mimic the mathematical notation in tensor
analysis.
Formulae of the form (with Einstein convention):
$$E_{ijk}= A_{ihl}C_{hj}C_{lk}$$
with defined tensors \(A_{ijk}\) and \(C_{ij}\) can
be given the
simple
form E <- A$ihl %e% C$hj %e% C$lk |"$ijk"
or alternatively for multi letter names: E <- A$i.h.l %e% C$h.j %e% C$l.k |"i.j.k"
or more flexible in computation with arguments I,J,K: E <- A^c(I,"h.l") %e% C^c("h",J) %e% C^c("l",K) | c(I,J,K)
The $ or ^ binds to the tensors with high precedence
and renames the first elements. The | binds with very low
precedence and reorders the tensor according to the
assumed index sequence of the result afterwards.
reorder.tensor, names<-.tensor, [[.tensor
A <- to.tensor(1:20,c(i=5,j=2,k=2))
C <- to.tensor(1:4,c(i=2,j=2))
E <- A$ihl %e% C$hj %e% C$lk |"$ijk"
E
#> , , 1
#>
#> j
#> i [,1] [,2]
#> [1,] 99 221
#> [2,] 108 242
#> [3,] 117 263
#> [4,] 126 284
#> [5,] 135 305
#>
#> , , 2
#>
#> j
#> i [,1] [,2]
#> [1,] 211 469
#> [2,] 232 518
#> [3,] 253 567
#> [4,] 274 616
#> [5,] 295 665
#>
#> attr(,"class")
#> [1] "tensor"
# Same as:
E2 <- reorder.tensor(A[[j=~h,k=~l]] %e% C[[i=~h]] %e% C[[i=~l,j=~k]],c("i","j","k"))
E-E2
#> , , 1
#>
#> j
#> i [,1] [,2]
#> [1,] 0 0
#> [2,] 0 0
#> [3,] 0 0
#> [4,] 0 0
#> [5,] 0 0
#>
#> , , 2
#>
#> j
#> i [,1] [,2]
#> [1,] 0 0
#> [2,] 0 0
#> [3,] 0 0
#> [4,] 0 0
#> [5,] 0 0
#>
#> attr(,"class")
#> [1] "tensor"
E <- A$i.h.l %e% C$h.j %e% C$l.k |"i.j.k"
E
#> , , 1
#>
#> j
#> i [,1] [,2]
#> [1,] 99 221
#> [2,] 108 242
#> [3,] 117 263
#> [4,] 126 284
#> [5,] 135 305
#>
#> , , 2
#>
#> j
#> i [,1] [,2]
#> [1,] 211 469
#> [2,] 232 518
#> [3,] 253 567
#> [4,] 274 616
#> [5,] 295 665
#>
#> attr(,"class")
#> [1] "tensor"
E-E2
#> , , 1
#>
#> j
#> i [,1] [,2]
#> [1,] 0 0
#> [2,] 0 0
#> [3,] 0 0
#> [4,] 0 0
#> [5,] 0 0
#>
#> , , 2
#>
#> j
#> i [,1] [,2]
#> [1,] 0 0
#> [2,] 0 0
#> [3,] 0 0
#> [4,] 0 0
#> [5,] 0 0
#>
#> attr(,"class")
#> [1] "tensor"
E <- A^"i.h.l" %e% C^"h.j" %e% C^"l.k" |"i.j.k"
E
#> , , 1
#>
#> j
#> i [,1] [,2]
#> [1,] 99 221
#> [2,] 108 242
#> [3,] 117 263
#> [4,] 126 284
#> [5,] 135 305
#>
#> , , 2
#>
#> j
#> i [,1] [,2]
#> [1,] 211 469
#> [2,] 232 518
#> [3,] 253 567
#> [4,] 274 616
#> [5,] 295 665
#>
#> attr(,"class")
#> [1] "tensor"
E-E2
#> , , 1
#>
#> j
#> i [,1] [,2]
#> [1,] 0 0
#> [2,] 0 0
#> [3,] 0 0
#> [4,] 0 0
#> [5,] 0 0
#>
#> , , 2
#>
#> j
#> i [,1] [,2]
#> [1,] 0 0
#> [2,] 0 0
#> [3,] 0 0
#> [4,] 0 0
#> [5,] 0 0
#>
#> attr(,"class")
#> [1] "tensor"