# [Rcpp-devel] Regression in conversion from NumericVector to ComplexVector?

Christian Gunning xian at unm.edu
Wed Dec 22 06:23:42 CET 2010

```The so-simple-i-missed it fix to my original question is here, though
i'm not quite sure how/why:

test1 = cxxfunction(, '
NumericVector aa(10, 1.5);
// bb = aa  // first attempt
ComplexVector bb(aa);   // working
return(bb);
', plugin='Rcpp'
)

> Just to follow up some more:  I just played with a few lines of Armadillo
> code and it seems pretty clear.  To create a complex matrix, you need two
> standard ones. So no implicit conversions.

Thanks for the example.  I'm focusing on ComplexVector right now, but
it's an interesting comparison - essentially opposite of ComplexVector
(as above).

> Now, on a strictly personal level: I haven't had a use case for complex
> numbers since the last time it came up in graduate school, so I am unlikely
> to push this cart very far for you.

Understood.  I use them regularly doing frequency-domain work.  I'm
starting to understand the Rcomplex-sugar interface, and I'm happy to
work on it.

A few unit tests of instantiating a ComplexVector are attached.  I
also discovered the following strange bug where a complex-valued
function changes its argument.

test1 = cxxfunction(signature(vec='complex'), '
ComplexVector x(vec) ;
int nn = x.size();
for( int i=0; i<nn; i++) {
x[i].r = x[i].r*2 ;
x[i].i = x[i].i*2 ;
}
return x ;
', plugin='Rcpp')

aa1 =  (0:9)*(1+1i)
aa2 =  (0:9)*(1+1i)
all.equal(aa1,aa2)  ## true
bb = test1(aa1)
all.equal(aa1,aa2)  ## false
-------------- next part --------------
Index: runit.Vector.R
===================================================================
--- runit.Vector.R	(revision 2803)
+++ runit.Vector.R	(working copy)
@@ -95,21 +95,41 @@
return x ;
'
),
-        	"complex_INTSXP" = list(
+        	"complex_CPLXSXP" = list(
signature(vec = "complex" ),
'
ComplexVector x(vec) ;
-				for( int i=0; i<x.size(); i++) {
+                int nn = x.size();
+				for( int i=0; i<nn; i++) {
x[i].r = x[i].r*2 ;
x[i].i = x[i].i*2 ;
}
return x ;
'
),
+        	"complex_INTSXP" = list(
+        		signature(vec = "integer" ),
+        		'
+				ComplexVector x(vec);
+                int nn = x.size();
+                IntegerVector tmp(nn, 2.0);
+                ComplexVector tmp1(tmp);
+                x = x * tmp1;
+				return x ;
+				'
+        	),
+        	"complex_REALSXP" = list(
+        		signature(vec = "numeric" ),
+        		'
+				ComplexVector x(vec);
+                int nn = x.size();
+                NumericVector tmp(nn, 3.0);
+                ComplexVector tmp1(tmp);
+                x = x * tmp1;
+				return x ;
+				'
+        	),

-
-
-
"integer_ctor"=list(
signature(),
'IntegerVector x(10) ;
@@ -727,11 +747,22 @@
funx <- .rcpp.Vector\$complex_
checkEquals( funx(), 0:9*(1+1i), msg = "ComplexVector" )
}
-
+test.ComplexVector.CPLXSXP <- function(){
+	funx <- .rcpp.Vector\$complex_CPLXSXP
+     vv = (0:9)*(1+1i) ## not working - funx changes its argument
+	#checkEquals( funx(vv), 2*vv, msg = "ComplexVector( CPLXSXP) " )
+	checkEquals( funx((0:9)*(1+1i)), 2*(0:9)*(1+1i), msg = "ComplexVector( CPLXSXP) " )
+}
test.ComplexVector.INTSXP <- function(){
funx <- .rcpp.Vector\$complex_INTSXP
-	checkEquals( funx(0:9*(1+1i)), 2*0:9*(1+1i), msg = "ComplexVector( CPLXSXP) " )
+    vv <- 0L:9L
+	checkEquals( funx(vv), (2+0i)*vv, msg = "ComplexVector( INTSXP) " )
}
+test.ComplexVector.REALSXP <- function(){
+	funx <- .rcpp.Vector\$complex_REALSXP
+    vv <- as.numeric(0:9)
+	checkEquals( funx(vv), (3+0i)*vv, msg = "ComplexVector( REALSXP) " )
+}
if( Rcpp:::capabilities()[["initializer lists"]] ){
test.ComplexVector.initializer.list <- function(){
funx <- .rcpp.Vector\$complex_initializer_list
```