[Rd] deriv (PR#953)

tlumley@u.washington.edu tlumley@u.washington.edu
Mon, 28 May 2001 22:58:20 +0200 (MET DST)

------- start of forwarded message -------
From: Martin Maechler <maechler@stat.math.ethz.ch>
To: R-core@stat.math.ethz.ch
Subject: PROTECT() bugs in deriv(*, *, function.arg = )
Date: Mon, 16 Apr 2001 21:02:10 +0200

In R versions 0.50 and 0.64.2 ,
the following worked

      > deriv(expression(sin(cos(x) * y)), c("x","y"), function(x,y){})
      function (x, y)
          .expr1 <- cos(x)
          .expr2 <- .expr1 * y
          .expr4 <- cos(.expr2)
          .value <- sin(.expr2)
          .grad <- array(0, c(length(.value), 2), list(NULL, c("x",
          .grad[, "x"] <- -.expr4 * (sin(x) * y)
          .grad[, "y"] <- .expr4 * .expr1
          attr(.value, "gradient") <- .grad

The second bug (gctorture-provoked) is a missing PROTECT(), which I will
commit when I've run make check.

The first bug is very strange and I suspect there's something going on in
the memory management.  Supplying a function as the third argument should
cause the body of the function to be replaced by the derivative. This
sometimes works, but doesn't depend on the function in any simple way.

It seems to depend on how new the function is. With a new anonymous
function it never works, but with a base function like lm or log it always

 ## works
 deriv(expression(sin(cos(x) * y)), c("x","y"), log)
 ## identical function
 LOG<-function (x, base = exp(1))
    if (missing(base)) .Internal(log(x)) else .Internal(log(x, base))
 ## doesn't work
 deriv(expression(sin(cos(x) * y)), c("x","y"), LOG)

Creating a new function somewhere else in the search path doesn't help
 # doesn't work
 deriv(expression(sin(cos(x) * y)), c("x","y"), a)

but copying a loaded function does

  deriv(expression(sin(cos(x) * y)), c("x","y"), a)



r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-devel-request@stat.math.ethz.ch