lazy.symbolic2 {lazy.symbolic2}R Documentation

lazy.symbolic2: Collection of some useful infix operators for lazy boys and girls

Details

This package provides the following infix (binary) operators which are the modifed version of the corresponding R native operators.

Type detach("package:lazy.symbolic2") to remove this package.

See the description of the package lazy.symbolic here. lazy.symbolic

Examples of Symbolic Math follows:

Examples

# Define matrices and vectors
set.seed(1701+1701)
Pn=matrix(round(100*runif(4)),2,2)
Qn=matrix(round(100*runif(4)),2,2)
Rn=matrix(round(100*runif(9)),3,3)
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


# numeric objects
(vn <- 1+2-(1:2)*(2:3))
vn + Pn%*%Qn



# matrix with a scalar
1 + A
2 - v2
3 * A
B %*% 4

# vector + or - vector
v2 + v2
v2 - v2

# matrix + or - matrix
A + B
A - B

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

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


# matrix x matrix
P %*% Q
Q %*% P

# matrix x matrix
A %*% C
C %*% A


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

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



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

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



# 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)
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%*%Ty%*%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



# Precedence of multiplication over addition
P + t(Q)%*%Q

# Not with \%p\%, \%m\%, \%T\%, \%t\%
P %p% t(Q)%*%Q



# transposition of the product of two matrices: t(PQ) == t(Q) t(P)
tPQ <- t( P%*%Q )
tQtP <- t(Q)%*%t(P)
Eval( tPQ-tQtP, P=Pn, Q=Qn)




# Hadamar product
A * B
v2 * v2

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


# R native vector product
vec3 * A
A * vec3
vec2 * A






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

# inverse matrix: 3 x 3
Rinv=Inv(R)
RRinv=R %*% Rinv
Eval( RRinv, R=Rn ) # 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)
invLT <- Expand(invLT)
LTinvLT <- LT%*%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-tinvP, P=Pn, Q=Qn)

# inverse of the product of two matrices: Inv(PQ) == Inv(Q) Inv(P)
invPQ <- Inv(P%*%Q)
invQinvP <- Inv(Q)%*%Inv(P)
dif <- invPQ-invQinvP
Eval(dif, P=Pn, Q=Qn)


# determinant
Det(P)
lv() %//% Expand %//% Simplify

# 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) %*% A %*% one2
Sum(A)

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

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



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

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



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


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



# vectorization of a matrix
vec(R)


# vec and Kronecker product
# vec( X %*% Y %*% Z)  ==  ( t(Z) %@% X ) %*% 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%*%Y%*%Z)
vecXYZ <- Expand( vecXYZ )
tZKXvY <- (t(Z)%@%X) %*% vec(Y)
Print(vecXYZ,"\n",tZKXvY)
dif <- vecXYZ - 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%*%Y%*%Z )
trXYZ <- Expand(trXYZ)
vXetc <- t(vec(t(X))) %*% (diag(2)%@%Y) %*% vec(Z)
vXetc <- Expand(vXetc)
(dif <- trXYZ - vXetc)



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

[Package lazy.symbolic2 version 0.1.2 Index]