lazy.symbolic {lazy.symbolic}R Documentation

lazy.symbolic: Collection of some useful symbolic tools for lazy boys and girls

Description

See the description of the package lazy.symbolic2 here ( lazy.symbolic2 ) for the modified infix (binary) operators.

Semi Symbolic Matrix Operations

Examples of Symbolic Math follows:

Examples


# Define matrices and vectors
P=demomat(2,2,root="p")
Q=demomat(2,2,root="q")
R=demomat(3,3,root="r")
A=demomat(3,2,root="a")
B=demomat(3,2,root="b")
C=demomat(2,3,root="c")
v2=demomat(2,1,root="v", vec=1)
v3=demomat(3,1,root="v", vec=1)
one2=matrix(1,2,1) # numeric
one3=matrix(1,3,1) # numeric

# Define native R vectors
vec2=c("c1","c2")
vec3=c("c1","c2", "c3")



# transposition
t(A)

# trace
tr(P)

# diagonal elements
Diag(P)
diag(P)


# Binary Operators
# Note that symbolic operators have EQUAL precedence. Use ( ).

# matrix with a scalar
1 %p% A
2 %t% v2
3 %t% A
B %T% 4

# vector + or - vector
v2 %p% v2
v2 %m% v2

# matrix + or - matrix
A %p% B
A %m% B

# matrix x vector
A %T% v2
t(v3) %T% A

# matrix x vector
A %T% one2
t(one3) %T% A

# matrix x matrix
P %T% Q
Q %T% P

# matrix x matrix
A %T% C
C %T% A


# multiplying the colums of a matrix
A %T% Diag(P)

# multiplying the rows of a matrix
Diag(R) %T% A



# permutation of the columns
A %T% pmat(2,1,2)

# permutation of the rows
pmat(3,1,2) %T% A



# No precedence of multiplication over addition
P %p% (t(A)%T%B)
#  error
# P %p% t(A)%T%B



# orthogonal matrix
temp <- " cos(theta) -sin(theta)
          sin(theta) cos(theta)"
T <- cards(temp)
T <- matrix(unlist(T),2,2)
Expand(T,simplify=0)
tTT <- t(T) %T% T
Expand(tTT)
Eval(lv(),theta=0)
Eval(lv(),theta=0, check=0)


# planar rotation in 3D
temp <- "
   1         0            0
   0         cos(theta1) -sin(theta1)
   0         sin(theta1) cos(theta1)  "
Tx <- matrix(unlist(cards(temp)),3,3)
temp <- "
   cos(theta2)  0            -sin(theta2)
   0            1            0
   sin(theta2)  0           cos(theta2)  "
Ty <- matrix(unlist(cards(temp)),3,3)
temp <- "
   cos(theta3)  -sin(theta3) 0
   sin(theta3)  cos(theta3)    0
   0            0            1    "
Tz <- matrix(unlist(cards(temp)),3,3)

# rotation in 3D
T <- Tx %T% Ty %T% Tz
T <- Simplify(T)
Tn <- Eval(T, values="theta1=1; theta2=2; theta3=3", check=0)
# see if T is orthonormal
t(Tn) %*% Tn %//% fuzz
Tn %*% t(Tn) %//% fuzz




# transposition of the product of two matrices: t(PQ) == t(Q) t(P)
tPQ <- t( P%T%Q )
tQtP <- t(Q)%T%t(P)
Eval( tPQ%m%tQtP, P=demomat(2,2), Q=demomat(2,2))




# Hadamar product
A %t% B
v2 %t% v2

# Kronecker product
A %@% B
one2 %@% P
diag(3) %@% P
b_diag( P,P,P)


# R native vector product
vec3 %t% A
A %t% vec3
vec2 %t% A




# inverse matrix: 2 x 2
Pinv=Inv(P)
PinvP=Pinv %T% P
Eval(PinvP, P=demomat(2,2))

# inverse matrix: 3 x 3
Rinv=Inv(R)
RRinv=R %T% Rinv
Eval( RRinv, R=matrix(runif(3*3),3) ) # Cannot use demomat(3,3) here.

# inverse of a diagonal matrix
Inv(Diag(R))

# inverse of a lower triangular matrix: n x n
n <- 3
LT <- demomat(n,n,root="t", shape="lowert")
LTn <- demomat(n,n)
invLT <- Inv(LT, simplify=0)
invLT <- Expand(invLT)
LTinvLT <- LT%T%invLT
Simplify(Expand(LTinvLT))
Eval(LTinvLT,LT=LTn)


# inverse of the transposed matrix: Inv(t(P)) = t(Inv(P))
invtP <- Inv(t(P))
tinvP <- t(Inv(P))
Print(invtP,"\n",tinvP)
Eval(invtP%m%tinvP, P=demomat(2,2), Q=demomat(2,2))

# inverse of the product of two matrices: Inv(PQ) == Inv(Q) Inv(P)
invPQ <- Inv(P%T%Q)
invQinvP <- Inv(Q)%T%Inv(P)
dif <- invPQ%m%invQinvP
Eval(dif, P=demomat(2,2), Q=demomat(2,2))


# determinant
Det(P)
Expand(lv())

# log determinant
Det(P,log=1)

# checing..
dd <- Det(R)
Rn <- matrix(runif(3*3),3)
Print( Eval(dd,R=Rn),det(Rn) )

Expand(dd); Expand(lv()); Expand(lv()); Expand(lv())
(ddd <- Simplify(lv()))
Print( Eval(ddd,R=Rn),det(Rn) )



# sum of all the element
t(one3) %T% A %T% one2
Sum(A)

# column sum
t(one3) %T% A
apply( A, 2, Sum)

# row sum
A %T% one2
apply( A, 1, Sum)



# centering operator
J <- diag(3) %m% ("(1/3)"%t%one3%T%t(one3))

# centering of a matrix
J %T% A
Expand(lv())
apply( Eval( J%T%A, A=matrix(runif(3*2),3) ), 2, sum )



# sum of squared elements of a vector
t(v2) %T% v2
Sum( v2 %t% v2 )


# sum of squared elements of a matrix
tr(t(A) %T% A)
tr(A %T% t(A))
Sum(A %t% A)



# vectorization of a matrix
vec(R)


# vec and Kronecker product
# vec( X %T% Y %T% Z)  ==  ( t(Z) %@% X ) %T% vec(Y)
X=demomat(2,2,root="x"); Y=demomat(2,2,root="y"); Z=demomat(2,2,root="z")
Xn=demomat(2,2); Yn=demomat(2,2); Zn=demomat(2,2)
vecXYZ <- vec(X%T%Y%T%Z)
vecXYZ <- Expand( vecXYZ )
tZKXvY <- (t(Z)%@%X) %T% vec(Y)
Print(vecXYZ,"\n",tZKXvY)
dif <- vecXYZ %m% tZKXvY
Eval(dif, X=Xn, Y=Yn, Z=Zn)


# trace, vec and Kronecker product: tr(XYZ) == t(vec(t(X)) I%@%Y vec(Z)
trXYZ <- tr( X%T%Y%T%Z )
trXYZ <- Expand(trXYZ)
vXetc <- t(vec(t(X))) %T% (diag(2)%@%Y) %T% vec(Z)
vXetc <- Expand(vXetc)
dif <- trXYZ %m% vXetc



# vectorization of the lower half of a matrix
vech(R)
vechinv(lv())
vech(R,1)
vechinv(lv(),1)





# matrix to summation
# (Upper case is a Matrix, lower case is a vector.)
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 )#'
mat2sum( "t(A)%*%t(B)%*%C - D%*%t(E)" )
mat2sum( "A%*%diag(B)%*%C - diag(D)%*%E%*%diag(F)%*%G + diag(H)" )

# summation operations
exp <- "s( a[i]+b[i] - s( c[i,j]-d[i,j], {j} ), {i})"
sumExpand( exp, all=1 )

exp <- "s( a[i]+b[i] - s( c[i,j]-d[i,j], {j,1,nj} ), {i,1,ni})"
sumEval( exp, values="nj=2; ni=3")

sumMoveIn( "s( s( a[i,j]*b[j,k]*c[k,l], {k}) , {j})" )

exp="s( x + s( s( a[i,j]*b[j,k]*c[k,l], {k}) - y, {j}),  {l})"
sumMoveIn( exp, all=1 )

sumSimplify( exp )



[Package lazy.symbolic version 0.1.3 Index]