library(calculus)
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=∂(n)kFi,…,j
where F is the tensor of functions and ∂(n)k 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=∂(n1)1⋯∂(nm)mFi,…,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).
derivative(f = "sin(x)", var = "x")
#> [1] "cos(x)"
Evaluation of symbolic and numerical derivatives: ∂xsin(x)|x=0.
<- derivative(f = "sin(x)", var = c(x = 0))
sym <- derivative(f = function(x) sin(x), var = c(x = 0)) num
#> Symbolic Numeric
#> 1 1
High order symbolic and numerical derivatives: ∂(4)xsin(x)|x=0.
<- derivative(f = "sin(x)", var = c(x = 0), order = 4)
sym <- derivative(f = function(x) sin(x), var = c(x = 0), order = 4) num
#> Symbolic Numeric
#> 0.000000e+00 -9.767766e-12
Symbolic derivatives of multivariate functions: ∂(1)x∂(2)yy2sin(x).
derivative(f = "y^2*sin(x)", var = c("x", "y"), order = c(1, 2))
#> [1] "2 * cos(x)"
Numerical derivatives of multivariate functions: ∂(1)x∂(2)yy2sin(x)|x=0,y=0 with degree of accuracy O(h6).
<- function(x, y) y^2*sin(x)
f derivative(f, var = c(x=0, y=0), order = c(1, 2), accuracy = 6)
#> [1] 2
Symbolic gradient of multivariate functions: ∂x,yx2y2.
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: ∂(6)x,yx6y6.
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.
<- function(x, y) x^2*y^2
f derivative(f, var = c(x=1, y=2))
#> [,1] [,2]
#> [1,] 8 4
Numerical Jacobian of vector valued functions: ∂x,y[xy,x2y2]|x=1,y=2.
<- function(x, y) c(x*y, x^2*y^2)
f 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.
<- function(x) c(sum(x), prod(x))
f derivative(f, var = c(0, 0, 0))
#> [,1] [,2] [,3]
#> [1,] 1 1 1
#> [2,] 0 0 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
@Article{calculus,
title = {{calculus}: High-Dimensional Numerical and Symbolic Calculus in {R}},
author = {Emanuele Guidotti},
journal = {Journal of Statistical Software},
year = {2022},
volume = {104},
number = {5},
pages = {1--37},
doi = {10.18637/jss.v104.i05},
}