[Rd] Error (or inaccuracy) in complex arithmetic (PR#13869)

RVaradhan at jhmi.edu RVaradhan at jhmi.edu
Tue Aug 4 20:45:16 CEST 2009


Dear All,

I have been trying to compute "exact" derivatives in R using the idea of
complex-step derivatives.  This is a really, really cool idea.  It gives
"exact" derivatives by using a very small, complex step (e.g. 1.e-15i).  

Unfortunately, I cannot implement this in R as the "complex arithmetic" in R
is inaccurate.

Here is an example:

#-- Classical Rosenbrock function in n variables
rosen <- function(x) {
n <- length(x)
x1 <- x[2:n]
x2 <- x[1:(n-1)]
sum(100*(x1-x2^2)^2 + (1-x2)^2)
}

x0 <- c(0.0094, 0.7146, 0.2179, 0.6883, 0.5757, 0.9549, 0.7136, 0.0849,
0.4147, 0.4540)
h <- c(1.e-15*1i, 0, 0, 0, 0, 0, 0, 0, 0, 0)
xh <- x0 + h

rx <- rosen(xh)
Re(rx)
Im (rx)

#  rx = 190.3079796814885 - 12.13915588266717e-15 i  # incorrect imaginary
part in R

However, the imaginary part of the above answer is inaccurate.  The correct
imaginary part (from Matlab, S+, Scilab) is:

190.3079796814886 - 4.66776376640000e-15 i  # correct imaginary part

This inaccuracy is serious enough to affect the acuracy of the compex-step
gradient drastically.


I am wondering whether his problem might be related to the C++ complex.h
library.
 
I am using Windows XP operating system.

Thanks for taking a look at this.

Best regards,
Ravi.


 
----------------------------------------------------------------------------
-------

Ravi Varadhan, Ph.D.

Assistant Professor, The Center on Aging and Health

Division of Geriatric Medicine and Gerontology 

Johns Hopkins University

Ph: (410) 502-2619

Fax: (410) 614-9625

Email: rvaradhan at jhmi.edu

Webpage:
http://www.jhsph.edu/agingandhealth/People/Faculty_personal_pages/Varadhan.h
tml

 

----------------------------------------------------------------------------
--------

 

	[[alternative HTML version deleted]]



More information about the R-devel mailing list