derivative

0th

Percentile

Numerical and Symbolic Derivatives

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

Usage
derivative(f, var = "x", order = 1, accuracy = 2, stepsize = NULL,
  deparse = TRUE)
Arguments
f

function, expression or character array.

var

character vector, giving the variable names with respect to which derivatives will be computed. If a named vector is provided, derivatives will be computed at that point. See examples.

order

integer vector, giving the differentiation order for each variable. See details.

accuracy

accuracy degree for numerical derivatives.

stepsize

finite differences stepsize for numerical derivatives. Auto-optimized by default.

deparse

logical. Return character instead of expression or call?

Details

The function behaves differently depending on the length of the order argument.

If order is of length 1, then the n-th order derivative is computed for each function with respect to each variable. $$D = \partial^{(n)} \otimes F \rightarrow D_{i,.,j,k,.,l} = \partial^{(n)}_{k,.,l} F_{i,.,j}$$ where \(F\) is the tensor of functions and \(\partial\) is the tensor of variable names with respect to which the \(n\)-th order derivatives will be computed.

If order matches the length of var, then it is assumed that the differentiation order is provided for each variable. In this case, each function will be derived \(n_i\) times with respect to the \(i\)-th variable, for each of the \(j\) variables. $$D = \partial^{(n_1)}_1\partial^{(...)}_{...}\partial^{(n_i)}_i\partial^{(...)}_{...}\partial^{(n_j)}_j F$$ where \(F\) is the tensor of functions to differentiate.

If var is a named vector, e.g. c(x = 0, y = 0), derivatives will be computed at that point. Note that if f is a function, then var must be a named vector giving the point at which the numerical derivatives will be computed.

Value

array of derivatives.

Aliases
  • derivative
Examples
# NOT RUN {
# derive f with respect to x
derivative(f = "sin(x)", var = "x")

# derive f with respect to x and evaluate in x = 0
derivative(f = "sin(x)", var = c("x" = 0))

# derive f twice with respect to x
derivative(f = "sin(x)", var = "x", order = 2)

# derive f once with respect to x, and twice with respect to y
derivative(f = "y^2*sin(x)", var = c("x","y"), order = c(1,2))

# compute the gradient of f with respect to (x,y)
derivative(f = "y*sin(x)", var = c("x","y"))

# compute the Jacobian of f with respect to (x,y)
f <- c("y*sin(x)", "x*cos(y)")
derivative(f = f, var = c("x","y"))

# compute the Hessian of f with respect to (x,y)
g <- derivative(f = "y^2*sin(x)", var = c("x","y"))
derivative(f = g, var = c("x","y"))

# compute the Jacobian of f with respect to (x,y) and evaluate in (0,0)
f1 <- function(x, y) y*sin(x)
f2 <- function(x, y) x*cos(y)
derivative(f = c(f1, f2), var = c("x"=0,"y"=0))

# }
Documentation reproduced from package calculus, version 0.1.0, License: GPL-3

Community examples

Looks like there are no examples yet.