lazy.symbolic {lazy.symbolic} | R Documentation |
See the description of the package lazy.symbolic2 here
( lazy.symbolic2
)
for the modified infix (binary) operators.
Generation of Matrix or Matrix Expressions:
demomat: creation of demo matrix (in lazy.tools)
gen_random_expr: generation of random expressions
Basic Matrix Operators:
See lazy.symbolic2
for more natural ones.
Plus or %p%: add matrices
Minus or %m%: subtract matrices
Times or %t%: elementwise multiplication of vectors and matrices
matTimes or %T%: multiply matrices
matKTimes or %@%: Kronecker product
tp: transposition of a fully symbolic matrix
Matrix Inversion by Sweep Operator:
matSweep or Inv: sweep operator
Det: determinant
Functions for Numerical Evaluation of Symbolic Expressions:
matReplace: replace a pattern with new pattern
Eval: evaluation of an expression with replacements
Utility Functions:
Simplify: simplification of symbolic expression (very primitive!)
Expand: expand (distribute) expression
add_paren: add enclosing parentheses (in lazy.tools)
locbalpar: find the location of the matching parenthesis
analyze_expr: decompose an expression into terms and factors
nterms: count the # of terms
find_terms: decompose an expression into addirive terms
find_factors: decompose a term into multiplicative factors
find_varfunc: find the variables/functions in an expression
is_valid: check if the expression is valid or not
Mathematica-like Operators:
%//% Mathematical-like postfix application of a function (in lazy.tools)
%/.% Mathematical-like postfix replacement
Summation Operation Functions:
mat2sum
: convert a full symbolic matrix
expression to summation
sumEval: evaluate summation created by mat2sum
sumExpand: expand (distribute) summations
sumMoveIn: move summation to the right
sumMoveOut: move summation to the left
sumSimplify: simplify summation
Kronecker Product Functions:
KP: Symbolic expression of the i-j element of the Kronecker product of A and B
tr2vecK: Conversion of the trace of the matrix product
vec2vecK: Conversion of the vec of the matrix product
from lazy.tools:
demomat: Set up a matrix whose elements consist of row and col numbers
Print: print objects with their names and formats.
Printb: print super matrix (partitioned or block matrix) with separators.
from lazy.mat:
b_diag: Makes a Block Diagonal Matrix from the Arguments.
Diag: Creats a diagonam matrix from its argument vector or matrix.
pmat: Generate a permutation matrix to switch row or column
Kmat: Generate a commutation matrix to convert vec(A) to vec(t(A)).
repmat: Stack a matrix n times either horizontally or vertically.
ssq: Returns the doubly weighted sum of squares of all the elements of A.
tr: trace of the input matrix. (modified in lazy.symbolic)
transp: Transpose each matrix contained in a list/array/syupermatrix.
vec: Vectorization of a matrix.
vecdiag: Returns diagonal elements of A as a vector.
vech: Vectorization of the lower or upper half of a square matrix.
vechindex: Returns the index to convert vech(Mat) to Mat and vice versa.
vechinv: Recover the Symmetric Matrix from its lower or upper half elements stored in a vector created by vech function.
vecindex: Returns the index to convert vec(Mat) to vec(t(Mat)) and vice versa.
supermat: Declare a supermatrix with SMdim attribute.
getSM: Extract a submatrix from a supermatrix.
modSM<-: Assign values to the submatrix of a supermatrix.
Mh2Mv: Convert a horizontally stacked supermatrix to a vertially stacked one.
Mv2Mh: Convert a vertically stacked supermatrix to a horizontally stacked one.
# 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
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 %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
temp <- Det(P)
Expand(temp)
# log determinant
Det(P,log=1)
# checing..
dd <- Det(R)
Rn <- matrix(runif(3*3),3)
Print( Eval(dd,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()
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
( temp <- vech(R) )
vechinv(temp)
( temp <- vech(R,1) )
vechinv(temp,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 )