diff --git a/DESCRIPTION b/DESCRIPTION index 852c5ee..1eae26f 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: cppDE Title: 'C++' Code Generation for ODEs with Sensitivity Propagation -Version: 0.13.15 +Version: 0.13.16 Authors@R: c( person("Simon", "Beyer", , "simon.beyer@fdm.uni-freiburg.de", role = c("aut", "cre")), person("Lawrence Livermore National Security", role = "cph", diff --git a/NEWS.md b/NEWS.md index 1a694cf..0ba9969 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,8 @@ +# cppDE 0.13.16 + +* A product with a constant and a quotient by a constant have finite first and second derivatives + where the other operand is infinite: `exp10(x)` at `x = -Inf` gives 0, not NaN. + # cppDE 0.13.15 * The reverse and forward-reverse modes of `bdf` solve the corrector equation of every step in the diff --git a/inst/include/cppde/cppde_dual2nd_expr.hpp b/inst/include/cppde/cppde_dual2nd_expr.hpp index cf52107..c161d4e 100644 --- a/inst/include/cppde/cppde_dual2nd_expr.hpp +++ b/inst/include/cppde/cppde_dual2nd_expr.hpp @@ -119,6 +119,9 @@ struct ScalarLeaf2 : Expr2nd> { CPPDE_ET2_INLINE unsigned width() const { return 0u; } }; +template struct is_scalar_leaf2 : std::false_type {}; +template struct is_scalar_leaf2> : std::true_type {}; + // =========================================================================== // Op tags for binary arithmetic. Each provides static methods: // value(av, bv) -> y @@ -200,12 +203,19 @@ struct BinExpr2 : Expr2nd> { dep_(l_.any_depend() || r_.any_depend()) {} CPPDE_ET2_INLINE value_type eval_value() const { return y_; } + // a constant factor or divisor adds no derivative term, also where the other operand is infinite CPPDE_ET2_INLINE value_type eval_d1(unsigned i) const { - return Op::d1(l_.eval_value(), r_.eval_value(), - l_.eval_d1(i), r_.eval_d1(i), y_); + if constexpr (std::is_same_v && is_scalar_leaf2::value) return l_.eval_value() * r_.eval_d1(i); + else if constexpr (std::is_same_v && is_scalar_leaf2::value) return l_.eval_d1(i) * r_.eval_value(); + else if constexpr (std::is_same_v && is_scalar_leaf2::value) return l_.eval_d1(i) / r_.eval_value(); + else return Op::d1(l_.eval_value(), r_.eval_value(), + l_.eval_d1(i), r_.eval_d1(i), y_); } CPPDE_ET2_INLINE value_type eval_d2(unsigned i, unsigned j) const { - return Op::d2(l_.eval_value(), r_.eval_value(), + if constexpr (std::is_same_v && is_scalar_leaf2::value) return l_.eval_value() * r_.eval_d2(i, j); + else if constexpr (std::is_same_v && is_scalar_leaf2::value) return l_.eval_d2(i, j) * r_.eval_value(); + else if constexpr (std::is_same_v && is_scalar_leaf2::value) return l_.eval_d2(i, j) / r_.eval_value(); + else return Op::d2(l_.eval_value(), r_.eval_value(), l_.eval_d1(i), r_.eval_d1(i), l_.eval_d1(j), r_.eval_d1(j), l_.eval_d2(i, j), r_.eval_d2(i, j), y_); diff --git a/inst/include/cppde/cppde_dual_expr.hpp b/inst/include/cppde/cppde_dual_expr.hpp index 0fd3a3b..da1d232 100644 --- a/inst/include/cppde/cppde_dual_expr.hpp +++ b/inst/include/cppde/cppde_dual_expr.hpp @@ -142,6 +142,9 @@ struct ScalarLeaf : Expr> { // tangent() takes the cached operand vals + per-index operand tangents + the // node's own cached y_ where useful (Div). // ============================================================================= +template struct is_scalar_leaf : std::false_type {}; +template struct is_scalar_leaf> : std::true_type {}; + struct AddOp { template static T value(const T& a, const T& b) { return a + b; } template static T tangent(const T&, const T&, @@ -196,8 +199,12 @@ struct BinExpr : Expr> { CPPDE_ET_INLINE value_type val() const { return y_; } CPPDE_ET_INLINE unsigned tan_size() const { return sz_; } CPPDE_ET_INLINE bool depends() const { return dep_; } + // a constant factor or divisor adds no tangent term, also where the other operand is infinite CPPDE_ET_INLINE value_type tan(unsigned i) const { - return Op::tangent(l_.val(), r_.val(), l_.tan(i), r_.tan(i), y_); + if constexpr (std::is_same_v && is_scalar_leaf::value) return l_.val() * r_.tan(i); + else if constexpr (std::is_same_v && is_scalar_leaf::value) return l_.tan(i) * r_.val(); + else if constexpr (std::is_same_v && is_scalar_leaf::value) return l_.tan(i) / r_.val(); + else return Op::tangent(l_.val(), r_.val(), l_.tan(i), r_.tan(i), y_); } }; diff --git a/tests/testthat/test-dual2nd-primitives.R b/tests/testthat/test-dual2nd-primitives.R index 34a99da..1bd0f83 100644 --- a/tests/testthat/test-dual2nd-primitives.R +++ b/tests/testthat/test-dual2nd-primitives.R @@ -19,7 +19,12 @@ d2prim <- cppFUN(prims, parameters = pars, deriv = TRUE, deriv2 = TRUE, derivMode = "forward", modelname = "d2prim") d1prim <- cppFUN(prims, parameters = pars, deriv = TRUE, derivMode = "forward", modelname = "d1prim") -compile(d2prim, d1prim, output = "test_dual2nd_primitives", cores = test_cores()) +# a constant times or over a parameter at -Inf +infs <- c(ten = "exp10(x)", mul = "exp(2*x)", mulr = "exp(x*2)", div = "exp(x/2)", lin = "2*x") +d2inf <- cppFUN(infs, parameters = "x", deriv = TRUE, deriv2 = TRUE, + derivMode = "forward", modelname = "d2inf") +d1inf <- cppFUN(infs, parameters = "x", deriv = TRUE, derivMode = "forward", modelname = "d1inf") +compile(d2prim, d1prim, d2inf, d1inf, output = "test_dual2nd_primitives", cores = test_cores()) # Points away from every singularity of the primitives above, one per row, # with a and b positive so that the oracles of atan2 hold. @@ -67,3 +72,12 @@ test_that("the Hessian comes out symmetric entry by entry", { for (nm in names(prims)) expect_identical(out[1, nm, , ], t(out[1, nm, , ]), info = nm) }) + +test_that("a constant factor or divisor gives finite derivatives at an infinite parameter", { + dy <- c(ten = 0, mul = 0, mulr = 0, div = 0, lin = 2) + expect_equal(d1inf$jac(x = -Inf)[1, names(infs), "x"], dy) + out <- d2inf$evaluate(x = -Inf, tangentP = matrix(1, 1, 1, dimnames = list("x", "x")), + hessianP = array(0, c(1, 1, 1), dimnames = list("x", "x", "x")), deriv2 = TRUE) + expect_equal(out$tangent[1, names(infs), "x"], dy) + expect_equal(out$hessian[1, names(infs), "x", "x"], setNames(numeric(5), names(infs))) +})