[Rcpp-devel] Using complex numbers in a C program called by R

Dirk Eddelbuettel edd at debian.org
Sun Jan 3 22:14:13 CET 2016


On 4 January 2016 at 09:50, Baptiste Auguie wrote:
| Hi,
| 
| Just to clarify: did you include files from RcppFaddeeva because you need some
| of its functionality (special complex error functions), or was it only a
| workaround to get access to complex numbers? In the latter case, I recommend
| you try making a minimal Rcpp package to see how easy it is to interface with
| C++ functions, and that way you will only have relevant header files included.

+1

I already responded to Pierre's initial emails in November and tried then to
explain to him that that .C() is a _really bad idea at this point in time.

It is two months later and nothing has changed.

So here is a quick illustration of what Baptiste meant. All it takes is

   library(Rcpp)    # no other depends

and then (and I even got this right on first try):

   R> cppFunction("Rcomplex addTwo(Rcomplex x, Rcomplex y) {return x + y; }")
   R> addTwo(2+2i, 3+3i)
   [1] 5+5i
   R> 

With one invocation of cppFunction() I created an ad-hoc compiled function
(technically compiled as C++ but you can call this C as well) which adds two
complex number -- one of Pierre's request as per the email below.  And low
and behold is just does that.

So yes -- maybe time to learn some Rcpp, maybe forget about .C() and simply
get on with this and other other things.

Dirk

| 
| Best,
| 
| baptiste
| 
| 
| 
| On 4 January 2016 at 09:36, Pierre Lafaye de Micheaux <lafaye at dms.umontreal.ca>
| wrote:
| 
|     Dear all,
| 
|     This email comes after a discussion on the R-pkg-devel mailing list:
|     https://stat.ethz.ch/pipermail/r-package-devel/2016q1/000627.html
| 
|     My purpose was to be able, in two of my packages, to use complex numbers
|     (create some, addition, multiplication, division, modulus, etc)
|     directly in a C code that is called from R (using the .C interface).
|     Note that these complex numbers might not come from R but could be created
|     and used directly in my C code, that will then output (via pointers)
|     real (I mean double) values back to R (via the .C interface). I could also
|     send from R these complex numbers via the .C interface.
| 
|     A very simple example of such a function called from R via the .C interface
|     could be the following:
| 
|     #include <R.h>
|     #include "Rmath.h"
|     extern "C" {
|     void Cfunc(complex double *var) {
|         double _Complex z = 1.0 + 2.0 * _Complex_I;
|         var[0] = z + exp(var[0]);
|     return;
|     }}
| 
|     I could call this function from R as follows:
|     .C(1i)
| 
|     No problem so far when I use such a function in a package that is compiled
|     under Linux. But this will not work under windows (see the discussion
|     on the R-pkg-devel list). So what I did to make everything work under
|     Windows also was to include in the src/ directory of my package the source
|     files
|     from the RcppFaddeeva package. Then I would modify the function above as
|     follows:
| 
|     #include <R.h>
|     #include "Rmath.h"
|     #include "libraries/callFaddeeva.cpp"
|     #include "libraries/Faddeeva.cpp"
|     #include "libraries/RcppExports.cpp"
|     extern "C" {
|     void Cfunc(complex double *var) {
|         cmplx z = C(1.0, 2.0);
|         var[0] = z + cexp(var[0]);
|     return;
|     }}
| 
|     Maybe there is a way not to include all the Faddeeva source files in my
|     packages? But I do not know how to do it.
| 
|     Best regards,
| 
|     Pierre L.
| 
| 
| 
|     --
|     Pierre Lafaye de Micheaux
| 
|     Adresse courrier:
|     Université de Montréal
|     Pavillon André-Aisenstadt
|     Département de Mathématiques et Statistique
|     CP 6128 Succursale Centre-ville
|     Montréal Qc H3C 3J7
|     CANADA
| 
|     Adresse physique:
|     Département de Mathématiques et Statistique
|     Bureau 4249, Pavillon André-Aisenstadt
|     2920, chemin de la Tour
|     Montréal, Québec H3T 1J4
|     CANADA
| 
|     Tél.: (00-1) 514-343-6607 / Fax: (00-1) 514-343-5700
|     lafaye at dms.umontreal.ca
|     http://www.biostatisticien.eu
| 
|     _______________________________________________
|     Rcpp-devel mailing list
|     Rcpp-devel at lists.r-forge.r-project.org
|     https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
| 
| 
| _______________________________________________
| Rcpp-devel mailing list
| Rcpp-devel at lists.r-forge.r-project.org
| https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel

-- 
http://dirk.eddelbuettel.com | @eddelbuettel | edd at debian.org


More information about the Rcpp-devel mailing list