diagmul.RdThis is a convenience function for scaling elements of a tensor with different numbers based on their position in the tensor.
diagmul.tensor(X,i=names(D),D,j=i,by=NULL)The tensor to be scaled
A tensor containing scaling constants
numeric of character vector giving the dimensions of X to be used for the product.
numeric of character vector giving the dimensions of D to be used for the product.
Every operation is parallel for all levels of by in X and/or D.
A tensor with the shape and dimensions as X with entries \(X_{ik}\) scaled by \(D_im\), where \(i\) and \(k\) can represent multi-indices.
Let $$X_{i_1\ldots i_d k_1 \ldots k_d}$$ and $$D_{j_1\ldots j_d}$$ than the result is: $$E_{i_1\ldots i_d k_1 \ldots k_d}=X_{i_1\ldots i_d k_1 \ldots k_d}D_{j_1\ldots j_d}$$
(A <- matrix(rep(1:3,each=3),nrow=3))
#> [,1] [,2] [,3]
#> [1,] 1 2 3
#> [2,] 1 2 3
#> [3,] 1 2 3
(b <- to.tensor(c(1,1/2,1/3)))
#> [1] 1.0000000 0.5000000 0.3333333
#> attr(,"class")
#> [1] "tensor"
diagmul.tensor(as.tensor(A),2,as.tensor(c(1,1/2,1/3)),1)
#> I2
#> I1 [,1] [,2] [,3]
#> [1,] 1 1 1
#> [2,] 1 1 1
#> [3,] 1 1 1
#> attr(,"class")
#> [1] "tensor" "matrix"
diagmul.tensor(as.tensor(A),1,as.tensor(c(1,1/2,1/3)),1)
#> I1
#> I2 [,1] [,2] [,3]
#> [1,] 1 0.5 0.3333333
#> [2,] 2 1.0 0.6666667
#> [3,] 3 1.5 1.0000000
#> attr(,"class")
#> [1] "tensor" "matrix"
A %*% diag(b)
#> [,1] [,2] [,3]
#> [1,] 1 1 1
#> [2,] 1 1 1
#> [3,] 1 1 1
diag(b) %*% A
#> [,1] [,2] [,3]
#> [1,] 1.0000000 2.0000000 3.0
#> [2,] 0.5000000 1.0000000 1.5
#> [3,] 0.3333333 0.6666667 1.0