In R, I have the following expression for which I would like to take sucessive derivatives with respect to s (theta and nu are nothing but unspecified parameters):
expr <- expression(exp((nu / (theta * (1 - nu))) *
(1 - (1 + theta * s / nu)^(1 - nu))))
To do this, I recursively use the D() function that computes derivatives of simple expressions, symbolically.
But that function does not perform any simplification, i.e., it does not reduce the result into a simpler form.
If you try to take the 10th derivative, say, you will see that the result is very awesome and it requires a lot of computing time. At the limit, it is practically impossible, at least on my computer, to compute the 15th derivative.
Hence, I believe it is worth to try to simplify the n-1th derivative before computing the nth derivative.
I think it is possible to simplify expressions in R thanks to the Ryacas package.
However, my tests are not conclusive…
Does anyone of you has some experience with such a problem?
Does anyone could give me some advice?
Thank you in advance!
For what it’s worth, this seems pretty in easy in Sage . I haven’t ever done more than putter with it, but I could do this by doing the derivative computation in Sage and then cutting & pasting the resulting expression into R (ugly but seems to get this particular job done). (Scroll down to the bottom of the code block for a few lines of R code …) Sage also has an R interface, although I haven’t tried it out.
In Sage (see http://www.sagenb.org/home/pub/3121) [one could make the expression a little more compact/easier to cut and paste, although even less readable, by using one-letter variable names (i.e.
nandtinstead ofnuandtheta)]:In R, cutting and pasting from Sage: