Skip to contents

f receives a list of Dual-like R lists list(value=, deriv=) and must return one; port note: R has no operator overloading for a bespoke S3 class here, so f is called with plain numeric duals via .w4a_dual_* helpers exposed to it. A finite-difference check runs alongside for an independent verification route.

Usage

morie_geron_forward_autodiff(f, x)

Arguments

f

function(x_numeric) -> numeric, evaluated exactly (analytic caller-supplied derivative not required: this port differentiates by finite differences AND by dual numbers on simple arithmetic expressions passed as an R function of one Dual list argument via f(list(value=xi, deriv=1))$deriv. See tests for the exact contract.

x

Point(s) at which to differentiate.

Value

List with value, grad, n_passes, fd_check, max_fd_gap.

Examples

f <- function(d) {
  x <- d[[1]]; y <- d[[2]]
  v <- x$value * y$value + x$value^2
  g <- x$deriv * y$value + x$value * y$deriv + 2 * x$value * x$deriv
  list(value = v, deriv = g)
}
morie_geron_forward_autodiff(f, c(1.5, -0.5))
#> $value
#> [1] 1.5
#> 
#> $grad
#> [1] 2.5 1.5
#> 
#> $gradient
#> [1] 2.5 1.5
#> 
#> $n_passes
#> [1] 2
#> 
#> $fd_check
#> [1] 2.5 1.5
#> 
#> $max_fd_gap
#> [1] 1.637801e-11
#> 
#> $estimate
#> [1] 2.915476
#> 
#> $n
#> [1] 2
#> 
#> $method
#> [1] "forward-mode autodiff with dual numbers (exact chain rule)"
#>