Simplify {lazy.symbolic}R Documentation

Simplify Symbolic Expressions (very primitive version)

Description

Simplify Symbolic Expressions (very primitive version)

Usage

Simplify(
  expr,
  all = 0,
  method = 1:3,
  func_level = 0,
  expandfuncarg = 1,
  markso = 1,
  verify = 1,
  debug = 0
)

Arguments

expr

Expression (vector or matrix)

all

= 1 to apply Simplify repeatedly.

method

How to simplify: default method is 1:3.
= 1 : varname*num -> num*varname
= 2 : processing 1s and 0s, a/a -> 1, etc w/o using find_terms
= 3 : a-a or -a+a -> 0 or 0*func(x) -> 0
= 4 : to print the result of analyze_expr( expr )

func_level

Level of function argument simplification
= 0 not to simplify function arguments
= 1 to simplify the top level function arguments
= 2 to simplify the 2nd level function arguments (the arguments of the functions in functions.)

expandfuncarg

= 0 not to expand the function argument.
= 1 to expand function argument with function name
= 2 to expand function argument (body)

markso

= 0 not to mark the special opeartors

verify

= 0 not to check the result by is_valid

debug

= 1 to print details

Details

This is a very primitive version.
This function applies some mathematical rules such as
0*a -> 0 or a*1 -> a described as regular expressions to an input expression.

In method=1 and 2, gsub function is used to replace the patterns such as 0*a or -((a)) with their simplified mathematically equivalent forms.
In method=3, input expression is decomposed into terms and the patterns such as func(a)*0 or X%*%inv(X) will be simplified.
Can handle fully symbolic matrix expressions.

Repeated use of this function may simplify the result further.
Simplify may work better if you Expand the expression prior to submission.

When called from remove_paren, use method=1:3.

Value

Simplified expression

Examples

expr="-((a))-b/b+0*c+d*0+1*e-e*2+f-3*f+0/(g+h) - (x-y)*(((z+1)))"
Simplify( expr, all=1 )
Expand(.Last.value, all=1)
Expand(expr, all=1)

( res <- matTimes( demomat(2,2,root="a"), demomat(2,1), simplify=0 ) )
Simplify(res)

Simplify(c("1*b","1*(b)","1*f(b)"))
Simplify(c("0*b","0*(b)","0*f(b)"))

dd <- Det(demomat(2,2,root="a"))
Simplify(Expand(dd))

expr1=c("-(X+A)%*%inv(X+A)%*%(X+A)%*%inv(X+A)"
   , "-(X+A1)%*%inv(X+A)%*%(X+A)%*%inv(X+A)")
Simplify(expr1)
Simplify(expr1, all=1)

( res <- Simplify( "a+b+2*a+2*b-3*a+3*b" ) )
Simplify(res)
# Sorry.

# semi symbolic matrix
A=demomat(2,2,root="a")
AinvA=matTimes( A, Inv(A) )
Simplify( AinvA, all=1 )
Simplify( Expand(AinvA, all=1), all=1 )
# Sorry, cannot simplify \code{AinvA[2,1]} and \code{AinvA[2,2]}.

# fully symbolic matrix
A=demomat(2,2,root="A", fullsymb=1)
A1=matSweep(A,1)
A11=matSweep(A1,1)
A11s=Simplify(A11)
Expand(A11s)



[Package lazy.symbolic version 0.1.4 ]