Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
richfitz committed Dec 18, 2024
1 parent 3557606 commit 00d45a7
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
2 changes: 1 addition & 1 deletion R/dsl-differentiate-expr.R
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ maths <- local({
fn == "[" ||
(length(except) > 0 && fn %in% except) ||
"unary_minus" %in% except && .is_unary_minus(x)
if (pass) {
if (fn != "if" && pass) {
return(x)
}
call("(", x)
Expand Down
21 changes: 10 additions & 11 deletions tests/testthat/test-dsl-differentiation.R
Original file line number Diff line number Diff line change
Expand Up @@ -454,22 +454,21 @@ test_that("can diferentiate basic trig functions", {


test_that("differentiate expressions with arrays", {
expect_equal(differentiate(quote(x[i] + y[i]), "x"), 1)
expect_equal(differentiate(quote(x[i] + y[i]), "z"), 0)
expect_equal(differentiate(quote(x[i]^2), "x"), quote(2 * x[i]))
expect_identical(differentiate(quote(x[i] + y[i]), "x"), 1)
expect_identical(differentiate(quote(x[i] + y[i]), "z"), 0)
expect_identical(differentiate(quote(x[i]^2), "x"), quote(2 * x[i]))

expect_equal(differentiate(quote((x[i] - x[i + 1])^2), "x"),
quote(2 * (x[i] - x[1 + i])))
expect_equal(differentiate(quote(x[i] - x[i + 1]), "x"), 1)

expect_equal(differentiate(quote(3 * (x[2] - x[i])), "x"),
quote(3 * ((if (2 == i) 1 else 0) - 1)))
expect_identical(differentiate(quote((x[i] - x[i + 1])^2), "x"),
quote(2 * (x[i] - x[1 + i])))
expect_identical(differentiate(quote(x[i] - x[i + 1]), "x"), 1)
expect_identical(differentiate(quote(3 * (x[i] - x[2])), "x"),
quote(3 * (1 - (if (2 == i) 1 else 0))))
})


test_that("differentiate expressions with arrays", {
expect_equal(differentiate(quote(sum(x)), "x"), 1)
expect_equal(differentiate(quote(sum(x)), "y"), 0)
expect_identical(differentiate(quote(sum(x)), "x"), 1)
expect_identical(differentiate(quote(sum(x)), "y"), 0)
})


Expand Down

0 comments on commit 00d45a7

Please sign in to comment.