Skip to content

Commit

Permalink
Handles parentheses in simplification
Browse files Browse the repository at this point in the history
This addresses #5.
  • Loading branch information
mailund committed Feb 2, 2017
1 parent 080b44c commit 40c7b34
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
6 changes: 6 additions & 0 deletions R/simplify.R
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ simplify_call <- function(expr) {

if (expr[[1]] == as.name("^")) return(simplify_exponentiation(expr[[2]], expr[[3]]))

if (expr[[1]] == as.name("(")) {
subexpr <- simplify_expr(expr[[2]])
if (is.atomic(subexpr) || is.name(subexpr)) return(subexpr)
else return(call("(", subexpr))
}

if (as.character(expr[[1]]) %in% .simplify_built_in_functions) return(simplify_built_in_function(expr))

stop(paste0("Unexpected call ", deparse(expr)))
Expand Down
3 changes: 3 additions & 0 deletions tests/testthat/test-simpification.R
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ test_that("we can do some simplifications when there are variables involved", {
expect_equal(simplify_expr(quote(x / x)), quote(x / x)) # we probably want to simplify this...

expect_equal(simplify_expr(quote(x ^ y)), quote(x ^ y))

expect_equal(simplify_expr(quote(2*(x + 0))), quote(2*x))
expect_equal(simplify_expr(quote(2*(x + y))), quote(2*(x + y)))
})

test_that("we can handle some simple functions", {
Expand Down

0 comments on commit 40c7b34

Please sign in to comment.