s-news
[Top] [All Lists]

Re: [S] deriv versus deriv3

To: Bill Venables <William.Venables@cmis.CSIRO.AU>
Subject: Re: [S] deriv versus deriv3
From: Douglas Bates <bates@stat.wisc.edu>
Date: 24 Feb 2000 09:00:14 -0600
Cc: "Timothy R. Johnson" <trjohns@pullman.com>, s-news@wubios.wustl.edu
In-reply-to: Bill Venables's message of "Thu, 24 Feb 2000 15:35:14 +1000"
References: <200002240535.QAA07643@snowy.nsw.cmis.CSIRO.AU>
Sender: owner-s-news@wubios.wustl.edu
Bill Venables <William.Venables@cmis.CSIRO.AU> writes:

> > > foo1 <- deriv(z ~ 2*x + 3*y, c("x","y"), function(x,y) {})
> > > foo1
> > function(x, y)
> > {
> >  .value <- (2 * x) + (3 * y)
> >  .actualArgs <- match.call()[c("x", "y")]
> >  if(all(unlist(lapply(as.list(.actualArgs), is.name)))) {
> >   .grad <- array(0, c(length(.value), 2), list(NULL, c("x", "y")))
> >   .grad[, "x"] <- 2
> >   .grad[, "y"] <- 3
> >   dimnames(.grad) <- list(NULL, .actualArgs)
> >   attr(.value, "gradient") <- .grad
> >  }
> >  .value
> > }
> > > foo1(1,1)
> > [1] 5
> > > attr(foo1(1,1), "gradient")
> > NULL
> 
> It is a bug that was introduced into S-PLUS 4.x and has remained
> safely ensconced there ever since, even S-PLUS 5.1 for Unix is
> affected.  It seems to be a classic case of fixing something that
> simply isn't broken.

Ahem.  Aren't we rewriting history a bit here?  The "someone" who
improved deriv was me and I did it because a certain Bill Venables had
a long-standing bug report against the previous version of deriv.

The previous version of deriv only assigns the correct names to the
columns of the gradient when the user calling the function uses
exactly the same names for the actual arguments as were used for the
formal arguments.  That is, the previous version of deriv assigned the
column names using the formal arguments.  As _you_ pointed out to me,
this is incorrect.  It should use the actual arguments, which this
version does.

When the actual arguments are not variables, there is no derivative so
it is not evaluated.  How do you differentiate a function with respect
to a constant?

I realize that this change is inconvenient and confusing.  My
suggestion is to modify deriv so the function produced by deriv will
always return the gradient but only assigns names to the columns when
the actual arguments are names.
-----------------------------------------------------------------------
This message was distributed by s-news@wubios.wustl.edu.  To unsubscribe
send e-mail to s-news-request@wubios.wustl.edu with the BODY of the
message:  unsubscribe s-news

<Prev in Thread] Current Thread [Next in Thread>