[Rcpp-devel] use of auxiliary functions

baptiste auguie baptiste.auguie at googlemail.com
Fri Aug 6 22:14:34 CEST 2010


Dear all,

In a package using RcppArmadillo I define several functions that use
the same piece of c++ code. For instance, several functions use a
rotation matrix constructed from 3 angles. I am considering the
following idea: 1) define a routine that performs the calculations; 2)
create a wrapper in case I want to access it at R level; 3) simply
call this routine in other c++ functions. The obvious advantage is
that I minimize code duplication and errors associated with it. It is
perhaps best illustrated with the code,

  arma::mat euler(double phi, double theta, double psi)
  {
    arma::mat Rot(3,3);
    Rot(0,0) = cos(psi)*cos(phi) - cos(theta)*sin(phi)*sin(psi);
    Rot(0,1) = cos(psi)*sin(phi) + cos(theta)*cos(phi)*sin(psi);
    Rot(0,2) = sin(psi)*sin(theta);

    Rot(1,0) = -sin(psi)*cos(phi) - cos(theta)*sin(phi)*cos(psi);
    Rot(1,1) = -sin(psi)*sin(phi) + cos(theta)*cos(phi)*cos(psi);
    Rot(1,2) = cos(psi)*sin(theta);

    Rot(2,0) = sin(phi)*sin(theta);
    Rot(2,1) = -cos(phi)*sin(theta);
    Rot(2,2) = cos(theta);
    return (Rot);
  }

// this is the wrapper for access at R level
  SEXP rotation(SEXP Iphi, SEXP Itheta, SEXP Ipsi)
  {
    try {

      // conversion of input variables
      double phi  =  as<double>(Iphi), theta  =  as<double>(Itheta),
psi  =  as<double>(Ipsi);
      arma::mat Rot= euler(theta, phi, psi);
      List res ;
      res["R"] = Rot;

      return res ;

    } catch( std::exception &ex ) {
      forward_exception_to_r( ex );
    } catch(...) {
      ::Rf_error( "c++ exception (unknown reason)" );
    }
    return R_NilValue; // -Wall
  }

// and perhaps
// other SEXP functions using euler()
// ...

I am wondering if there is any downside to this approach, in terms of
efficiency. In particular, when rotation() calls euler(), is it making
copies of the variables in memory? Should I use pointers instead? (I
have forgotten the most basic things about them).
I have made a quick, inconclusive timing comparison of both
techniques; on this particular example the cost would be negligible (I
have other routines that use large matrices, however).


Regards,

baptiste


More information about the Rcpp-devel mailing list