4

I would like to evaluate higher order derivatives of some function f in R. Two possibilities are available to me.

  1. Either I determine a general expression for f(k), the k-th derivative of f (which I can do in my particular case), and then I evaluate it;
  2. Or I take advantage of the capacities of R to perform symbolic derivative (function D()).

What are the advantages of 1 over 2? Let us say that f(k) is not a recursive formula. What if f(k) is recursive?

Any hint will be appreciated.

joran
  • 169,992
  • 32
  • 429
  • 468
Marco
  • 9,334
  • 7
  • 33
  • 51
  • What are you trying to do? Your questions are unclear. Yes, Ryacas and other algebraic computer languages can calculate analytic (not "symbolic" derivatives (not "derivation"), and yes you can make R calculate the value of various functions which represent derivatives. But why, and for what set of functions `f` ? – Carl Witthoft Jan 08 '12 at 17:03
  • I do speak about "symbolic" derivatives in R (try help(D)). "Why" would be too long and unrelevant to explain. "For what set of functions f"... well, let us say any function for which derivatives can be computed... I would like to know what advantages you would point out, e.g., in terms of computing time, memory, speed,... – Marco Jan 08 '12 at 17:32
  • @G. Grothendieck: Thank you very much for your comments – Marco Jan 08 '12 at 19:12
  • 1
    @G.Grothendieck -- Thanks for chiming in. Would you consider making your comments an answer, and/or mentioning if there is any other advantage of Ryacas over D() w.r.t. symbolic differentiation? The help page for `D()` lists a number of single variable functions that it recognizes. Does Ryacas recognize a bunch more? – Josh O'Brien Jan 08 '12 at 19:26
  • ok, I have deleted my comments and transferred them to an answer. – G. Grothendieck Jan 08 '12 at 20:23

1 Answers1

13

Symbolic differentiation is less error prone than doing it by hand.

For low orders I would not think that symbolic differentiation would take much computer time but you can readily time your specific situation to determine what it is using proc.time, system.time or the rbenchmark package. Also see these examples.

You might want to try both symbolic and hand differentiation as a check.

Regarding R's deriv (and associated functions such as D) vs. the Ryacas package, the latter has the facility to do repeated differentiation without requiring that the user iterate themselves (third arg of deriv specifies the order) and it has a Simplify function for which no counterpart exists in R although Ryacas should be carefully checked since yacas can be a bit buggy at times.

Here is an example:

> library(Ryacas)
> x <- Sym("x")
> y <- (x^2+x)^2
> dy <- deriv(y, x, 2) # 2nd deriv
> dy
expression(2 * (2 * x + 1)^2 + 4 * (x^2 + x))
> Simplify(dy)
expression(2 * (6 * x^2 + 6 * x + 1))

EDIT: Added to example:

> Eval(dy, list(x = 3))
[1] 146
> Eval(Simplify(dy), list(x = 3))
[1] 146
>
> f <- function(x) {}
> body(f) <- yacas(Simplify(dy))[[1]]
> f
function (x) 
2 * (6 * x^2 + 6 * x + 1)
> f(3)
[1] 146
>
> # double check
> w <- 3
> eval(D( D(expression((w^2+w)^2), "w"), "w"))
[1] 146

Also try demo("Ryacas-Function") .

EDIT 2: Fixed an error and added more to the example.

G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341