auto_diff {ADtools}R Documentation

Automatic differentiation

Description

Automatic differentiation

Usage

auto_diff(f, wrt = NULL, at)

Arguments

f

A function of which the derivative is sought.

wrt

A character vector; the name of the variables to differentiate with respect to.

at

A named list of variables; the point at which the derivative is evaluated.

Value

A dual number with components "x" and "dx". The first gives the value of 'f', and the second gives the derivative of 'f'.

Examples

f <- function(y, X, beta) { y - X %*% beta }
auto_diff(
  f, wrt = "beta",
  at = list(beta = c(5,6), X = matrix(1:4, 2, 2), y = c(2,3))
)

g <- function(X, Y) { X %*% Y }
X <- randn(2, 2)
Y <- randn(2, 2)
auto_diff(g, at = list(X = X, Y = Y))


[Package ADtools version 0.5.5 Index]