Dear R-Group: Recently, I ran into a problem. I was using a function called "I.1", which evaluates the first-order modified Bessel function of the first kind, in the package "CircStats". This function is not vectorized, since it uses a couple of "if" conditions. However, when I called this function with a vector argument, I got no error/warning messages in R1.5.1 (under Windows 2000). Furthermore, the results were generally incorect (please see the attached results). However, if I vectorized it using "ifelse" everything is fine (I created a function called "myI.1" to do this). I was wondering if there are functions in other packages, where such problems exist due to non-vectorization? thanks, Ravi. #######################################################> require(CircStats) > I.1(1:10)[1] 0.5651591 1.5906369 3.9533702 9.7594643 24.3351955 [6] 61.3232698 155.7162813 396.4825232 1005.2747089 2517.6347571> myI.1(1:10)[1] 0.5651591 1.5906369 3.9533702 9.7594652 24.3356418 [6] 61.3419369 156.0390965 399.8731348 1030.9147087 2670.9883206 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help 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-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Ravi Varadhan wrote:> > Dear R-Group: > > Recently, I ran into a problem. I was using a function called "I.1", > which evaluates the first-order modified Bessel function of the first > kind, in the package "CircStats". This function is not vectorized, > since it uses a couple of "if" conditions. However, when I called this > function with a vector argument, I got no error/warning messages in > R1.5.1 (under Windows 2000). Furthermore, the results were generally > incorect (please see the attached results). However, if I vectorized > it using "ifelse" everything is fine (I created a function > called "myI.1" to do this). I was wondering if there are functions in > other packages, where such problems exist due to non-vectorization?That's extremely probable. It depends on the package author and the complexity of the problem, thus you have to check it yourself whether it works vectorized or not, e.g. in the help files. BTW: You might want to send your improvements of I.1() to the package author as well (or even at first - hint, hint) ... Uwe Ligges> thanks, > Ravi. > > ####################################################### > > require(CircStats) > > I.1(1:10) > [1] 0.5651591 1.5906369 3.9533702 9.7594643 24.3351955 > [6] 61.3232698 155.7162813 396.4825232 1005.2747089 2517.6347571 > > myI.1(1:10) > [1] 0.5651591 1.5906369 3.9533702 9.7594652 24.3356418 > [6] 61.3419369 156.0390965 399.8731348 1030.9147087 2670.9883206 > > -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- > r-help 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-help-request at stat.math.ethz.ch > _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help 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-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Martin Maechler
2002-Nov-27 08:08 UTC
[R] BesselI(.) {was "Existence of non-vectorised functions"}
>>>>> "Ravi" == Ravi Varadhan <rvaradha at jhsph.edu> >>>>> on Tue, 26 Nov 2002 11:17:45 -0500 writes:Ravi> Dear R-Group: Recently, I ran into a problem. I was Ravi> using a function called "I.1", which evaluates the Ravi> first-order modified Bessel function of the first Ravi> kind, in the package "CircStats". This function is not Ravi> vectorized, since it uses a couple of "if" conditions.... Ravi> <...........> If you use library(help = "CircStats") (something you should do for any package when you find problems!) you see that it has been ported from S (probably S-plus). In R, the Bessel functions have been available for long time, in fast and vectorized form --- although for "numeric" (i.e non-complex) arguments only, and help(bessel) {e.g.} gives you besselI(x, nu, expon.scaled = FALSE) besselK(x, nu, expon.scaled = FALSE) besselJ(x, nu) besselY(x, nu) i.e. I.0(x) = besselI(x,0) \ I.1(x) = besselI(x,1) > when x is numeric I.p(x) = besselI(x,p) / Only if you are interested in the non-numeric complex case, you (and even more the maintainer of CircStats) should investigate improving I.1 (and I.p) to work vectorized and you are right, ifelse() might a good way to do so. Martin Maechler <maechler at stat.math.ethz.ch> http://stat.ethz.ch/~maechler/ Seminar fuer Statistik, ETH-Zentrum LEO C16 Leonhardstr. 27 ETH (Federal Inst. Technology) 8092 Zurich SWITZERLAND phone: x-41-1-632-3408 fax: ...-1228 <>< -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help 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-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._