2

I often use Rcpp code to incorporate C++ code into R. Through the BH-package I am also able to use the Boost-library. However, the Boost library lacks a function that I would like to use (to be precise, it only has Bessel function but I would like to get Log-Bessel immediately because of overflow). I know that Alglib does have this feature.

Would it be possible to use Alglib with Rcpp, that is, use the log-bessel function from Alglib somehow?

Richie Cotton
  • 118,240
  • 47
  • 247
  • 360
Kees Mulder
  • 490
  • 3
  • 8
  • 2
    The question "is it possible" is obviously "yes", since Alglib is C++. There isn't a wrapper already that I know of, so how to do that is too broad for SO, unless you have specific problems. Whether you need to do it is another question: are R's Bessel functions not OK http://www.inside-r.org/r-doc/base/bessel? The alglib Bessel functions look very similar http://www.alglib.net/translator/man/manual.cpp.html#unit_bessel – Richie Cotton Jan 05 '15 at 09:01
  • Is the question how to use some package for C++ in Rcpp too broad for SO? In that case, I am sorry, but I really do not know where I could find information that would help me get the Alglib library working within Rcpp. The R functions would be ok, but as far as I am aware I would not be able to use the R function within my Rcpp-based C++ code because that requires the developers of Rcpp to add this function to Rcpp. – Kees Mulder Jan 05 '15 at 09:09
  • You can of course call any R function (easily) in Rcpp code, with the obvious overhead cost. – Roland Jan 05 '15 at 09:46
  • I can? I was under the impression that I could only call specific 'sugar' functions from your C++, which I thought because the [Quick Reference](http://cran.r-project.org/web/packages/Rcpp/vignettes/Rcpp-quickref.pdf) and [Hadley's guide](http://adv-r.had.co.nz/Rcpp.html) seem to imply this. How would I call an arbitrary R function from C++? – Kees Mulder Jan 05 '15 at 09:57
  • http://gallery.rcpp.org/articles/r-function-from-c++/ – Roland Jan 05 '15 at 10:05
  • Thanks so much! I suppose I could pass the log-bessel function like this. If I get this right, though, the overhead would almost certainly be crippling for my code, so that Alglib would still be by far preferable, but I'll do some tests. Thanks anyway. – Kees Mulder Jan 05 '15 at 10:45
  • 1
    Well, Rcpp is for *extending* R with C++ code. Why don't you just create a package interfacing Alglib, or just carry the one Bessel function over? – Dirk Eddelbuettel Jan 05 '15 at 12:31
  • Both of those things seem like good ideas, but I don't have enough experience with Rcpp to come up with or implement either of those currently. Carrying only the Log-Bessel function over seems difficult because there seem to be lots of dependencies that lead I-don't-know-where. Would love to read up on both options more, though. I suppose I will try to go through the documentation again to find more about creating packages for interfacing. Thanks, though! – Kees Mulder Jan 05 '15 at 17:00

1 Answers1

2

I do not see a clear difference in functionality between the

As such, I think you can just use the BH package giving you all of Boost Math and then some.

Last but not least there is a package bessel on CRAN written by the R Core member focusing on special functions so you could start from there too.

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
  • Thanks! You are right, I was misinformed that AlgLib contained a LogBessel function, this seems to be openTURNS instead. A bit embarrassing, I must admit. I was using the BH package before, but I was running into overflow issues, which is why I wanted to get a package that would allow Log-Bessel. I will look into the R package as well. Thanks! – Kees Mulder Jan 05 '15 at 17:17