[Rd] bugs in deriv(*, *, function.arg = ) (PR#953)

maechler@stat.math.ethz.ch maechler@stat.math.ethz.ch
Mon, 28 May 2001 10:45:18 +0200 (MET DST)


Also, this should have gone in R-bugs quite a while ago :

------- 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", 
	      "y")))
	  .grad[, "x"] <- -.expr4 * (sin(x) * y)
	  .grad[, "y"] <- .expr4 * .expr1
	  attr(.value, "gradient") <- .grad
	  .value
      }


    which *was* S(-plus) compatible.
    It stopped working with 0.65.1,
    and later someone added to  help(deriv) 's description of  function.arg

      \item{function.arg}{If specified, a character vector of arguments for
	a function return, or a function (with empty body) or \code{TRUE},
	the latter indicating that a function with argument names
	\code{namevec} should be used.

    the comment 
       >>>> \bold{Note:} this is incompatible with S.}

    Today, I've been looking a bit at the C code and I can't understand why
    giving the function.arg as a function doesn't work anymore.
    (it just returns the 'empty' function.arg).
    Interestingly, it *did* work when I ran things under gdb .. ? .. ?

    The result is the same under gctorture(),
    however, that does reveal a bug (missing PROTECT() ?),
    clearly even under 1.2.2 :

    > gctorture()

    > deriv(expression(sin(x + y^2)), c("x", "y"),c("x", "y"))
    [[1]]


    . 

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

    > 

which is a pretty funny beast.

Something for PROTECT() specialists; 
I think there really are two bugs, one when the `function.arg' is a
character vector (bug showing with gctorture()) and one when it is a
function (the body is just not set).

Note that I've committed a very slightly enhanced deriv.c for R-devel
but that should hopefully merge nicely once the bug is found and fixed on
R-release.

Martin

------- end of forwarded message -------


Note that the above "beast" is a "pairlist" ...
MM

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._