mat2sum {lazy.symbolic} | R Documentation |
Express the i-j element of an Matrix Expression using Summation Operators
Description
Japanese help file: mat2sum_JPH
Usage
mat2sum(
expr,
row = "i",
col = "j",
root = "s",
sublist = NA,
simple = 1,
expand = 1,
lcasvec = 1,
print = 0,
debug = 0
)
Arguments
expr |
An full symbplic matrix expression |
row |
row subscript name (one alphabet character) |
col |
column subscript name (one alphabet character) |
root |
root of the intermediate subscripts |
sublist |
a character vector containing the intermediate subscript names |
simple |
= 0 to use full spec: subscript, from , to |
expand |
= 0 not to expand (A+B) or (A*B) etc. |
lcasvec |
= 0 not to regard lowercase objects as vectors |
print |
= 1 to print the result |
debug |
= 1 to print the intermediate result |
Details
The summation of an expression over subscript k is expressed as
s( expr, {k} )
or
s( expr, {k, from, to} )
The lowercase object is treated as a column vector. (See examples below.)
When row="i" and col="j":
mat2sum( "A %*% B" ) -> s(A[i,s1]*B[s1,j], {s1}) mat2sum( "A %*% B", root="z" ) -> s(A[ij,z1]*B[z1,j], {z1}) mat2sum( "A %*% B", simple=0 ) -> s(A[i,s1]*B[s1,j], {s1,1,ncol(A)}) mat2sum( "t(A) %*% B" ) -> s(A[s1,i]*B[s1,j], {s1}) mat2sum( "A %*% t(B)" ) -> s(A[i,s1]*B[j,s1], {s1}) mat2sum( "A %*%Diag(B)%*%C" ) -> s(A[i,s1]*B[s1,s1]*C[s1,j],{s1}) mat2sum( "A %*% b" ) -> s(A[i,s1]*b[s1],{s1}) mat2sum( "t(a) %*% B" ) -> s(a[s1]*B[s1,j],{s1})
Identity matrix, I, and vec function are not recognized.
Diag or diag function will be recognized.
Kronecker products will not be converted.
Hadamar product may not be handled correctly.
Expressions in parentheses will not be converted.
The resulting summation expression can be expanded and evaluated by sumEval function.
Do NOT try to convert complex expressions!!
Examples
mat2sum( "A %*% B" )
mat2sum( "A %*% B", root="z" )
mat2sum( "A %*% B", simple=0 )
mat2sum( "t(A) %*% B" )
mat2sum( "A %*% t(B)" )
mat2sum( "A %*%Diag(B)%*%C" )
mat2sum( "A %*% b" )
mat2sum( "t(a) %*% B" )
mat2sum( "a %*% t(b)" )
mat2sum( "A%*%(B+C)" )
mat2sum( "A%*%(B+C)", expand=0 )
expr="t(A)%*%t(B)%*%C - D%*%t(E)"
mat2sum( expr )
mat2sum( expr, root="k" )
mat2sum( expr, sublist=letters[11:20] )
expr="A%*%diag(B)%*%C - diag(D)%*%E%*%diag(F)%*%G + diag(H)"
mat2sum( expr )
sexp0=mat2sum( "A%*%(B+C)", expand=0 )
sexp=mat2sum( "A%*%(B+C)" )
sexpe=Expand(c(sexp0,sexp))
printm(sexp0,sexp,sexpe)
mat2sum("t(y-yh)%*%diag(W)%*%(y-yh)", sublist="i")
mat2sum("t(y-yh)%*%diag(W)%*%(y-yh)", sublist="i", lcasvec=0)
mat2sum("t(y-yh)%*%W%*%(y-yh)", sublist=c("i","j"))
mat2sum("t(y-yh)%*%W%*%(y-yh)", sublist=c("i","j"), lcasvec=0)