Bug in printing symbolic derivative (PR#1697)

murdoch@stats.uwo.ca murdoch@stats.uwo.ca
Thu, 20 Jun 2002 22:28:30 +0200 (MET DST)


A colleague pointed out the following error in D():

 f  <- expression(exp(-2*x*y))
 Dx <- D(f, 'x')
 Dy <- D(f,'y')
 Dxy <- D(Dx, 'y')
 Dyx <- D(Dy, 'x')

Then 

>  Dx    # this is right
-exp(-2 * x * y) * (2 * y)
>  Dy    # this is right
exp(-2 * x * y) * (-2 * x)
>  Dxy   # this is wrong in the sign of the second term!!!
-exp(-2 * x * y) * (-2 * x) * (2 * y) + exp(-2 * x * y) * 2
>  Dyx   # this is also wrong in the sign of the second term!!!
-exp(-2 * x * y) * 2 + exp(-2 * x * y) * (2 * y) * (-2 * x)

In both cases, the answer would be correct if the initial sign were
applied to the whole expression; is this just a case of not printing
some parentheses?  Let's experiment...

> x <- 1
> y <- 2
> eval(Dxy)
[1] 0.1098938  # this is right
> eval(Dyx)
[1] 0.1098938  # this is right
> -exp(-2 * x * y) * (-2 * x) * (2 * y) + exp(-2 * x * y) * 2 # this is how Dxy prints
[1] 0.1831564  # this is wrong!!
> -(exp(-2 * x * y) * (-2 * x) * (2 * y) + exp(-2 * x * y) * 2) # this is how it should print
[1] 0.1098938  # this is right

Looks like that's it...

Duncan Murdoch

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