auto_diff: Automatic differentiation

Description Usage Arguments Value Examples

View source: R/auto_diff.R

Description

Automatic differentiation

Usage

1
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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
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))

ADtools documentation built on Nov. 9, 2020, 5:09 p.m.