derivative function

Numerical and Symbolic Derivatives

Numerical and Symbolic Derivatives

Computes symbolic derivatives based on the D function, or numerical derivatives based on finite differences.

derivative( f, var, params = list(), order = 1, accuracy = 4, stepsize = NULL, drop = TRUE, deparse = TRUE )

Arguments

  • f: array of characters or a function returning a numeric array.
  • var: vector giving the variable names with respect to which the derivatives are to be computed and/or the point where the derivatives are to be evaluated. See details.
  • params: list of additional parameters passed to f.
  • order: integer vector, giving the differentiation order for each variable. See details.
  • accuracy: degree of accuracy for numerical derivatives.
  • stepsize: finite differences stepsize for numerical derivatives. It is based on the precision of the machine by default.
  • drop: if TRUE, return the array of derivatives without adding a dummy dimension when order is of length 1.
  • deparse: if TRUE, return character instead of expression.

Returns

array.

Details

The function behaves differently depending on the arguents order, the order of differentiation, and var, the variable names with respect to which the derivatives are computed.

When multiple variables are provided and order is a single integer nn, then the nn-th order derivative is computed for each element of f

with respect to each variable:

D=(n)F D = \partial^{(n)} \otimes F

that is:

Di,,j,k=k(n)Fi,,j D_{i,\dots,j,k} = \partial^{(n)}_{k} F_{i,\dots,j}

where FF is the array of functions and k(n)\partial_k^{(n)} denotes the nn-th order partial derivative with respect to the kk-th variable.

When order matches the length of var, it is assumed that the differentiation order is provided for each variable. In this case, each element is derived nkn_k times with respect to the kk-th variable, for each of the mm variables.

Di,,j=1(n1)m(nm)Fi,,j D_{i,\dots,j} = \partial^{(n_1)}_1\cdots\partial^{(n_m)}_m F_{i,\dots,j}

The same applies when order is a named vector giving the differentiation order for each variable. For example, order = c(x=1, y=2) differentiates once with respect to xx and twice with respect to yy. A call with order = c(x=1, y=0) is equivalent to order = c(x=1).

To compute numerical derivatives or to evaluate symbolic derivatives at a point, the function accepts a named vector for the argument var; e.g. var = c(x=1, y=2) evaluates the derivatives in x=1x=1 and y=2y=2. For functions where the first argument is used as a parameter vector, var should be a numeric vector indicating the point at which the derivatives are to be calculated.

Examples

### symbolic derivatives derivative(f = "sin(x)", var = "x") ### numerical derivatives f <- function(x) sin(x) derivative(f = f, var = c(x=0)) ### higher order derivatives f <- function(x) sin(x) derivative(f = f, var = c(x=0), order = 3) ### multivariate functions ## - derive once with respect to x ## - derive twice with respect to y ## - evaluate in x=0 and y=0 f <- function(x, y) y^2*sin(x) derivative(f = f, var = c(x=0, y=0), order = c(1,2)) ### vector-valued functions ## - derive each element twice with respect to each variable ## - evaluate in x=0 and y=0 f <- function(x, y) c(x^2, y^2) derivative(f, var = c(x=0, y=0), order = 2) ### vectorized interface f <- function(x) c(sum(x), prod(x)) derivative(f, var = c(0,0,0), order = 1)

References

Guidotti E (2022). "calculus: High-Dimensional Numerical and Symbolic Calculus in R." Journal of Statistical Software, 104(5), 1-37. tools:::Rd_expr_doi("10.18637/jss.v104.i05")

See Also

Other derivatives: taylor()

Other differential operators: curl(), divergence(), gradient(), hessian(), jacobian(), laplacian()

  • Maintainer: Emanuele Guidotti
  • License: GPL-3
  • Last published: 2023-03-09