Deriv (version 4.0)

Deriv-package: Symbolic Differentiation

Description

R already contains two differentiation functions: D and deriv.

These functions have several limitations:

  • the derivatives table can't be modified at runtime, and is only available in C.

  • function cannot substitute function calls. eg:

    f <- function(x, y) x + y; deriv(~f(x, x^2), "x")

The advantages of this package include:

  • It is entirely written in R, so would be easier to maintain.

  • Can differentiate function calls:

    • if the function is in the derivative table, then the chain rule is applied.

    • 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, e.g.

    drule[["cos"]] <- alist(x=-sin(x))

  • The output can be an executable function, which makes it suitable for use in optimization problems.

  • Starting from v4.0, some matrix calculus operations are possible (contribution of Andreas Rappold). See an example in help("Deriv") for differentiation of the inverse of 2x2 matrix and whose elements depend on variable of differentiation x.

Arguments

Details

Package: Deriv
Type: Package
Version: 4.0
Date: 2019-12-10
License: GPL (>= 3)

Two main functions are Deriv() for differentiating and Simplify() for simplifying symbolically.

References

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

See Also

D, deriv, packages Ryacas, rSymPy

Examples

Run this code
# NOT RUN {
f <- function(x) x^2
# }
# NOT RUN {
Deriv(f)
# }
# NOT RUN {
# function (x)
# 2 * x
# }

Run the code above in your browser using DataCamp Workspace