Re: [Rd] Inaccurate complex arithmetic of R (Matlab is accurate)
> "JN" == John Nolan > on Tue, 4 Aug 2009 18:05:47 -0400 writes: JN> Ravi, JN> There has been a lot of chatter about this, and people don't seem to be JN> reading carefully. Perhaps this will help clarify things. Yes, I hope so; thanks a lot, John!! {Indeed, I've been appalled too about the amount of proclaiming without listening, and even more about the no-bug report... } JN> The problem appears to be that R was evaluating x^2 not as multiplication JN> x*x, but as x^2.0=exp(2.0*log(x)), using standard C functions for the JN> complex log and exponentiation. Apparently the C library functions for JN> this have some inaccuracies in it, which showed up in your calculations. JN> One of the other responders said that matlab, octave, etc. detect the case JN> when there is a positive integer power and use multiplication to evaluate. JN> It appears that Martin Maechler has now submitted a change to R to detect JN> integer powers and evaluate them via repeated multiplication, which should JN> eliminate your problem. Yes; note however that -- exactly for the reason John has given (and Martin Becker and I had tried to explain earlier in this thread!) -- that your problem (*yours* indeed, not R's !!) will resurface if you change "^2" by "^2.01" in your original problem. JN> There is no guarantee that R or matlab or any other JN> program will evaluate every expression correctly. JN> matlab has many years of use and evaluation that have JN> guided them in correct problems. R is catching up, but JN> not there yet. hhmm, hmm,... are you sure that the catching up is only on our side? I dare say that I'm pretty sure R does quite a few computations better than e.g., matlab... But let's not get into more unproductive chattering... JN> We are all part of improving R; your question led to a careful JN> examination of the issue and a fix within a few days. No commercial JN> company responds so quickly! JN> HTH, John it did, thank you very much! Martin JN> ... JN> John P. Nolan JN> Math/Stat Department JN> 227 Gray Hall JN> American University JN> 4400 Massachusetts Avenue, NW JN> Washington, DC 20016-8050 JN> jpno...@american.edu JN> 202.885.3140 voice JN> 202.885.3155 fax JN> http://academic2.american.edu/~jpnolan JN> ... JN> -r-devel-boun...@r-project.org wrote: - JN> To: "'Martin Becker'" JN> From: "Ravi Varadhan" JN> Sent by: r-devel-boun...@r-project.org JN> Date: 08/04/2009 10:59AM JN> cc: hwborch...@googlemail.com, r-de...@stat.math.ethz.ch JN> Subject: Re: [Rd] Inaccurate complex arithmetic of R (Matlab is accurate) JN> Please forgive me for my lack of understanding of IEEE floating-point JN> arithmetic. I have a hard time undertsanding why "this is not a problem of JN> R itself", when "ALL" the other well known computing environments including JN> Matlab, Octave, S+, and Scilab provide accurate results. My concern is not JN> really about the "overall" accuracy of R, but just about the complex JN> arithmetic. Is there something idiosyncratic about the complex arithmetic? JN> I am really hoping that some one from the R core would speak up and address JN> this issue. It would be a loss to the R users if this fascinating idea of JN> "complex-step derivative" could not be implemented in R. JN> Thanks, JN> Ravi. JN> JN> --- JN> Ravi Varadhan, Ph.D. JN> Assistant Professor, The Center on Aging and Health JN> Division of Geriatric Medicine and Gerontology JN> Johns Hopkins University JN> Ph: (410) 502-2619 JN> Fax: (410) 614-9625 JN> Email: rvarad...@jhmi.edu JN> Webpage: JN> http://www.jhsph.edu/agingandhealth/People/Faculty_personal_pages/Varadhan.h JN> tml JN> JN> JN> -Original Message- JN> From: Martin Becker [mailto:martin.bec...@mx.uni-saarland.de] JN> Sent: Tuesday, August 04, 2009 7:34 AM JN> To: Ravi Varadhan JN> Cc: r-de...@stat.math.ethz.ch; hwborch...@googlemail.com JN> Subject: Re: [Rd] Inaccurate complex arithmetic of R (Matlab is accurate) JN> Dear Ravi, JN> I suspect that, in general, you may be facing the limitations of machine JN> accuracy (more precisely, IEEE 754 arithmetics on [64-bit] doubles) in your JN> application. This is not a problem of R itself, but rather a problem of JN> standard arithmetics provided by underlying C compilers/CPUs. JN> In fact, every operation in IEEE arithmetics (so, t
[Rd] Error (or inaccuracy) in complex arithmetic (PR#13869)
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.6677637664e-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: rvarad...@jhmi.edu Webpage: http://www.jhsph.edu/agingandhealth/People/Faculty_personal_pages/Varadhan.h tml [[alternative HTML version deleted]] __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] Inaccurate complex arithmetic of R (Matlab is accurate)
Dear John, Thank you. I am amazed at the insightful responses of people in the R-devel group and even more so amazed at the rapdity with which the R core group and other knowledgeable R folks respond to and fix the problems. Big thanks to Martin Becker, Ash Richardson, Duncan Murdoch and Martin Maechler! My frustration was only due to my impatience in having to wait a few days (!) to experiment with this marvellous idea of analytic continuation for computing numerical derivatives that are "exact", but with only minimal computation. Best regards, Ravi. Ravi Varadhan, Ph.D. Assistant Professor, Division of Geriatric Medicine and Gerontology School of Medicine Johns Hopkins University Ph. (410) 502-2619 email: rvarad...@jhmi.edu - Original Message - From: John Nolan Date: Tuesday, August 4, 2009 8:36 pm Subject: Re: [Rd] Inaccurate complex arithmetic of R (Matlab is accurate) To: Ravi Varadhan Cc: 'Martin Becker' , hwborch...@googlemail.com, r-de...@stat.math.ethz.ch > Ravi, > > There has been a lot of chatter about this, and people don't seem to > be > reading carefully. Perhaps this will help clarify things. > > The problem appears to be that R was evaluating x^2 not as multiplication > x*x, but as x^2.0=exp(2.0*log(x)), using standard C functions for the > complex log and exponentiation. Apparently the C library functions for > this have some inaccuracies in it, which showed up in your calculations. > > One of the other responders said that matlab, octave, etc. detect the > case > when there is a positive integer power and use multiplication to evaluate. > It appears that Martin Maechler has now submitted a change to R to detect > integer powers and evaluate them via repeated multiplication, which should > eliminate your problem. > > There is no guarantee that R or matlab or any other program will evaluate > every expression correctly. matlab has many years of use and evaluation > that have guided them in correct problems. R is catching up, but not > there > yet. We are all part of improving R; your question led to a careful > examination of the issue and a fix within a few days. No commercial > company responds so quickly! > > HTH, John > > ... > > John P. Nolan > Math/Stat Department > 227 Gray Hall > American University > 4400 Massachusetts Avenue, NW > Washington, DC 20016-8050 > > jpno...@american.edu > 202.885.3140 voice > 202.885.3155 fax > > ... > > > > -r-devel-boun...@r-project.org wrote: - > > > To: "'Martin Becker'" > From: "Ravi Varadhan" > Sent by: r-devel-boun...@r-project.org > Date: 08/04/2009 10:59AM > cc: hwborch...@googlemail.com, r-de...@stat.math.ethz.ch > Subject: Re: [Rd] Inaccurate complex arithmetic of R (Matlab is accurate) > > Please forgive me for my lack of understanding of IEEE floating-point > arithmetic. I have a hard time undertsanding why "this is not a > problem of > R itself", when "ALL" the other well known computing environments including > Matlab, Octave, S+, and Scilab provide accurate results. My concern > is not > really about the "overall" accuracy of R, but just about the complex > arithmetic. Is there something idiosyncratic about the complex arithmetic? > > > I am really hoping that some one from the R core would speak up and address > this issue. It would be a loss to the R users if this fascinating > idea of > "complex-step derivative" could not be implemented in R. > > Thanks, > 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: rvarad...@jhmi.edu > > Webpage: > > > tml > > > > > > > > > -Original Message- > From: Martin Becker [ > Sent: Tuesday, August 04, 2009 7:34 AM > To: Ravi Varadhan > Cc: r-de...@stat.math.ethz.ch; hwborch...@googlemail.com > Subject: Re: [Rd] Inaccurate complex arithmetic of R (Matlab is accurate) > > Dear Ravi, > > I suspect that, in general, you may be facing the limitations of machine > accuracy (more precisely, IEEE 754 arithmetics on [64-bit] doubles) > in your > application. This is not a problem of R itself, but rather a problem > of > standard arithmetics provided by underlying C compilers/CPUs. > In fact, every operation in IEEE arithmetics (so, this is not really > a > problem only for complex numbers) may suffer from inexactness, a > particularly difficu
[Rd] compiling DLL under Visual Studio IDE
Hi, I have successfully compiled DLLs and invoked the functions using the R toolset. I would also like to compile the DLL using Microsof Visual Studio compiler (2005, if that matters). The FAQ tells how to do that from the command line; however I'd also like to do that from within the IDE. So far I failed: The DLL was built, and actually the function of interest was put under export category; but apparently something did not work out because R couldn't locate the function (loading the DLL worked fine). I know this question is only marginally R related, so : a) is it simple to set up a virgin (empty) project using the MSVC IDE? Any hints would be much appreciated. b) otherwise, shall I transfer the question to a MSVC newsgroup and ask for helping translating the command-line options into the project settings I have to set in the IDE ? Is there a 1:1 mapping ? Thanks, Thomas __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
[Rd] exec subdirectory of a package
Hello, WRE contains the following information about the "exec" subdirectory of a package : "Subdirectory exec could contain additional executables the package needs, typically scripts for interpreters such as the shell, Perl, or Tcl. This mechanism is currently used only by a very few packages, and still experimental." I think it would be useful to expand "very few". For example "tcltk" uses it. But the real questions are; - can this be used in conjunction with "R CMD", something like $ R CMD execute package script parameters The alternative used in a few packages (Rserve, Roxygen, ...) is to add a file in ${R_HOME}/bin, but I am not sure this works with binary builds of packages - can the script be an R script, in that case would a shebang like this be enough : #!/bin/env Rscript definitely works on (at least my) linux A very naive "execute" written in R follows. Romain #!/bin/env Rscript args <- commandArgs(TRUE) if( length(args) < 2 ){ stop( "usage : R CMD execute package script [parameters]\n" ) } package <- args[1] script <- args[2] scriptfile <- file.path( system.file( "exec", script, package = package ) ) if( !file.exists( scriptfile ) ){ stop( sprintf( "file not found: '%s' ", scriptfile ) ) } trail <- if( length(args) > 2 ) paste( tail( args, -2 ), sep = " " ) else "" cmd <- sprintf( '"%s" %s', scriptfile, trail ) system( cmd ) -- Romain Francois Professional R Enthusiast +33(0) 6 28 91 30 30 http://romainfrancois.blog.free.fr |- http://tr.im/vsK1 : R parser package on CRAN |- http://tr.im/vshK : Transfer files through Rserve `- http://tr.im/vfxe : R GUI page on the R wiki [[alternative HTML version deleted]] __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
[Rd] inheriting C symbols
The package coxme depends heavily on bdsmatrix, to the point of needing access to some of its C calls. The kinship package (in progress) uses the R level functions in bdsmatrix, but not the C calls. That is, I don't want to always export the symbols. For testing I can use an explicit dyn.load('somepath/bsdmatrix.so', local=F). How do I incorporate such a dependency in the DESCRIPTION file for coxme, so that it happens in general? Terry T. __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] inheriting C symbols
This has been discussed before: http://www.nabble.com/Dynamic-linking-to-binary-code-from-other-packages---td20167535.html#a20474561 Take a look at 'xts' on cran, or R-forge. Specifically: http://r-forge.r-project.org/plugins/scmsvn/viewcvs.php/pkg/inst/api_example/README?rev=386&root=xts&view=markup HTH Jeff On Wed, Aug 5, 2009 at 11:29 AM, Terry Therneau wrote: > The package coxme depends heavily on bdsmatrix, to the point of needing > access to some of its C calls. The kinship package (in progress) uses > the R level functions in bdsmatrix, but not the C calls. That is, I > don't want to always export the symbols. > > For testing I can use an explicit dyn.load('somepath/bsdmatrix.so', > local=F). How do I incorporate such a dependency in the DESCRIPTION > file for coxme, so that it happens in general? > > Terry T. > > __ > R-devel@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel > -- Jeffrey Ryan jeffrey.r...@insightalgo.com ia: insight algorithmics www.insightalgo.com __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] inheriting C symbols
Thanks for the information. I'd looked through the manual but missed this. Follow-up questions 1. I'd never paid much attention to that whole "register routines" section of the manual because a) it didn't seem necessary (my code worked anyway) and b) it's quite complex. Your answer is that if I dig into it, there is a way to export routines. Is there any other real advantage to registration? 2. The simpler method is to use a local=FALSE argument to dyn.load. Since it wasn't mentioned, I'll assume that there is no way to add text to the DESCRIPTION file saying "and load the symbols too"? It would be much simpler. Also a bit more dangerous of course, since function prototypes are not being exported via a header. Terry T On Wed, 2009-08-05 at 11:54 -0500, Jeff Ryan wrote: > This has been discussed before: > > http://www.nabble.com/Dynamic-linking-to-binary-code-from-other-packages---td20167535.html#a20474561 > > Take a look at 'xts' on cran, or R-forge. Specifically: > > http://r-forge.r-project.org/plugins/scmsvn/viewcvs.php/pkg/inst/api_example/README?rev=386&root=xts&view=markup > > HTH > Jeff > > On Wed, Aug 5, 2009 at 11:29 AM, Terry Therneau wrote: > > The package coxme depends heavily on bdsmatrix, to the point of needing > > access to some of its C calls. The kinship package (in progress) uses > > the R level functions in bdsmatrix, but not the C calls. That is, I > > don't want to always export the symbols. > > > > For testing I can use an explicit dyn.load('somepath/bsdmatrix.so', > > local=F). How do I incorporate such a dependency in the DESCRIPTION > > file for coxme, so that it happens in general? > > > > Terry T. > > > > __ > > R-devel@r-project.org mailing list > > https://stat.ethz.ch/mailman/listinfo/r-devel > > > > > __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] inheriting C symbols
Terry, I think I don't quite follow what you want to do. You have a package that needs to access the R level functions in another, or the C level functions? Do you need access *from R* for the above, or *from new C code*? If you are just looking to call .Call("other_package", ...) I think your only strategy is to list it as a dependency. Is there some reason you don't want to do that? Once it is a dependency you can simply call the C routines like they are called within the package's R code. Jeff On Wed, Aug 5, 2009 at 2:24 PM, Terry Therneau wrote: > Thanks for the information. I'd looked through the manual but missed > this. Follow-up questions > > 1. I'd never paid much attention to that whole "register routines" > section of the manual because a) it didn't seem necessary (my code > worked anyway) and b) it's quite complex. Your answer is that if I dig > into it, there is a way to export routines. Is there any other real > advantage to registration? > > 2. The simpler method is to use a local=FALSE argument to dyn.load. > Since it wasn't mentioned, I'll assume that there is no way to add text > to the DESCRIPTION file saying "and load the symbols too"? It would be > much simpler. Also a bit more dangerous of course, since function > prototypes are not being exported via a header. > > Terry T > > > On Wed, 2009-08-05 at 11:54 -0500, Jeff Ryan wrote: >> This has been discussed before: >> >> http://www.nabble.com/Dynamic-linking-to-binary-code-from-other-packages---td20167535.html#a20474561 >> >> Take a look at 'xts' on cran, or R-forge. Specifically: >> >> http://r-forge.r-project.org/plugins/scmsvn/viewcvs.php/pkg/inst/api_example/README?rev=386&root=xts&view=markup >> >> HTH >> Jeff >> >> On Wed, Aug 5, 2009 at 11:29 AM, Terry Therneau wrote: >> > The package coxme depends heavily on bdsmatrix, to the point of needing >> > access to some of its C calls. The kinship package (in progress) uses >> > the R level functions in bdsmatrix, but not the C calls. That is, I >> > don't want to always export the symbols. >> > >> > For testing I can use an explicit dyn.load('somepath/bsdmatrix.so', >> > local=F). How do I incorporate such a dependency in the DESCRIPTION >> > file for coxme, so that it happens in general? >> > >> > Terry T. >> > >> > __ >> > R-devel@r-project.org mailing list >> > https://stat.ethz.ch/mailman/listinfo/r-devel >> > >> >> >> > > -- Jeffrey Ryan jeffrey.r...@insightalgo.com ia: insight algorithmics www.insightalgo.com __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] Wishlist: Navigate to "Index" page of help when no topic (PR#13872)
Thanks Romain, I find index?survival more intuitive than my proposed ?survival:: Steven McKinney From: r-devel-boun...@r-project.org [r-devel-boun...@r-project.org] On Behalf Of Romain Francois [romain.franc...@dbmail.com] Sent: August 4, 2009 11:24 PM To: murd...@stats.uwo.ca Cc: r-de...@stat.math.ethz.ch Subject: Re: [Rd] Wishlist: Navigate to "Index" page of help when no topic (PR#13872) Hello, I'm jumping a bit late on this, but what about something like: index?survival or more generally foo?bar with the ability for the user/package to define what the combination "foo"/"bar" means. At the moment, the way "package?survival" is handled is to : - create the string "package-survival" - call help with it Romain On 08/05/2009 02:10 AM, murd...@stats.uwo.ca wrote: > On 04/08/2009 7:33 PM, Steven McKinney wrote: >>> -Original Message- >>> From: Duncan Murdoch [mailto:murd...@stats.uwo.ca] >>> Sent: Tuesday, August 04, 2009 8:03 AM >>> To: Steven McKinney >>> Cc: r-de...@stat.math.ethz.ch; r-b...@r-project.org >>> Subject: Re: [Rd] Wishlist: Navigate to "Index" page of help when no >>> topic specified (PR#13860) >>> >>> On 7/28/2009 6:30 PM, smckin...@bccrc.ca wrote: Hi all, When I install a new package, and don't yet know any function names, I have to play the "poor man's game" to get to the standard help system "Index" page for the package: >>> You could complain to the package author or maintainer, who should have >>> created a help alias with the package name so that ?survival would give >>> you something useful. But I would guess R 2.10.x or 2.11.x will do >>> that >>> automatically. >>> >>> Duncan Murdoch >> Unfortunately it happens frequently - hopefully I >> don't have to complain so much - I'd rather figure >> out code to get to the index page! >> >> When you say "I would guess R 2.10.x or 2.11.x will do >> that automatically" do you mean >> ?survival:: >> or some similar incantation >> will take me to the Index or 00Index page? > > I would guess package?survival will take you to a help page that is > similar to what you get now in the flat display from > library(help=survival). Ideally it will have a link on it to the index > page. It is possible that ?survival will work too; that may be more > controversial, because for many packages (e.g. boot) an alias like that > is already defined, pointing to something other than the package main page. > >> Do I need to propose code or is it already >> known how to do this within the R core group? > > I doubt if code to do that would be accepted (since it will be redundant > once the above changes are in place), but posting it to r-devel might > help people in the meantime. You should look at the tools package, > which has a number of functions that are used by R for constructing the > help pages; there's probably something there that will let you construct > a link to the index fairly easily. > > Duncan Murdoch > >> Best >> Steve McKinney >> >> Poor Man's Game -=20 Load new package;=20 issue search() command; find position (say N) of loaded package;=20 issue objects(pos =3D N) command; get name of a random function (san newFunction);=20 issue ?newFunction command; scroll to bottom of page;=20 click on the "Index" hyperlink There are other variations, but they all involve this=20 long march to the Index page. What I'd like to be able to do is enter the command > help(package =3D "survival") or > ?survival:: and get the usual hyperlinked help page displayed (the "00Index" >>> page) instead of the static "text only" display or an error message (for example, on Windows, this equates to invoking "C:/PROGRA~1/R/R-29~1.1/library/survival/chm/00Index" on Apple Mac, >>> "/Library/Frameworks/R.framework/Resources/library/survival/html/00Inde >>> x.ht= ml" etc.) Details: --- The help() function returns an object of class "help_files_with_topic". The object consists of a character vector with several attributes. PC: Windows XP > library("survival") > foo<- help("aareg", package =3D "survival") > class(foo) [1] "help_files_with_topic" > foo[1] [1] "C:/PROGRA~1/R/R-29~1.1/library/survival/chm/aareg" > attributes(foo) $call help(topic =3D "aareg", package =3D "survival") $pager [1] "internal" $topic [1] "aareg" $tried_all_packages [1] FALSE $type [1] "chm" $class [1] "help_files_with_topic" > bar<- help("", package =3D "survival") > class(bar) [1] "help_files_with_topic" > bar[1] [1] NA > attributes(bar) $call help(topic =3D "", package =3D "survival") $pager [1] "internal" >>