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 n, then the
n-th order derivative is
computed for each element of the tensor with respect to each
variable:
D = ∂(n) ⊗ F
that is:
Di, …, j, k = ∂k(n)Fi, …, j
where F is the tensor of functions and ∂k(n) denotes the n-th order partial derivative with respect to the k-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 nk times with
respect to the k-th variable,
for each of the m
variables.
Di, …, j = ∂1(n1)⋯∂m(nm)Fi, …, 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
x and twice with respect to
y. 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 = 1 and y = 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.
Symbolic derivatives of univariate functions: ∂xsin(x).
Evaluation of symbolic and numerical derivatives: ∂xsin(x)|x = 0.
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: ∂x(4)sin(x)|x = 0.
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 -2.062605e-11
Symbolic derivatives of multivariate functions: ∂x(1)∂y(2)y2sin(x).
Numerical derivatives of multivariate functions: ∂x(1)∂y(2)y2sin(x)|x = 0, y = 0 with degree of accuracy O(h6).
f <- function(x, y) y^2*sin(x)
derivative(f, var = c(x=0, y=0), order = c(1, 2), accuracy = 6)
#> [1] 2
Symbolic gradient of multivariate functions: ∂x, yx2y2.
High order derivatives of multivariate functions: ∂x, y(6)x6y6.
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: ∂x, yx2y2|x = 1, y = 2.
Numerical Jacobian of vector valued functions: ∂x, y[xy, x2y2]|x = 1, y = 2.
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 4
Numerical Jacobian of vector valued where the first argument is used as a parameter vector: ∂X[∑ixi, ∏ixi]|X = 0.
Guidotti 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