[Rcpp-devel] RcppMAtrix<complex> ?

Romain Francois romain at r-enthusiasts.com
Thu Apr 22 13:17:52 CEST 2010

Thank you for reposting here.

It is not trivial to see what is happening in your example, so I'll just 
give you some tools.

The old api (which the classicRcppMatrixExample example uses) does not 
have support for complex vectors or matrices.

The new api does have support for complex vectors and complex matrices. 
The unit test file runit.ComplexVector.R does indeed contain some very 
basic examples of using ComplexVector, but not complex matrices. 
However, you can use Rcpp::ComplexMatrix.

Here is an example that calculates the sum of the real part of the 
elements of a complex matrix diagonal and the sum of the imaginary part:

require( Rcpp )
require( inline)

fx <- cfunction( signature( x = "matrix" ), '
	/* grab the R object as a complex matrix */
	ComplexMatrix m(x) ;
	double re_sum = 0.0 ;
	double im_sum = 0.0 ;
	for( int i=0; i<m.ncol(); i++){
		re_sum += m(i,i).r ;
		im_sum += m(i,i).i ;
	return List::create(
		_["sum real part"] = re_sum,
		_["sum imag part"] = im_sum
		) ;
', Rcpp = TRUE, includes = "using namespace Rcpp;" )

x <- diag( (1-2i)*1:5 )
fx( x )

Let us know if this gives you enough to get started.


Le 22/04/10 12:59, baptiste auguie a écrit :
> Dear all,
> I'm hoping to port some R code to C++ to make it faster. The code
> makes heavy use of matrices of complex numbers, and playing with the
> RcppExamples package this morning I got the impression that it's not
> currently implemented in Rcpp. I basically took the example from
> classicRcppMatrixExample and tried to change the types from double to
> complex. I must confess that I don't know C++, so maybe I missed
> something obvious.
> Attached is my dummy example, as well as the R code I'm trying to port
> to give you an idea. Any suggestions would be greatly appreciated!
> Best regards,
> baptiste
> PS: This message initially failed to reach the list; in the meantime I
> got the suggestion from Romain and Dirk to have a look at the class
> Rcpp::ComplexMatrix and the example in runit.ComplexVector.R

Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30
|- http://bit.ly/9aKDM9 : embed images in Rd documents
|- http://tr.im/OIXN : raster images and RImageJ
|- http://tr.im/OcQe : Rcpp 0.7.7

More information about the Rcpp-devel mailing list