remove_paren {lazy.tools} | R Documentation |
Remove Redundant Parentheses (very very primitive version)
remove_paren(exp, method = 1:9, loc = 0, debug = 0)
exp |
expression vector or matrix |
method |
1, 2 or 3 |
loc |
the location of the paren to be removed. |
debug |
= 1 to print intermediate result. |
When loc > 0 is given, those parentheses located at the loc-th position
of the elements of exp will be removed.
No signs will be distributed:
remove_paren("-(a+b)",loc=2)
will result in -a+b
.
First, (+....) will be replaced by (...) .
When method = 1, a pair of parentheses which encloses a single term
w/o divisions will be removed.
When method = 2, a pair of parentheses which is not preceded by [*/-]
and not trailed by [*/] will be removed.
When method = 3, patterns like -(-a) or -(-(a)) will be processed.
Use Expand to distribute multiplicative factors.
method=2
may return wrong result:
remove_paren("-((a+b))", method=2)
becomes -a+b
.
A vector or matrix of the same size as the input
remove_paren( c("(x)", "-(x)", "(x+y)", "-(x+y)") )
remove_paren( c("a*(x)*b", "a*(x+y)*b", "(-x)*a", "-(x+y)*a") )
remove_paren( c("a%*%(x)%*%b", "a%*%(x+y)%*%b", "(-x)%*%a", "-(x+y)%*%a") )
remove_paren( c("a%@%(x)%*%b", "a%@%(x+y)%@%b", "(-x)%@%a", "-(x+y)%@%a") )
exp <- "((a)) - ((a)+f(b)) + (a-b) - (x-gg(y)) + (((z+1)))"
remove_paren( exp )
exp2 <- c(exp, "(a+b)*(c)-d+1")
remove_paren( exp2 )
( temp <- And(Or("a==A","b>0"),"c") )
remove_paren(temp)
remove_paren("!(!(a))")
remove_paren("!(!(a))",loc=4)
remove_paren("!( !(a) )",loc=9)