addtensor.RdAdds/subs/multiplies/devides tensors element by element .
The luxury difference to a simple + is that we
do not need to consider the correct permutation of indices or rules
on implicit replication, since all of this is handled automatically.
add.tensor(X,Y,op="+",only=NULL)
## Methods for class tensor
# x + y
# x - y
# x * y
# x - yA tensor giving the element-wise operation X,Y. If some of the indices are missing in one of the tensors they are added by repetition.
The tensors are properly reordered such that dimensions of the same name are identified. If dimensions are missing in one of the tensors it is correspondingly repeated.
A <- to.tensor(1:20,c(U=2,V=2,W=5))
add.tensor(A,A)/2 -A
#> , , 1
#>
#> V
#> U [,1] [,2]
#> [1,] 0 0
#> [2,] 0 0
#>
#> , , 2
#>
#> V
#> U [,1] [,2]
#> [1,] 0 0
#> [2,] 0 0
#>
#> , , 3
#>
#> V
#> U [,1] [,2]
#> [1,] 0 0
#> [2,] 0 0
#>
#> , , 4
#>
#> V
#> U [,1] [,2]
#> [1,] 0 0
#> [2,] 0 0
#>
#> , , 5
#>
#> V
#> U [,1] [,2]
#> [1,] 0 0
#> [2,] 0 0
#>
#> attr(,"class")
#> [1] "tensor"
(A+A)/2
#> , , 1
#>
#> V
#> U [,1] [,2]
#> [1,] 1 3
#> [2,] 2 4
#>
#> , , 2
#>
#> V
#> U [,1] [,2]
#> [1,] 5 7
#> [2,] 6 8
#>
#> , , 3
#>
#> V
#> U [,1] [,2]
#> [1,] 9 11
#> [2,] 10 12
#>
#> , , 4
#>
#> V
#> U [,1] [,2]
#> [1,] 13 15
#> [2,] 14 16
#>
#> , , 5
#>
#> V
#> U [,1] [,2]
#> [1,] 17 19
#> [2,] 18 20
#>
#> attr(,"class")
#> [1] "tensor"
A/A
#> , , 1
#>
#> V
#> U [,1] [,2]
#> [1,] 1 1
#> [2,] 1 1
#>
#> , , 2
#>
#> V
#> U [,1] [,2]
#> [1,] 1 1
#> [2,] 1 1
#>
#> , , 3
#>
#> V
#> U [,1] [,2]
#> [1,] 1 1
#> [2,] 1 1
#>
#> , , 4
#>
#> V
#> U [,1] [,2]
#> [1,] 1 1
#> [2,] 1 1
#>
#> , , 5
#>
#> V
#> U [,1] [,2]
#> [1,] 1 1
#> [2,] 1 1
#>
#> attr(,"class")
#> [1] "tensor"
A * 1/A
#> , , 1
#>
#> V
#> U [,1] [,2]
#> [1,] 1 1
#> [2,] 1 1
#>
#> , , 2
#>
#> V
#> U [,1] [,2]
#> [1,] 1 1
#> [2,] 1 1
#>
#> , , 3
#>
#> V
#> U [,1] [,2]
#> [1,] 1 1
#> [2,] 1 1
#>
#> , , 4
#>
#> V
#> U [,1] [,2]
#> [1,] 1 1
#> [2,] 1 1
#>
#> , , 5
#>
#> V
#> U [,1] [,2]
#> [1,] 1 1
#> [2,] 1 1
#>
#> attr(,"class")
#> [1] "tensor"
norm.tensor(reorder.tensor(A,c(2,3,1)) - A)
#> [1] 0