Compute derivatives of simple expressions symbolically, allowing user-specified derivatives.
1 2 3 4 5 6 7 8
nlsDeriv(expr, name, derivEnv = sysDerivs, do_substitute = FALSE, verbose = FALSE, ...) codeDeriv(expr, namevec, hessian = FALSE, derivEnv = sysDerivs, do_substitute = FALSE, verbose = FALSE, ...) fnDeriv(expr, namevec, args = all.vars(expr), env = environment(expr), do_substitute = FALSE, verbose = FALSE, ...) newDeriv(expr, deriv, derivEnv = sysDerivs) sysDerivs
An expression represented in a variety of ways. See Details.
The name of the variable with respect to which the derivative will be computed.
The environment in which derivatives are stored.
Additional parameters which will be passed to
Character vector giving the variable names with respect to which the derivatives will be taken.
Logical indicator of whether the 2nd derivatives should also be computed.
An expression giving the derivative of the function call in
Desired arguments for the function. See Details below.
The environment to be attached to the created function. If
codeDeriv are designed as replacements
for the stats package functions
respectively, though the argument lists do not match exactly.
nlsDeriv function computes a symbolic derivative of an expression
or language object. Known derivatives are stored in
derivEnv; the default
sysDerivs contains expressions for
all of the derivatives recognized by
deriv, but in
addition allows differentiation with respect to any parameter
where it makes sense. It also allows the derivative of
sign, using an arbitrary choice of 0 at the discontinuities.
codeDeriv function computes
an expression for efficient calculation of the expression value together
with its gradient and optionally the Hessian matrix.
fnDeriv function wraps the
in a function. If the
args are given as a character
vector (the default), the arguments will have those names,
with no default values. Alternatively, a custom argument list with default values can
be created using
alist; see the example below.
expr argument will be converted to a
language object using
dex (but note
the different default for
Normally it should be a formula with no left
hand side, e.g.
~ x^2 , or an expression vector
expression(x, x^2, x^3) , or a language
fnDeriv the expression vector must be of length 1.
newDeriv function is used to define a new derivative.
expr argument should match the header of the function as a
call to it (e.g. as in the help pages), and the
should be an expression giving the derivative, including calls to
D(arg), which will not be evaluated, but will be substituted
with partial derivatives of that argument with respect to
See the examples below.
deriv is missing in a call to
newDeriv(), it will return the currently saved derivative
name is missing in a call to
nlsDeriv with a function call, it will print a message describing
the derivative formula and return
To handle functions which act differently if a parameter is
missing, code the default value of that parameter to
and give a derivative that is conditional on
applied to that parameter. See the derivatives of
in the file
derivs.R for an example.
expr is an expression vector,
return expression vectors containing the response.
For formulas or language objects, a language object is returned.
codeDeriv always returns a language object.
fnDeriv returns a closure (i.e. a function).
nlsDeriv returns the symbolic derivative of the expression.
deriv specified is
called for the side effect of recording the derivative in
expr is missing, it will return the list of names of functions
for which derivatives are recorded. If
deriv is missing, it
will return its record for the specified function.
newDeriv(expr, deriv, ...) will issue a warning
if a different definition for the derivative exists
in the derivative table.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
newDeriv() newDeriv(sin(x)) nlsDeriv(~ sin(x+y), "x") f <- function(x) x^2 newDeriv(f(x), 2*x*D(x)) nlsDeriv(~ f(abs(x)), "x") nlsDeriv(~ pnorm(x, sd=2, log = TRUE), "x") fnDeriv(~ pnorm(x, sd = sd, log = TRUE), "x") f <- fnDeriv(~ pnorm(x, sd = sd, log = TRUE), "x", args = alist(x =, sd = 2)) f f(1) 100*(f(1.01) - f(1)) # Should be close to the gradient # The attached gradient attribute (from f(1.01)) is # meaningless after the subtraction. # Multiple point example xvals <- c(1, 3, 4.123) print(f(xvals)) # Getting a hessian matrix f2 <- ~ (x-2)^3*y - y^2 mydf2 <- fnDeriv(f2, c("x","y"), hessian=TRUE) # display the resulting function print(mydf2) x <- c(1, 2) y <- c(0.5, 0.1) evalmydf2 <- mydf2(x, y) print(evalmydf2) # the first index of the hessian attribute is the point at which we want the hessian hmat1 <- as.matrix(attr(evalmydf2,"hessian")[1,,]) print(hmat1) hmat2 <- as.matrix(attr(evalmydf2,"hessian")[2,,]) print(hmat2)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.