Learn R Programming

ADtools (version 0.5.4)

auto_diff: 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

Run this code
# NOT RUN {
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))

# }

Run the code above in your browser using DataLab