remove_paren {lazy.tools}R Documentation

Remove Redundant Parentheses (very very primitive version)

Description

Remove Redundant Parentheses (very very primitive version)

Usage

remove_paren(exp, method = 1:9, loc = 0, debug = 0)

Arguments

exp

expression vector or matrix

method

1, 2 or 3
If exp contains any logical operator, method=1 will be used.

loc

the location of the paren to be removed.
Use with caution when the paren is preceded by - sign, or exp is a vector or matrix.

debug

= 1 to print intermediate result.

Details

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.

Value

A vector or matrix of the same size as the input

Examples

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)


[Package lazy.tools version 0.1.6 ]