[Rcpp-devel] Multiplication of ComplexVector?

Dirk Eddelbuettel edd at debian.org
Tue Aug 17 03:24:52 CEST 2010


On 16 August 2010 at 19:43, Dirk Eddelbuettel wrote:
| Hi Christian,
| 
| Thanks for your interest in Rcpp, and for posting here.
| 
| On 16 August 2010 at 17:19, Christian Gunning wrote:
| | Dear list,
| | 
| | I'm trying to use the ComplexVector class, and I'm having difficulty
| | with multiplication. I sugar multiplication, element-by-element
| | multiplication (no * operator for either), and using temporary
| | Rcomplex variables (don't really understand what I'm doing here).
| 
| Well, NumericVector et al aren't really made for all possible math ops done
| on vectors, real or complex. They are first and foremost storage types. But
| for the math we do have Armadillo and RcppArmadillo. So in the short run, I
| would suggest the following two-step procedure:
| 
|    i)   have a good look at Armadillo (http://arma.sf.net) and use the cx_mat
|         or cx_vec types for mulitplication; write a small test program to see
|         that everything works
| 
|    ii)  use RcppArmadillo to from R via Rcpp / RcppArmadillo to this
|         functionality
| 
| | I was able to successfully import get("*") as an Rcpp::Function and do
| | the multiplication through R. Is this the best option for now?
| 
| I am sure we can do better than this but I am not sure all pieces are in
| place just yet.  

Looks like I wasn't all that far off.  This builds:

  library(inline)
  
  code <- '
      ComplexVector y1(i), y2(ii);
      //arma::cx_vec a1(y1.begin(), y1.size(), false);
      //arma::cx_vec a2(y2.begin(), y2.size(), false);
      arma::cx_vec a1(y1.size());
      arma::cx_vec a2(y2.size());
      arma::cx_vec a3 = a1 * a2;
      arma::cx_vec a4 = a1 + a2;
      List z = List::create(a1, a2, a3, a4);
      return z;
      '
  
  fun <- cxxfunction(signature(i="compex", ii="comples"), code, plugin="RcppArmadillo")
  
The only trouble is that nobody has written the corresponding 'glue' code to
make

      arma::cx_vec a1(y1.begin(), y1.size(), false);

happen: create an Armadillo complex vector from an Rcpp::ComplexVector.  We
can init by scalar size, what you'd need to insert for now is a simply (and
very pedestrian) copy-loop.

Hth, Dirk

  
| 
| Cheers, Dirk
| 
| 
| | Thanks,
| | Christian Gunning
| | University of New Mexico
| | 
| | // doesn't work
| | SEXP rcpp_hello_world(SEXP i, SEXP ii){
| |     using namespace Rcpp ;
| | 
| |     ComplexVector y1(i), y2(ii);
| |     int n(y1.size()), j;
| |     ComplexVector y3(n), y4(n);
| |     y3 = y1 * y2;  // no operator
| |     for (j = 0; j++; j<n) {
| |         y4[j] = y1[j] + y2[j]; // no operator
| |     }
| |     List z            = List::create( y1, y2, y3) ;
| |     return z ;
| | }
| | 
| | // does work, doing multiplication in R
| | SEXP mymult(SEXP input1, SEXP input2) {
| |     Rcpp::Environment base("package:base");
| |     Rcpp::Function funbase = base["get"];
| |     SEXP funbase1 = funbase("*");
| |     Rcpp::Function funmult(funbase1);
| |     Rcpp::ComplexVector xxa = funmult(input1, input2);
| |     return xxa;
| | }
| | 
| | SEXP rcpp_hello_world(SEXP i, SEXP ii){
| |     using namespace Rcpp ;
| |     ComplexVector y3( mymult(i, ii) );
| |     return y3 ;
| | }
| | _______________________________________________
| | Rcpp-devel mailing list
| | Rcpp-devel at lists.r-forge.r-project.org
| | https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
| 
| -- 
| Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com
| _______________________________________________
| Rcpp-devel mailing list
| Rcpp-devel at lists.r-forge.r-project.org
| https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel

-- 
Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com


More information about the Rcpp-devel mailing list