Computes the matrix of second partial derivatives of f at x using forward-mode AD. Equivalent to D(f, x, order = 2).

hessian(f, x)

Arguments

f

A function taking a parameter vector and returning a scalar.

x

A numeric vector of parameter values.

Value

A p x p numeric matrix (the Hessian).

Examples

f <- function(x) -(x[1] - 3)^2 - (x[2] - 5)^2
hessian(f, c(1, 2))  # diag(c(-2, -2))
#>      [,1] [,2]
#> [1,]   -2    0
#> [2,]    0   -2