Skip to content
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
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
16 changes: 13 additions & 3 deletions inst/include/cppde/cppde_dual2nd_expr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ struct ScalarLeaf2 : Expr2nd<ScalarLeaf2<T>> {
CPPDE_ET2_INLINE unsigned width() const { return 0u; }
};

template<class X> struct is_scalar_leaf2 : std::false_type {};
template<class T> struct is_scalar_leaf2<ScalarLeaf2<T>> : std::true_type {};

// ===========================================================================
// Op tags for binary arithmetic. Each provides static methods:
// value(av, bv) -> y
Expand Down Expand Up @@ -200,12 +203,19 @@ struct BinExpr2 : Expr2nd<BinExpr2<L, R, Op>> {
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<Op, MulOp2> && is_scalar_leaf2<L>::value) return l_.eval_value() * r_.eval_d1(i);
else if constexpr (std::is_same_v<Op, MulOp2> && is_scalar_leaf2<R>::value) return l_.eval_d1(i) * r_.eval_value();
else if constexpr (std::is_same_v<Op, DivOp2> && is_scalar_leaf2<R>::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<Op, MulOp2> && is_scalar_leaf2<L>::value) return l_.eval_value() * r_.eval_d2(i, j);
else if constexpr (std::is_same_v<Op, MulOp2> && is_scalar_leaf2<R>::value) return l_.eval_d2(i, j) * r_.eval_value();
else if constexpr (std::is_same_v<Op, DivOp2> && is_scalar_leaf2<R>::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_);
Expand Down
9 changes: 8 additions & 1 deletion inst/include/cppde/cppde_dual_expr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,9 @@ struct ScalarLeaf : Expr<ScalarLeaf<T>> {
// tangent() takes the cached operand vals + per-index operand tangents + the
// node's own cached y_ where useful (Div).
// =============================================================================
template<class X> struct is_scalar_leaf : std::false_type {};
template<class T> struct is_scalar_leaf<ScalarLeaf<T>> : std::true_type {};

struct AddOp {
template<class T> static T value(const T& a, const T& b) { return a + b; }
template<class T> static T tangent(const T&, const T&,
Expand Down Expand Up @@ -196,8 +199,12 @@ struct BinExpr : Expr<BinExpr<L, R, Op>> {
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<Op, MulOp> && is_scalar_leaf<L>::value) return l_.val() * r_.tan(i);
else if constexpr (std::is_same_v<Op, MulOp> && is_scalar_leaf<R>::value) return l_.tan(i) * r_.val();
else if constexpr (std::is_same_v<Op, DivOp> && is_scalar_leaf<R>::value) return l_.tan(i) / r_.val();
else return Op::tangent(l_.val(), r_.val(), l_.tan(i), r_.tan(i), y_);
}
};

Expand Down
16 changes: 15 additions & 1 deletion tests/testthat/test-dual2nd-primitives.R
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)))
})
Loading