[Rcpp-devel] Multiplication of ComplexVector?

Romain Francois romain at r-enthusiasts.com
Wed Aug 18 09:10:48 CEST 2010


Yep, that is because of RcppExport. You only use RcppExport when you 
call the function from .Call in R.

RcppExport is an alias to extern "C" so it essentially voids the 
C++-ness of the function it is applied to.

Romain

Le 18/08/10 09:05, Christian Gunning a écrit :
> I'm using a package generated by Rcpp.package.skeleton.  No C files.
> Upon reflection, I'm not entirely sure I have the header right,
> though.
>
> ///////
> // rcpp_operators.h:
> ///////
> #ifndef _Rcwttest_RCPP_OPERATORS_H
> #define _Rcwttest_RCPP_OPERATORS_H
> #include<Rcpp.h>
> RcppExport Rcomplex operator*(const Rcomplex&, const Rcomplex&);
> RcppExport Rcomplex operator*(const Rcomplex&, const double&);
> #endif
>
> ///////
> // rcpp_operators.cpp
> ///////
> #include "rcpp_operators.h"
> Rcomplex operator*( const Rcomplex&  lhs, const double&  rhs){
>         Rcomplex y ;
>         y.r = lhs.r * rhs;
>         y.i = lhs.i * rhs ;
>         return y ;
> }
> Rcomplex operator*( const Rcomplex&  lhs, const Rcomplex&  rhs){
>         Rcomplex y ;
>         y.r = lhs.r * rhs.r - lhs.i * rhs.i ;
>         y.i = lhs.r * rhs.i + rhs.r * lhs.i ;
>         return y ;
> }
>
>
> Compiler output (from "R CMD build Rcwttest; R CMD INSTALL Rcwttest;"):
>
> g++ -I/usr/share/R/include
> -I"/usr/local/lib/R/site-library/Rcpp/include"   -fpic  -g -O2 -c
> rcpp_cwt_thierry.cpp -o rcpp_cwt_thierry.o
> In file included from rcpp_cwt_thierry.cpp:2:
> rcpp_operators.h:7: error: declaration of C function ‘Rcomplex
> operator*(const Rcomplex&, const double&)’ conflicts with
> rcpp_operators.h:6: error: previous declaration ‘Rcomplex
> operator*(const Rcomplex&, const Rcomplex&)’ here
>
>
> -xian
>
> On Tue, Aug 17, 2010 at 11:30 PM, Romain Francois
> <romain at r-enthusiasts.com>  wrote:
>> Le 18/08/10 08:19, Romain Francois a écrit :
>>>
>>> Le 18/08/10 07:44, Christian Gunning a écrit :
>>>>
>>>> Thanks to all for the helpful suggestions. I was excited to learn
>>>> about Armadillo's plans for fft/ifft -- my original interest in Rcpp
>>>> was to facilitate the R fft call from C...
>>>>
>>>> For conceptual simplicity, I'm opting for manually adding an operator*
>>>> for now.
>>>
>>> Note that they will appear in the next version of Rcpp, which is
>>> scheduled (loosely) for the end of august.
>>>
>>>> Benchmarks show a ~10% speedup over sending large vectors (2
>>>> million) to R for multiplication.
>>>>
>>>> One question that I ran into that I don't quite understand -- there
>>>> are several permutations of an Rcomplex-returning operator*, depending
>>>> upon the type of lhs and rhs (e.g Rcomplex and Rcomplex; Rcomplex and
>>>> double; etc.). I admit, the template system is over my head for now,
>>>> and this has a low priority for me, but perhaps there's a simple
>>>> explanation asides from using separate operator symbols?
>>>>
>>>> Here are 2 definitions that vary in input type:
>>>>
>>>> Rcomplex operator*( const Rcomplex&  lhs, const Rcomplex&  rhs){
>>>> Rcomplex y ;
>>>> y.r = lhs.r * rhs.r - lhs.i * rhs.i ;
>>>> y.i = lhs.r * rhs.i + rhs.r * lhs.i ;
>>>> return y ;
>>>> }
>>>>
>>>> Rcomplex operator*( const Rcomplex&  lhs, const double&  rhs){
>>>> Rcomplex y ;
>>>> y.r = lhs.r * rhs ;
>>>> y.i = lhs.i * rhs ;
>>>> return y ;
>>>> }
>>>>
>>>> yielding the following compiler error:
>>>>
>>>> rcpp_operators.h:7: error: declaration of C function ‘Rcomplex
>>>> operator*(const Rcomplex&, const double&)’ conflicts with
>>>> rcpp_operators.h:6: error: previous declaration ‘Rcomplex
>>>> operator*(const Rcomplex&, const Rcomplex&)’ here
>>>>
>>>> best,
>>>> Christian
>>>
>>> Do you include this from a .c file or a .cpp file ? C has no concept of
>>> overloading.
>>>
>>> Can you share a small reproducible example that shows the problem.
>>>
>>> Romain
>>
>> It seems to work fine for me :
>>
>> require( inline )
>> require( Rcpp )
>>
>> inc<- '
>> Rcomplex operator*( const Rcomplex&  lhs, const Rcomplex&  rhs){
>>         Rcomplex y ;
>>         y.r = lhs.r * rhs.r - lhs.i * rhs.i ;
>>         y.i = lhs.r * rhs.i + rhs.r * lhs.i ;
>>         return y ;
>> }
>> Rcomplex operator*( const Rcomplex&  lhs, const double&  rhs){
>>        Rcomplex y ;
>>        y.r = lhs.r * rhs ;
>>        y.i = lhs.i * rhs ;
>>        return y ;
>> }
>>
>> '
>> fx<- cxxfunction( signature( ), '
>> using namespace Rcpp ;
>>     Rcomplex x, y ;
>>     x.r = 2.0 ; x.i = 2.0 ;
>>     y.r = 1.0 ; y.i = 1.0 ;
>>
>>     Rcomplex z = x * y ;
>>     Rcomplex a = x * 3 ;
>>     return ComplexVector::create( x, y, z, a ) ;
>>
>> ', plugin = "Rcpp", includes = inc )
>>
>>
>>
>>> fx()
>> [1] 2+2i 1+1i 0+4i 6+6i
>>
>> so I'm staying with my guess that this is a C vs C++ issue.
>>
>> Romain

-- 
Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30
http://romainfrancois.blog.free.fr
|- http://bit.ly/bzoWrs : Rcpp svn revision 2000
|- http://bit.ly/b8VNE2 : Rcpp at LondonR, oct 5th
`- http://bit.ly/aAyra4 : highlight 0.2-2



More information about the Rcpp-devel mailing list