# [Rcpp-devel] Multiplication of ComplexVector?

Romain Francois romain at r-enthusiasts.com
Wed Aug 18 08:30:12 CEST 2010

```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
>
> 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

```