Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse min/max/prod properly #72

Merged
merged 3 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions R/constants.R
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ FUNCTIONS <- list(
nrow = 1,
ncol = 1,
sum = 1,
prod = 1,
min = 1:2,
max = 1:2,
as.logical = 1,
as.integer = 1,
as.numeric = 1,
Expand Down
8 changes: 4 additions & 4 deletions R/generate_dust_sexp.R
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,9 @@ generate_dust_sexp_reduce <- function(expr, dat, options) {
dim <- paste0(
if (isFALSE(options$shared_exists)) "dim_" else "shared.dim.",
target)
stopifnot(fn == "sum")
stopifnot(fn %in% c("sum", "prod", "min", "max"))
if (is.null(index)) {
sprintf("dust2::array::sum<real_type>(%s, %s)", target_str, dim)
sprintf("dust2::array::%s<real_type>(%s, %s)", fn, target_str, dim)
} else {
index_str <- paste(vcapply(index, function(el) {
if (el$type == "single") {
Expand All @@ -217,7 +217,7 @@ generate_dust_sexp_reduce <- function(expr, dat, options) {
generate_dust_sexp(from, dat, options),
generate_dust_sexp(to, dat, options))
}), collapse = ", ")
sprintf("dust2::array::sum<real_type>(%s, %s, %s)",
target_str, dim, index_str)
sprintf("dust2::array::%s<real_type>(%s, %s, %s)",
fn, target_str, dim, index_str)
}
}
4 changes: 3 additions & 1 deletion R/parse_expr.R
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,9 @@ parse_expr_usage <- function(expr, src, call) {
}
fn_str <- as.character(fn)
ignore <- "["
if (fn_str == "sum") {
is_reduction <- fn_str %in% c("sum", "prod") ||
(fn_str %in% c("min", "max") && length(expr) == 2)
if (is_reduction) {
expr <- parse_expr_usage_rewrite_reduce(expr, src, call)
} else if (fn_str %in% monty::monty_dsl_distributions()$name) {
expr <- parse_expr_usage_rewrite_stochastic(expr, src, call)
Expand Down
18 changes: 18 additions & 0 deletions tests/testthat/test-generate.R
Original file line number Diff line number Diff line change
Expand Up @@ -1660,3 +1660,21 @@ test_that("Generate conditional debug", {
" }",
"}"))
})


test_that("support min/max", {
dat <- odin_parse({
update(x) <- min(a) + max(b, c)
initial(x) <- 0
a[] <- i
dim(a) <- 10
b <- 20
c <- 30
})
dat <- generate_prepare(dat)
expect_equal(
generate_dust_system_update(dat),
c(method_args$update,
" state_next[0] = dust2::array::min<real_type>(shared.a, shared.dim.a) + std::max(shared.b, shared.c);",
"}"))
})
9 changes: 9 additions & 0 deletions tests/testthat/test-parse-expr-array.R
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,15 @@ test_that("can parse call sum over part of part of array", {
})


test_that("parse prod as a reduction", {
res <- parse_expr(quote(a <- prod(x)), NULL, NULL)
expect_equal(res$rhs$expr,
quote(OdinReduce("prod", "x", index = NULL)))
expect_equal(res$rhs$depends,
list(functions = "prod", variables = "x"))
})


test_that("can check that sum has the right number of arguments", {
expect_error(
parse_expr(quote(a <- sum(x, 1)), NULL, NULL),
Expand Down
38 changes: 38 additions & 0 deletions tests/testthat/test-parse-expr.R
Original file line number Diff line number Diff line change
Expand Up @@ -490,3 +490,41 @@ test_that("prevent nested special calls", {
"Invalid nested special lhs function 'update' within 'initial'",
fixed = TRUE)
})


test_that("parse min as a reduction", {
res <- parse_expr(quote(a <- min(x)), NULL, NULL)
expect_equal(res$rhs$expr,
quote(OdinReduce("min", "x", index = NULL)))
expect_equal(res$rhs$depends,
list(functions = "min", variables = "x"))
})


test_that("parse min as a 2-arg function", {
res <- parse_expr(quote(a <- min(x, y)), NULL, NULL)
expect_equal(res$rhs$expr, quote(min(x, y)))
expect_equal(res$rhs$depends,
list(functions = "min", variables = c("x", "y")))

expect_error(
parse_expr(quote(a <- min(x, y, z)), NULL, NULL),
"Invalid call to 'min': incorrect number of arguments")
})


test_that("parse max as a reduction", {
res <- parse_expr(quote(a <- max(x)), NULL, NULL)
expect_equal(res$rhs$expr,
quote(OdinReduce("max", "x", index = NULL)))
expect_equal(res$rhs$depends,
list(functions = "max", variables = "x"))
})


test_that("parse max as a 2-arg function", {
res <- parse_expr(quote(a <- max(x, y)), NULL, NULL)
expect_equal(res$rhs$expr, quote(max(x, y)))
expect_equal(res$rhs$depends,
list(functions = "max", variables = c("x", "y")))
})
Loading