Skip to contents

Convenience functions that allow taping of gradients in RTMB with if expressions, following the corresponding CppAD functions.

Usage

CondExpLt(left, right, if_true, if_false)

CondExpLe(left, right, if_true, if_false)

CondExpGt(left, right, if_true, if_false)

CondExpGe(left, right, if_true, if_false)

CondExpEq(left, right, if_true, if_false)

Arguments

left

Numeric on left hand side of the evaluation

right

Numeric on right hand side of the evaluation

if_true

Numeric if expression is true

if_false

Numeric if expression is false

Details

Functions should be vectorized.

CondExpLt evaluates whether left < right

CondExpLe evaluates whether left <= right

CondExpGt evaluates whether left > right

CondExpGe evaluates whether left >= right

CondExpEq evaluates whether left == right

Examples

library(RTMB)
TapeConfig(comparison = "tape")
f <- function(x) CondExpLt(x, 3, 0, x^2)
g <- MakeTape(f, numeric(1))
x <- seq(0, 5)

# Does not work!
f2 <- function(x) if (x < 3) 0 else x^2
g2 <- MakeTape(f2, numeric(1))

data.frame(x = x, deriv = sapply(x, g$jacobian), deriv2 = sapply(x, g2$jacobian))
#>   x deriv deriv2
#> 1 0     0      0
#> 2 1     0      0
#> 3 2     0      0
#> 4 3     6      0
#> 5 4     8      0
#> 6 5    10      0