EVOLUTION-MANAGER
Edit File: deriv.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>R: Symbolic and Algorithmic Derivatives of Simple Expressions</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link rel="stylesheet" type="text/css" href="R.css" /> </head><body> <table width="100%" summary="page for deriv {stats}"><tr><td>deriv {stats}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2>Symbolic and Algorithmic Derivatives of Simple Expressions</h2> <h3>Description</h3> <p>Compute derivatives of simple expressions, symbolically and algorithmically. </p> <h3>Usage</h3> <pre> D (expr, name) deriv(expr, ...) deriv3(expr, ...) ## Default S3 method: deriv(expr, namevec, function.arg = NULL, tag = ".expr", hessian = FALSE, ...) ## S3 method for class 'formula' deriv(expr, namevec, function.arg = NULL, tag = ".expr", hessian = FALSE, ...) ## Default S3 method: deriv3(expr, namevec, function.arg = NULL, tag = ".expr", hessian = TRUE, ...) ## S3 method for class 'formula' deriv3(expr, namevec, function.arg = NULL, tag = ".expr", hessian = TRUE, ...) </pre> <h3>Arguments</h3> <table summary="R argblock"> <tr valign="top"><td><code>expr</code></td> <td> <p>a <code><a href="../../base/html/expression.html">expression</a></code> or <code><a href="../../base/html/call.html">call</a></code> or (except <code>D</code>) a formula with no lhs.</p> </td></tr> <tr valign="top"><td><code>name,namevec</code></td> <td> <p>character vector, giving the variable names (only one for <code>D()</code>) with respect to which derivatives will be computed.</p> </td></tr> <tr valign="top"><td><code>function.arg</code></td> <td> <p>if specified and non-<code>NULL</code>, a character vector of arguments for a function return, or a function (with empty body) or <code>TRUE</code>, the latter indicating that a function with argument names <code>namevec</code> should be used.</p> </td></tr> <tr valign="top"><td><code>tag</code></td> <td> <p>character; the prefix to be used for the locally created variables in result.</p> </td></tr> <tr valign="top"><td><code>hessian</code></td> <td> <p>a logical value indicating whether the second derivatives should be calculated and incorporated in the return value.</p> </td></tr> <tr valign="top"><td><code>...</code></td> <td> <p>arguments to be passed to or from methods.</p> </td></tr> </table> <h3>Details</h3> <p><code>D</code> is modelled after its S namesake for taking simple symbolic derivatives. </p> <p><code>deriv</code> is a <em>generic</em> function with a default and a <code><a href="formula.html">formula</a></code> method. It returns a <code><a href="../../base/html/call.html">call</a></code> for computing the <code>expr</code> and its (partial) derivatives, simultaneously. It uses so-called <em>algorithmic derivatives</em>. If <code>function.arg</code> is a function, its arguments can have default values, see the <code>fx</code> example below. </p> <p>Currently, <code>deriv.formula</code> just calls <code>deriv.default</code> after extracting the expression to the right of <code>~</code>. </p> <p><code>deriv3</code> and its methods are equivalent to <code>deriv</code> and its methods except that <code>hessian</code> defaults to <code>TRUE</code> for <code>deriv3</code>. </p> <p>The internal code knows about the arithmetic operators <code>+</code>, <code>-</code>, <code>*</code>, <code>/</code> and <code>^</code>, and the single-variable functions <code>exp</code>, <code>log</code>, <code>sin</code>, <code>cos</code>, <code>tan</code>, <code>sinh</code>, <code>cosh</code>, <code>sqrt</code>, <code>pnorm</code>, <code>dnorm</code>, <code>asin</code>, <code>acos</code>, <code>atan</code>, <code>gamma</code>, <code>lgamma</code>, <code>digamma</code> and <code>trigamma</code>, as well as <code>psigamma</code> for one or two arguments (but derivative only with respect to the first). (Note that only the standard normal distribution is considered.) <br /> Since <span style="font-family: Courier New, Courier; color: #666666;"><b>R</b></span> 3.4.0, the single-variable functions <code><a href="../../base/html/Log.html">log1p</a></code>, <code>expm1</code>, <code>log2</code>, <code>log10</code>, <code><a href="../../base/html/Trig.html">cospi</a></code>, <code>sinpi</code>, <code>tanpi</code>, <code><a href="../../base/html/Special.html">factorial</a></code>, and <code>lfactorial</code> are supported as well. </p> <h3>Value</h3> <p><code>D</code> returns a call and therefore can easily be iterated for higher derivatives. </p> <p><code>deriv</code> and <code>deriv3</code> normally return an <code><a href="../../base/html/expression.html">expression</a></code> object whose evaluation returns the function values with a <code>"gradient"</code> attribute containing the gradient matrix. If <code>hessian</code> is <code>TRUE</code> the evaluation also returns a <code>"hessian"</code> attribute containing the Hessian array. </p> <p>If <code>function.arg</code> is not <code>NULL</code>, <code>deriv</code> and <code>deriv3</code> return a function with those arguments rather than an expression. </p> <h3>References</h3> <p>Griewank, A. and Corliss, G. F. (1991) <em>Automatic Differentiation of Algorithms: Theory, Implementation, and Application</em>. SIAM proceedings, Philadelphia. </p> <p>Bates, D. M. and Chambers, J. M. (1992) <em>Nonlinear models.</em> Chapter 10 of <em>Statistical Models in S</em> eds J. M. Chambers and T. J. Hastie, Wadsworth & Brooks/Cole. </p> <h3>See Also</h3> <p><code><a href="nlm.html">nlm</a></code> and <code><a href="optim.html">optim</a></code> for numeric minimization which could make use of derivatives, </p> <h3>Examples</h3> <pre> ## formula argument : dx2x <- deriv(~ x^2, "x") ; dx2x ## Not run: expression({ .value <- x^2 .grad <- array(0, c(length(.value), 1), list(NULL, c("x"))) .grad[, "x"] <- 2 * x attr(.value, "gradient") <- .grad .value }) ## End(Not run) mode(dx2x) x <- -1:2 eval(dx2x) ## Something 'tougher': trig.exp <- expression(sin(cos(x + y^2))) ( D.sc <- D(trig.exp, "x") ) all.equal(D(trig.exp[[1]], "x"), D.sc) ( dxy <- deriv(trig.exp, c("x", "y")) ) y <- 1 eval(dxy) eval(D.sc) ## function returned: deriv((y ~ sin(cos(x) * y)), c("x","y"), func = TRUE) ## function with defaulted arguments: (fx <- deriv(y ~ b0 + b1 * 2^(-x/th), c("b0", "b1", "th"), function(b0, b1, th, x = 1:7){} ) ) fx(2, 3, 4) ## First derivative D(expression(x^2), "x") stopifnot(D(as.name("x"), "x") == 1) ## Higher derivatives deriv3(y ~ b0 + b1 * 2^(-x/th), c("b0", "b1", "th"), c("b0", "b1", "th", "x") ) ## Higher derivatives: DD <- function(expr, name, order = 1) { if(order < 1) stop("'order' must be >= 1") if(order == 1) D(expr, name) else DD(D(expr, name), name, order - 1) } DD(expression(sin(x^2)), "x", 3) ## showing the limits of the internal "simplify()" : ## Not run: -sin(x^2) * (2 * x) * 2 + ((cos(x^2) * (2 * x) * (2 * x) + sin(x^2) * 2) * (2 * x) + sin(x^2) * (2 * x) * 2) ## End(Not run) ## New (R 3.4.0, 2017): D(quote(log1p(x^2)), "x") ## log1p(x) = log(1 + x) stopifnot(identical( D(quote(log1p(x^2)), "x"), D(quote(log(1+x^2)), "x"))) D(quote(expm1(x^2)), "x") ## expm1(x) = exp(x) - 1 stopifnot(identical( D(quote(expm1(x^2)), "x") -> Dex1, D(quote(exp(x^2)-1), "x")), identical(Dex1, quote(exp(x^2) * (2 * x)))) D(quote(sinpi(x^2)), "x") ## sinpi(x) = sin(pi*x) D(quote(cospi(x^2)), "x") ## cospi(x) = cos(pi*x) D(quote(tanpi(x^2)), "x") ## tanpi(x) = tan(pi*x) stopifnot(identical(D(quote(log2 (x^2)), "x"), quote(2 * x/(x^2 * log(2)))), identical(D(quote(log10(x^2)), "x"), quote(2 * x/(x^2 * log(10))))) </pre> <hr /><div style="text-align: center;">[Package <em>stats</em> version 3.6.0 <a href="00Index.html">Index</a>]</div> </body></html>