The function derivative
performs high-order symbolic and numerical differentiation for generic
tensors with respect to an arbitrary number of variables. The function
behaves differently depending on the arguments 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
,
then the
-th
order derivative is computed for each element of the tensor with respect
to each variable:
that is:
where is the tensor of functions and denotes the -th order partial derivative with respect to the -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
times with respect to the
-th
variable, for each of the
variables.
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
and twice with respect to
.
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
and
.
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.
Symbolic derivatives of univariate functions: .
derivative(f = "sin(x)", var = "x")
#> [1] "cos(x)"Evaluation of symbolic and numerical derivatives: .
sym <- derivative(f = "sin(x)", var = c(x = 0))
num <- derivative(f = function(x) sin(x), var = c(x = 0))#> Symbolic Numeric
#> 1 1
High order symbolic and numerical derivatives: .
sym <- derivative(f = "sin(x)", var = c(x = 0), order = 4)
num <- derivative(f = function(x) sin(x), var = c(x = 0), order = 4)#> Symbolic Numeric
#> 0.000000e+00 -9.767766e-12
Symbolic derivatives of multivariate functions: .
derivative(f = "y^2*sin(x)", var = c("x", "y"), order = c(1, 2))
#> [1] "2 * cos(x)"Numerical derivatives of multivariate functions: with degree of accuracy .
f <- function(x, y) y^2*sin(x)
derivative(f, var = c(x=0, y=0), order = c(1, 2), accuracy = 6)
#> [1] 2Symbolic gradient of multivariate functions: .
derivative("x^2*y^2", var = c("x", "y"))
#> [,1] [,2]
#> [1,] "2 * x * y^2" "x^2 * (2 * y)"High order derivatives of multivariate functions: .
derivative("x^6*y^6", var = c("x", "y"), order = 6)
#> [,1] [,2]
#> [1,] "6 * (5 * (4 * (3 * 2))) * y^6" "x^6 * (6 * (5 * (4 * (3 * 2))))"Numerical gradient of multivariate functions: .
f <- function(x, y) x^2*y^2
derivative(f, var = c(x=1, y=2))
#> [,1] [,2]
#> [1,] 8 4Numerical Jacobian of vector valued functions: .
f <- function(x, y) c(x*y, x^2*y^2)
derivative(f, var = c(x=1, y=2))
#> [,1] [,2]
#> [1,] 2 1
#> [2,] 8 4Numerical Jacobian of vector valued where the first argument is used as a parameter vector: .
f <- function(x) c(sum(x), prod(x))
derivative(f, var = c(0, 0, 0))
#> [,1] [,2] [,3]
#> [1,] 1 1 1
#> [2,] 0 0 0Guidotti E (2022). “calculus: High-Dimensional Numerical and Symbolic Calculus in R.” Journal of Statistical Software, 104(5), 1-37. doi:10.18637/jss.v104.i05
A BibTeX entry for LaTeX users is