| Title: | Automatic Differentiation of Simple Functions |
|---|---|
| Description: | Implementation of automatically computing derivatives of functions (see Mailund Thomas (2017) <doi:10.1007/978-1-4842-2881-4>). Moreover, calculating gradients and Hessian matrices is possible. |
| Authors: | Thomas Mailund [aut], Konrad Krämer [aut, cre] |
| Maintainer: | Konrad Krämer <[email protected]> |
| License: | GPL-3 |
| Version: | 0.2.0 |
| Built: | 2026-11-23 16:39:02 UTC |
| Source: | https://github.com/Konrad1991/dfdr |
Differentiate a function for a single variable.
d(f, x, derivs = NULL)d(f, x, derivs = NULL)
f |
The function to differentiate. |
x |
The variable that f should be differentiated with respect to. |
derivs |
An S4 class of type fcts that defines additional derivatives. See |
The following functions are already supported:
sin, sinh, asin, cos, cosh, acos, tan, tanh, atan, exp, log, sqrt, c, vector, numeric, rep and matrix.
Notably, for the functions: c, vector, numeric, rep and matrix the function is ignored during differentiation.
For example function f and symbol x:
df/dx
library(dfdr) d(sin, x) f <- function(x) -sin(x) d(f, x) # Initialize list lst <- dfdr::fcts() # The function which should be added f <- function(x) x^2 # The dervative function of f f_deriv <- function(x) 2*x # add new entry to list lst <- fcts_add_fct(lst, f, f_deriv) g <- function(z) f(z) d(g, z, lst)library(dfdr) d(sin, x) f <- function(x) -sin(x) d(f, x) # Initialize list lst <- dfdr::fcts() # The function which should be added f <- function(x) x^2 # The dervative function of f f_deriv <- function(x) 2*x # add new entry to list lst <- fcts_add_fct(lst, f, f_deriv) g <- function(z) f(z) d(g, z, lst)
A S4 class containing additional functions which can be used for calculating derivatives with d().
To create a class the function fcts() should be used.
Adding functions is only possible via the function add_fct.
The following functions are already supported:
sin, sinh, asin, cos, cosh, acos, tan, tanh, atan, exp, log, sqrt, c, vector, numeric, rep and matrix.
Notably, for the functions: c, vector, numeric, rep and matrix the function is ignored during differentiation.
funsA list containing the specified functions. This slot should not be accessed and is used only internally.
d()
library(dfdr) # Initialize list lst <- dfdr::fcts() # The function which should be added f <- function(x) x^2 # The dervative function of f f_deriv <- function(x) 2*x # add new entry to list lst <- fcts_add_fct(lst, f, f_deriv) g <- function(z) f(z) df <- d(g, z, lst) dflibrary(dfdr) # Initialize list lst <- dfdr::fcts() # The function which should be added f <- function(x) x^2 # The dervative function of f f_deriv <- function(x) 2*x # add new entry to list lst <- fcts_add_fct(lst, f, f_deriv) g <- function(z) f(z) df <- d(g, z, lst) df
A function which appends a S4 class of type fcts with a new function-derivative pair.
fcts_add_fct(lst, f, f_deriv, keep = FALSE)fcts_add_fct(lst, f, f_deriv, keep = FALSE)
lst |
is the S4 class of type fcts. Newly created by |
f |
is the function which should be differentiated. The argument has to be of type function. |
f_deriv |
is a function defining the derivative of f. The argument has to be of type function. |
keep |
is a logical value. If set to TRUE the function f is ignored of |
The following functions are already supported:
sin, sinh, asin, cos, cosh, acos, tan, tanh, atan, exp, log, sqrt, c, vector, numeric, rep and matrix.
Notably, for the functions: c, vector, numeric, rep and matrix the function is ignored during differentiation.
a S4 class of type fcts extended by the new function-derivative pair.
The body of f and f_deriv have to be defined without curly brackets.
library(dfdr) # Initialize list lst <- dfdr::fcts() # The function which should be added f <- function(x) x^2 # The dervative function of f f_deriv <- function(x) 2*x # add new entry to list lst <- fcts_add_fct(lst, f, f_deriv) g <- function(z) f(z) df <- d(g, z, lst) dflibrary(dfdr) # Initialize list lst <- dfdr::fcts() # The function which should be added f <- function(x) x^2 # The dervative function of f f_deriv <- function(x) 2*x # add new entry to list lst <- fcts_add_fct(lst, f, f_deriv) g <- function(z) f(z) df <- d(g, z, lst) df
Creates a function that computes the derivative of a function with respect to each parameter and return a vector of these.
gradient(f, use_names, ...)gradient(f, use_names, ...)
f |
A function |
use_names |
Should the gradient add variable names to the output of the function? |
... |
The variable names for which gradients should be calculated |
A function that computes the gradient of f at any point.
f <- function(x, y) x^2 + y^2 df <- gradient(f, FALSE, x, y) df(1, 1)f <- function(x, y) x^2 + y^2 df <- gradient(f, FALSE, x, y) df(1, 1)
Creates a function that computes the second-order derivatives of a function with respect to each pair of parameters and return a vector of these.
hessian(f, use_names = FALSE, ...)hessian(f, use_names = FALSE, ...)
f |
A function |
use_names |
Should the gradient add variable names to the output of the function? |
... |
The variable names for which gradients should be calculated |
A function that computes the gradient of f at any point.
f <- function(x, y) x**2 + y**2 h <- hessian(f, FALSE, x, y) h(0, 0)f <- function(x, y) x**2 + y**2 h <- hessian(f, FALSE, x, y) h(0, 0)
Simplify an expression by computing the values for constant expressions
simplify(expr)simplify(expr)
expr |
An expression |
a simplified expression
ex <- quote(a*0 + b^2 + 0) simplify(ex)ex <- quote(a*0 + b^2 + 0) simplify(ex)