On Jul 30, 2010, at 4:28 PM, Ravi Varadhan wrote:

> I am not as lazy as Peter (but neither am I as good as him)!  So, please
> allow me to take a stab at completing his exercise:
> 
> Deriv <- function(fn, order=1) {
> ddf <- fn
> body(ddf) <- D(body(f),"x")
###  Typo: body(ddf) or body(fn)

> if (order > 1) {
>       for (i in 2:order) {
>               body(ddf) <- D(body(ddf),"x")
>               } 
>       }
> ddf
> }


Yes. 

One thing: body() and body<-() are relatively expensive operations, and only 
really needed on the way in and the way out, so you might want to use a 
temporary variable.

-- 
Peter Dalgaard
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Email: pd....@cbs.dk  Priv: pda...@gmail.com

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to