Skip to content

[R-pkg-devel] Problem (with complex.h?) under Microsoft Windows

6 messages · Pierre Lafaye de Micheaux, Dirk Eddelbuettel, Roebuck,Paul L +2 more

#
Dear all,

I created a new version of the package IndependenceTests where I call 
some C and Fortran functions (using the .C() or .Fortran() interface).
It works perfectly under Linux, with gcc. No problem with R CMD check. I 
would like to upload this package on the CRAN.

But when I first tried to use winbuilder, I got an error with the 
following message:

* installing *source* package 'IndependenceTests' ...
** libs

*** arch - i386
g++  -I"D:/RCompile/recent/R/include" -DNDEBUG 
-I"d:/RCompile/r-compiling/local/local320/include"     -O2 -Wall 
-mtune=core2 -c Cnhat.cpp -o Cnhat.o
Cnhat.cpp:9:89: error: 'complex' has not been declared
Cnhat.cpp:9:105: error: two or more data types in declaration of 'res'
....
.....
etc.


I think I understand that there is a problem (only under Windows though) 
with the complex type.
The beginning of my file Cnhat.cpp is as follows:

1. #include <R.h>
2. #include "Rmath.h"
3. #include <complex.h>
4. #include <iostream>
5. using namespace std;
6.
7. extern"C" {
8.
9.   void CnhatC(double *vecs, double *vect, double *X, int *n, int *q, 
int *p, int *vecd, complex double *res) {
10.
11.    void phinhatReturn(double *vect1, double *vect2, double *vect3, 
double *X, int *q, int *n, double _Complex *res1, double _Complex *res2, 
double _Complex *res3);
12.    double _Complex tmp2, prod1 = 1.0 + 0.0*_Complex_I, prod2 = 1.0 + 
0.0*_Complex_I, somme = 0.0 + 0.0*_Complex_I;
13.    int indbeginblocl, l, i, *vecdl, k, j;
14.    double *vecsl, *mvectl, *diffvecsvectl, *Xl;
15.    double _Complex *res1, *res2, *res3;
16.    vecdl = new int[1];
17.    res1 =  new _Complex double[1];
....
.....
etc.

As you can see, there seem to be some problem (on line 9.) with: complex 
double *res

Could you please point me to something (e.g., another package) that 
might help me solve the problem? If possible, I would like to avoid 
invest too much time using the new .Call() interface because I am quite 
familiar
with the old .C(). And also I am not even sure if it would help solving 
the problem.

Best regards,

Pierre L.
#
On 13 November 2015 at 17:24, Pierre Lafaye de Micheaux wrote:
| might help me solve the problem? If possible, I would like to avoid 
| invest too much time using the new .Call() interface because I am quite 
| familiar with the old .C().

If you pay attention to posts in recent years of the R-devel list (or here)
you will see that .C is now actively discouraged -- eg in a few posts by R
Core member Simon Urbanek.

Dirk
#
Too bad you couldn't just use C++ std::complex, but that's a no-go if your function needs C linkage.

<http://stackoverflow.com/questions/1063406/c99-complex-support-with-visual-studio>
suggests MS-specific topic, but answers explain more.

Have you looked at RCpp package?
#
On 13/11/2015 11:24 AM, Pierre Lafaye de Micheaux wrote:
You should be using Rcomplex in the type declaration. (See Writing R 
Extensions for a discussion of this.)  It is defined as

typedef struct {
	double r;
	double i;
} Rcomplex;

which is likely compatible with your "complex double" on those other 
systems (which are using newer gcc versions), but it's not guaranteed to 
be, even there.

Duncan Murdoch
#
On 13 November 2015 at 20:24, Roebuck,Paul L wrote:
| Too bad you couldn't just use C++ std::complex, but that's a no-go if your function needs C linkage.
| 
| <http://stackoverflow.com/questions/1063406/c99-complex-support-with-visual-studio>
| suggests MS-specific topic, but answers explain more.
| 
| Have you looked at RCpp package?

Rcpp can help quite a bit with all of the mechanics: header files, linking,
loading, conversion, ...  We don't use it all that much for complex data
types but some things certainly work, and Baptiste has a few packages (using
RcppArmadillo) which use complex numbers.

Here is a quick example:

R> library(Rcpp)             # load Rcpp, them define a one-liner in the R shell
R> cppFunction("ComplexVector timesTwo(ComplexVector x) { ComplexVector y = x+x; return y; }")
R> timesTwo(7i)              # use it
[1] 0+14i
R> timesTwo(c(4+2i, 7i))     # vectors or scalars are the same to R
[1] 8+ 4i 0+14i
R> 

This should very much work the same way in Windows.  And note that we didn't
have to specify a single header (though you can of course should you need
extra ones -- see many examples at http://gallery.rcpp.org)

Dirk
1 day later
#
Hi, folks,

I just want to throw in another vote for Rcpp.  I recently rewrote some old code that used the legacy interface to use Rcpp, and life with Rcpp is *much* better.  The code is clean and easy to read (and write), and memory protection is taken care of.  I don't know if it will fix Pierre's specific problem, but if you try it, I suspect you'll like it.

Many thanks to Dirk, Romain, and all.

Cheers,

 - Gord


From: R-package-devel <r-package-devel-bounces at r-project.org<mailto:r-package-devel-bounces at r-project.org>> on behalf of "Roebuck,Paul L" <proebuck at mdanderson.org<mailto:proebuck at mdanderson.org>>
Date: Friday, 13 November 2015 20:24
To: "r-package-devel at r-project.org<mailto:r-package-devel at r-project.org>" <r-package-devel at r-project.org<mailto:r-package-devel at r-project.org>>
Subject: Re: [R-pkg-devel] Problem (with complex.h?) under Microsoft Windows

Too bad you couldn't just use C++ std::complex, but that's a no-go if your function needs C linkage.

<http://stackoverflow.com/questions/1063406/c99-complex-support-with-visual-studio>
suggests MS-specific topic, but answers explain more.

Have you looked at RCpp package?