Learn R Programming

Deriv (version 2.0)

Deriv: Symbollic differentiation of an expression or function

Description

Symbollic differentiation of an expression or function

Usage

Deriv(f, x = if (is.function(f)) names(formals(f)) else NA, env = if
  (is.function(f)) environment(f) else parent.frame())

Arguments

f
An expression or function to be differentiated. f can be
  • a user defined function:function(x) x**n
  • a string:"x**n"
  • an expression:expression(x**n)
  • a call:call("^", quote(x), quote(
x
A character string with variable name(s) with resptect to which f must be differentiated. If f is a function x is optional andis set to names(formals(f)). If f is a primitive function, x is set to c("x"
env
An environment where the symbols and functions are searched for. Defaults to parent.frame() for f expression and to environment(f) if f is a function.

Value

    • a function iffis a function
    • an expression iffis an expression
    • a character string iffis a character string
    • a language (usually a so called 'call' but may be also a symbol or just a numeric) for other types off

concept

symbollic differentiation

Details

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.
  • Function cannot substitute function calls. eg: f <- function(x, y) x + y; deriv(~f(x, x^2), "x")

So, here are the advantages of this implementation:

  • It is entirely written in R, so would be easier to maintain.
  • Can do multi-variate differentiation.
  • Can differentiate function calls:
  • It's easy to add custom entries to the derivatives table, e.g.
  • The output is an executable function, which makes it suitable for use in optimization problems.

Two working environments drule and simplifications are exported in the package namescape. As their names indicates, they contain tables of derivative and simplification rules. To see the list of defined rules do ls(drule). To add your own derivative rule for a function called say sinpi(x) calculating sin(pi*x), do drule[["sinpi"]] <- list(quote(pi*._d1*cospi(._1))). Here, "._1" stands for the "first arguments", "._d1" for the first derivative of the first arguments. For a function that might have more than one argument, e.g. log(x, base=exp(1)), the drule entry must be a list with one rule per number of possible arguments. See drule$log for an example to follow. After adding sinpi you can derivate expressions like Deriv(~ sinpi(x^2), "x")

Examples

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

f <- function(x, y) sin(x) * cos(y)
Deriv(f)
# function (x, y)
# c(x = cos(x) * cos(y), y = -(sin(x) * sin(y)))

f_ <- Deriv(f)
f_(3, 4)
#              x         y
# [1,] 0.6471023 0.1068000

Deriv(~ f(x, y^2), "y")
# -(2 * (sin(x) * y * sin(y^2)))

Deriv(quote(f(x, y^2)), c("x", "y"))
# c(x = cos(x) * cos(y^2), y = -(2 * (sin(x) * y * sin(y^2))))

Deriv(expression(sin(x^2) * y), "x")
# expression(cos(x^2) * (2 * x) * y)

Deriv("sin(x^2) * y", "x")
"2 * (x * cos(x^2) * y)"

Run the code above in your browser using DataLab