tr2vecK {lazy.symbolic} | R Documentation |
Conversion of the Trace of the Product of Several Matrices using vec operator and Kronecker Product #'
tr2vecK(A = "I", B = "I", C = "I", D = "I", method = 1)
A |
The first matrix or Identity matrix |
B |
The second matrix or Identity matrix |
C |
The third matrix or Identity matrix |
D |
The forth matrix or Identity matrix |
method |
= 1 or 2 |
This function converts tr(A%*%B%*%C%*%D)
.
If method=1 is given, the result is
t(vec(D)) %*% (A %@% t(C)) %*% vec(t(B))
.
If method=2 is given, the result is
t(vec(t(D))) %*% (t(C) %@% A ) %*% vec(B)
.
Note that the input matrices can be numeric, semi symbolic, or full symbolic.
When the number of input matrices is less than 4, by choosing the matrices to be set equal to I, various results can be obtained.
An expression as the result of conversion.
# general form with fully symbolic matrices
tr2vecK( "A", "B", "C", "D", method=1 )
tr2vecK( "A", "B", "C", "D", method=2 )
# simpler version
tr00 <- "tr(A%*%B)"
tr01 <- tr2vecK( "A", "B",,, method=1 )
tr02 <- tr2vecK( "A",, "B",, method=1 )
## Not run:
# checking the resut with semi symbolic matrices
A <- demomat(2,2,root="a")
B <- demomat(2,2,root="b")
dim(tr00) <- c(1,1); attributes(tr00)$fullsymb <- 1
dim(tr01) <- c(1,1); attributes(tr01)$fullsymb <- 1
dim(tr02) <- c(1,1); attributes(tr02)$fullsymb <- 1
matReplace(tr00,A=A,B=B,I=diag(2))
matReplace(tr01,A=A,B=B,I=diag(2))
matReplace(tr02,A=A,B=B,I=diag(2))
# with semi symbolic matrices
library(lazy.symbolic2)
C <- demomat(2,2,root="c")
D <- demomat(2,2,root="d")
tr1 <- tr(A%*%B%*%C%*%D)
tr1 <- Expand(tr1,all=1)
tr2 <- tr2vecK( A,B,C,D, method=1 )
tr2 <- Expand(tr2,all=1)
res1 <- analyze_expr(c(tr1))$tab
res2 <- analyze_expr(c(tr2))$tab
# reorder multiplicative factors
od <- order(res1[,"factor_1"], res1[,"factor_2"]
, res1[,"factor_3"],res1[,"factor_4"])
res1 <- res1[od,]
res11 <-
paste( apply(res1,1, function(x){paste(x,collapse="")} ), collapse=" ")
od <- order(res2[,"factor_2"], res2[,"factor_4"]
, res2[,"factor_3"], res2[,"factor_1"])
res2 <- res2[od, c("topers", "factor_2", "o2", "factor_4"
, "o2", "factor_3", "o2", "factor_1")]
res22 <-
paste( apply(res2,1, function(x){paste(x,collapse="")} ), collapse=" ")
Print(res11,res22)
identical(res11,res22)
# full version
tr3 <- tr2vecK( A,B,C,D, method=2 )
tr3 <- Expand(tr3,all=1)
res3 <- analyze_expr(tr3)$tab
od <- order(res3[,"factor_3"], res3[,"factor_4"]
, res3[,"factor_2"], res3[,"factor_1"])
res3 <- res3[od, c("topers", "factor_3", "o2", "factor_4"
, "o2", "factor_2", "o2", "factor_1")]
res33 <-
paste( apply(res3,1, function(x){paste(x,collapse="")} ), collapse=" ")
Print(res11,res33)
identical(res11,res33)
## End(Not run)