Learn R Programming

Deriv (version 1.0)

Deriv-package: Symbolic differentiation

Description

R already contains two differentiation functions: D and deriv. D does simple univariate differentiation. "deriv" uses D do to multivariate differentiation. The output of "D" is an expression, whereas the output of "deriv" is an executable function.

R's existing functions have several limitations. They can probably be fixed, but since they are written in C, this would probably require a lot of work. Limitations include:

  • The derivatives table can't be modified at runtime, and is only available in C.
  • The output of "deriv" can not be differentiated again.
  • Neither function can substitute function calls. eg: f <- function(x, y) x + y; deriv(f(x, x^2), "x")
So, here are the advantages and disadvantages of this implementation:

GOOD POINTS:

  • It is entirely written in R, so would be easier to maintain.
  • Can do multi-variate differentiation.
  • Can differentiate function calls: - if the function is in the derivative table, then the chain rule is applied. For example, if you declared that the derivative of sin is cos, then it would figure out how to call cos correctly. - if the function is not in the derivative table (or it is anonymous), then the function body is substituted in. - these two methods can be mixed. An entry in the derivative table need not be self-contained -- you don't need to provide an infinite chain of derivatives.
  • It's easy to add custom entries to the derivatives table. It could be easier though... it would be nice if something like add.deriv("cos", "-sin(expr)") worked, rather than the clunky function definitions. (This is purely a cosmetic issue, though... everything works as is.)
  • The output is an executable function, which makes it suitable for use in optimization problems.

BAD POINTS:

  • Differentiating vector-valued functions doesn't work properly, since the multiplication code doesn't know when to use scalar vs matrix multiplication. Unfortunately, solving this is a hard problem because we would need to know if an arbitrary expression is a vector or not. We would have to add extra metadata to do this. Bottom line: can compute gradients but not Jacobians or Hessians.
  • Gives useless error messages when it gets stuck. This could be fixed.

Arguments

Details

ll{ Package: Deriv Type: Package Version: 1.0 Date: 2014-12-10 License: GPL (>= 3) } The package contains two useful functions: Deriv() for differentiating expressions, and Deriv.function() for differentiating functions symbolically. It uses a series of Simplify.something() functions.

References

https://andrewclausen.net/computing/deriv.html

See Also

D, deriv, yacas

Examples

Run this code
f <- function(x) x^2
Deriv.function(f)
# function (x) 
# 2 * x

Run the code above in your browser using DataLab