[Rcpp-devel] Filling NumericMatrix with NumericVectors with apply by row/column?

Romain Francois romain at r-enthusiasts.com
Wed Sep 8 11:41:45 CEST 2010


Le 08/09/10 11:24, Christian Gunning a écrit :
> On Tue, Sep 7, 2010 at 11:40 PM, Romain Francois
> <romain at r-enthusiasts.com>  wrote:
>> Le 08/09/10 06:07, Christian Gunning a écrit :
>>>
>>> Yes, I see the reinvent-the-wheel problem, especially after combing
>>> through rcpp-devel archives some more.  I admit that I don't well
>>> understand the relative costs/benefits of extending the R proxy class
>>> versus outsourcing to Armadillo.  Two examples that come to mind are
>>> Rcpp::Array and Rcpp::DataFrame.
>>>
>>> In the first case, could I conceivably turn an input 3D SEXP array
>>> into, e.g., NumericVector( Dimension(2,3,4) ), create an arma::cube
>>> out of it, process it and return?
>>
>> I think so. I have not tried though ... have you ?
>
> No, not yet.  I just now went mucking about through the armadillo
> classes (and, by extension, the rcpp-devel archives) at Dirk's
> suggestion.

I'm sure people will be interested about the results of this experiment :-)

>>> In the second case, there's a column level but no row-level accessor
>>> for the DataFrame (that I could find), nor is there a mapping into
>>> Armadillo. I get the impression that DataFrame is more of a
>>> convenience class for *returning* data rather than processing it?
>>
>> Yes. It makes it easy to create a data frame and access it by column.
>>
>> We'd be open to extend the interface if you provide some sort of design of
>> the sort of code you'd want to write.
>
> Yeah, I apologize - I'm just at the edge of my experience here.  I
> spent the last few days learning proper C++ OOP writing (as opposed to
> reading) in response to your earlier comment about references.  I
> admit, templates are still magic to me.

that's alright. templates can do amazing things, but can also give 
persistant headaches.

> I guess I'm still curious about the fundamental design goals.

Me too

> RcppArmadillo allows us to easily use armadillo's rich object model,
> with a wealth of object-specific methods, e.g.:
>
> mat A = randu<mat>(5,10);
> mat B = ones<mat>(5,2);
> // at column 2, insert a copy of B;
> A.insert_cols(2, B);
>
> Personally, R's indexing is one of my favorite parts of the language
> (and C++ indexing my least). With this in mind, I've wondered about
> the possibility of rolling some sort of generic Index class that
> contains enough information to do (as above):
>
> A(aColIndex) = B;

We have started very shy steps towards this. See for example the 
convolve5_cpp.cpp file :

RcppExport SEXP convolve5cpp(SEXP a, SEXP b) {
     NumericVector xa(a); int n_xa = xa.size() ;
     NumericVector xb(b); int n_xb = xb.size() ;
     NumericVector xab(n_xa + n_xb - 1,0.0);

     for(int i=0; i<n_xa; i++){
     	xab[ Range(i, i+n_xb-1) ] += xa[i] * xb ;
     }
     return xab ;
}

as opposed to the usual implementation:

RcppExport SEXP convolve3cpp(SEXP a, SEXP b){
     Rcpp::NumericVector xa(a);
     Rcpp::NumericVector xb(b);
     int n_xa = xa.size() ;
     int n_xb = xb.size() ;
     int nab = n_xa + n_xb - 1;
     Rcpp::NumericVector xab(nab);

     for (int i = 0; i < n_xa; i++)
         for (int j = 0; j < n_xb; j++)
             xab[i + j] += xa[i] * xb[j];

     return xab ;
}

would be interesting to go beyond Range and have a more flexible system.

> So, if the overall goal is Simple/Reads-more-like-C++, then it's a
> moot point.  If something like this is worth doing, is it worth doing
> for a variety of Rcpp classes?  I admit that i'm not entirely clear on
> the cost side of the "worth" equation here :)

Me neither (although I might have a better idea).

We want C++ code to look more like R code, yet be more efficient, so 
there is scope for such things. It will be easier to come up with an 
estimate when you hand the design document. Then we can decide if this 
is something we want to do upfront, something for which we will need 
extra help (patches, someone's time, funding to get some extra time from 
us, etc ...), something we might do later ...

Romain

> Christian
>
>> Romain
>>
>>> best,
>>> Christian
>>>
>>> On Tue, Sep 7, 2010 at 5:48 PM, Dirk Eddelbuettel<edd at debian.org>    wrote:
>>>>
>>>> On 7 September 2010 at 17:26, Christian Gunning wrote:
>>>> | I was thinking about this today, and I wondered if getter/setter
>>>> | functions for NumericMatrix, along with MatrixIndex classes might make
>>>> | any sense, as opposed to sugar? Something like this:
>>>> |
>>>> | SEXP foo1( int n ){
>>>> |   NumericVector x(n);
>>>> |   MatrixIndex i(1);  // row index
>>>> |   NumericMatrix xx(n,n) ;
>>>> |   // possible to assign by row or column?
>>>> |   for (i.index =0; i.index<    n; i.index++) {
>>>> |     xx.Setter(i) = zeros(n) ;
>>>> |   }
>>>> |   return(xx)
>>>> | }
>>>> |
>>>> | class MatrixIndex:
>>>> | {
>>>> |   // use apply()'s convention of 1=row, 2=col
>>>> |   // row/col specification is set on creation
>>>> |   private:
>>>> |     int m_rowcol;
>>>> |   public:
>>>> |     int index;
>>>> |     MatrixIndex( int rowcol, int i=0) {
>>>> |       m_rowcol = rowcol;
>>>> |       index = i;
>>>> |     }
>>>> | };
>>>>
>>>> I have to admit that questions like this always leads to _numeric
>>>> classes_
>>>> such as Armadillo or NewMat rather than our R proxy classes.  Did you see
>>>> what Armadillo:  http://arma.sourceforge.net/docs.html#insert_rows
>>>>
>>>> Would that help?  Passing matrices from R via Rcpp to Armadillo (and
>>>> back) is
>>>> pretty easy.
>>>>
>>>> Cheers, Dirk
>>>>
>>>> PS We are a little behind wrapping Armadillo 0.9.70 in RcppArmadillo but
>>>> we
>>>> will get there "soon".
>>>>
>>>>
>>>> | best,
>>>> | Christian
>>>> |
>>>> | On Sun, Aug 22, 2010 at 5:23 AM, Romain Francois
>>>> |<romain at r-enthusiasts.com>    wrote:
>>>> |>    Hello,
>>>> |>
>>>> |>    There currently is no sugar facility to generate a matrix the way you
>>>> want.
>>>> |>    The last option is probably the best thing to do for now.
>>>> |>
>>>> |>    Perhaps outer can help you :
>>>> |>
>>>> |>    NumericVector xx(x) ;
>>>> |>    NumericVector yy(y);
>>>> |>    NumericMatrix m = outer( xx, yy, std::plus<double>() ) ;
>>>> |>    return m ;
>>>> |>
>>>> |>    Romain
>>>> |>
>>>> |>    Le 21/08/10 23:13, Christian Gunning a écrit :
>>>> |>>
>>>> |>>    Dear list,
>>>> |>>
>>>> |>>    I'm amazed at the ability to use the apply family in Rcpp.  Yet I'm
>>>> still
>>>> |>>    unsure of the best way to assign NumericVector objects into
>>>> |>>    NumericMatrix objects.  Must this be done element-by-element, or is
>>>> |>>    there something equivalent to R's MyMatrix[,1] = MyColVector?
>>>> |>>    (As an aside, are both mymatrix[i,j] and mymatrix(i,j) equivalent?
>>>>   It
>>>> |>>    seems that I've seen them used interchangably on the list.)
>>>> |>>
>>>> |>>    A simple example of what I'm aiming for:
>>>> |>>    Make an n*n matrix, and use sapply to perform a vector operation by
>>>> |>>    row, here constructing a vector of length n full of zeros.
>>>> |>>
>>>> |>>    // a simple vector-returning function
>>>> |>>    NumericVector zeros( int n){
>>>> |>>      NumericVector ret(n);
>>>> |>>      ret = 0;
>>>> |>>      return ret;
>>>> |>>    }
>>>> |>>
>>>> |>>    // sapply version, doesn't work but is easy to read
>>>> |>>    SEXP foo( int n ){
>>>> |>>      NumericVector x(n);
>>>> |>>      x = n;
>>>> |>>      NumericMatrix xx(n,n) ;
>>>> |>>      // possible to assign by row or column?
>>>> |>>      xx = sapply( x, zeros ) ;
>>>> |>>      return(xx);
>>>> |>>    }
>>>> |>>
>>>> |>>    // the looped version, where xx[,i] is not syntactically valid
>>>> |>>    SEXP foo1( int n ){
>>>> |>>      NumericVector x(n);
>>>> |>>      int i;
>>>> |>>      NumericMatrix xx(n,n) ;
>>>> |>>      // possible to assign by row or column?
>>>> |>>      for (i =0; i<n; i++) {
>>>> |>>        xx[,i] = zeros(n) ;
>>>> |>>      }
>>>> |>>      return(xx)
>>>> |>>    }
>>>> |>>
>>>> |>>
>>>> |>>    // syntactically valid, element-wise assignment
>>>> |>>    SEXP foo2( int n ){
>>>> |>>      NumericVector x(n);
>>>> |>>      int i, j;
>>>> |>>      NumericMatrix xx(n,n) ;
>>>> |>>      // possible to assign by row or column?
>>>> |>>      for (i=0; i<n; i++) {
>>>> |>>        x = zeros(n) ;
>>>> |>>        for (j=0;  j<n; j++) {
>>>> |>>          xx(i,j) = x[j]
>>>> |>>        }
>>>> |>>      }
>>>> |>>      return(xx)
>>>> |>>    }
>>>> |>>
>>>> |>>    thanks so much,
>>>> |>>    Christian Gunning
>>>> |>>    --
>>>> |>>    A man, a plan, a cat, a ham, a yak, a yam, a hat, a canal – Panama!
>>>> |>
>>>> |>    --
>>>> |>    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
>>>> |>
>>>> |>
>>>> |
>>>> |
>>>> |
>>>> | --
>>>> | A man, a plan, a cat, a ham, a yak, a yam, a hat, a canal – Panama!
>>>> | _______________________________________________
>>>> | 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

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




More information about the Rcpp-devel mailing list