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.

Also, it provides a general transposition function t which can be used for numeric or symbolic matrices.

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

matReplace function in lazy.symbolic package is included here exactly as it is.

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)
tTT <- t(T%*%T)
Eval(tTT,theta=0)
Eval(tTT,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)

# block diagonal matrix and its inverse
A2 <- demomat(2,2,root="a")
B2 <- demomat(2,2,root="b")
( bigAB2 <- b_diag(A2,B2) )
( invbigAB2 <- Inv(bigAB2) )
( invBigAB2 <- b_diag(Inv(A2),Inv(B2)) )
invBigAB2-invbigAB2
# Eval(invbigAB2-invBigAB2, A2=demomat(2,2),B=demomat(2,2))


# determinant
( temp <- Det(P) )
temp |>  Expand(simplify=0) |> Simplify()

# log determinant
Det(P,log=1)

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

( ddd <- Expand(dd) |> Expand() |> Simplify() )
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
( temp <- J %*% A )
Expand(temp)
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
( temp <- vech(R) )
vechinv(temp)
( temp <- vech(R,1) )
vechinv(temp,1)




# Partitioned Matrix


# Fully Symbolic Partitioned Matrix
# 2 x 2 partitioned matrices
Ap=demomat(2,2,root="A" ,fullsymb=1)
Bp=demomat(2,2,root="B" ,fullsymb=1)

Print(Ap,Bp)


# Semi Symbolic Partitioned Matrix
A11=demomat(2,2,root="a11_"); A12=demomat(2,3,root="a12_")
A21=demomat(3,2,root="a21_"); A22=demomat(3,3,root="a22_")
B11=demomat(2,2,root="b11_"); B12=demomat(2,3,root="b12_")
B21=demomat(3,2,root="b21_"); B22=demomat(3,3,root="b22_")
bigA=rbind( cbind(A11,A12), cbind(A21,A22) )
# attributes(bigA)$SMdim=list(nrows=c(2,3), ncols=c(2,3))
 bigB=rbind( cbind(B11,B12), cbind(B21,B22) )
# attributes(bigB)$SMdim=list(nrows=c(2,3), ncols=c(2,3))
Printb(bigA, sep="  ")


# semi-symbolic checking
bigAp=matReplace( Ap, A11=A11, A12=A12, A21=A21, A22=A22)
Print(bigA,bigAp)


# transpose
( tAp=t(Ap) )

# semi-symbolic checking
bigtAp=matReplace( tAp, A11=A11, A12=A12, A21=A21, A22=A22)
Print(t(bigA),bigtAp)


# addition and subtraction
Ap+Bp
Ap-Bp

# semi-symbolic checking
vlist="A11=A11; A12=A12; A21=A21; A22=A22; B11=B11; B12=B12; B21=B21; B22=B22"
matReplace(Ap+Bp, values=vlist)
bigA+bigB

# multiplication
Ap %*% Bp

# semi-symbolic checking
matReplace(Ap%*%Bp, values=vlist) - bigA%*%bigB


# diagonaization
Diag(Ap)
Diag( map("Diag", diag(Ap)) )


# block triangular matrix
( bigL <- demomat(3,3,root="A", shape="lowert", fullsymb=1) )
# inverse of block triangular matrix
( invbigL <- matSweep(bigL) )

# full-symbolic checking
Expand(Simplify( bigL%*%invbigL, all=1 ), all=1 )
Expand(Simplify( invbigL%*%bigL, all=1 ), all=1 )



# invserse by matSweep (Inv)
( invAp1=Inv(Ap) )
( invAp2=Inv(Ap, 2:1) )

# full-symbolic checking
( iAA <- Simplify(Expand(invAp1 %*% Ap, all=1),all=1) )
( AiA <- Simplify(Expand(Ap %*% invAp1, all=1),all=1) )

# pick up the 12 element of inv(Ap1)%+%Ap
iAA12 <- iAA[1,2]
analyze_expr( iAA12 )

# Let A22.1=A22-A21%*%inv(A11)%*%A12 and simplify visually.
iAA12s <- gsub("A22-A21%*%inv(A11)%*%A12","A22.1", iAA12, fixed=1 )
analyze_expr( iAA12s )

# pick up the 22 element of inv(Ap1)%+%Ap
iAA22 <- iAA[2,2]
analyze_expr( iAA22 )

# Let A22.1=A22-A21%*%inv(A11)%*%A12 and simplify visually.
iAA22s <- gsub("A22-A21%*%inv(A11)%*%A12","A22.1", iAA22, fixed=1 )
analyze_expr( iAA22s )




[Package lazy.symbolic2 version 0.1.4 ]