[Rcpp-devel] redimension help

Dirk Eddelbuettel edd at debian.org
Wed Jun 8 04:42:32 CEST 2011


On 7 June 2011 at 22:09, Silkworth,David J. wrote:
| Further to our personal exchange I have reviewed Romain's recent post in reply to Fatemeh Riahi
| http://lists.r-forge.r-project.org/pipermail/rcpp-devel/2011-May/002256.html
| 
| I've attempted to re-write my simple example in the list of vectors format as shown by Romain.
| 
| src<-'
| int s = 7;  // result of original oversize estimate before process runs
| int c=3;  //known column count established from a list argument (variable to function)
| int r = 4;  // number of rows that more complex process found necessary to fill
| int i=0;
|      Rcpp::List Lmv(c) ;  // list of matrix column vectors
|      for(  i=0; i<c; i++)  {
|          Rcpp::IntegerVector m(s) ;
| // insertion of next three lines partially fills the matrix column vectors
| // the same way as previous sample code
| for( int j=0; j<r; j++){
| m[j]= (i+1)*(j+1);
| }
| Lmv[i]=m;
| }
| // this code does not access the vector elements as I wish
| //for(int j=0; j<r;j++)  { 
| //for( i=0;i<r;i++)  {
| //Lmv[i][j]= (i+1)*(j+1);} }

Try this:

require(inline)

src <- '
  int s = 7;  // result of original oversize estimate before process runs
  int c = 3;  //known column count established from a list argument (variable to function)
  int r = 4;  // number of rows that more complex process found necessary to fill
  int i = 0;
  Rcpp::List Lmv(c) ;  // list of matrix column vectors
  for(  i=0; i<c; i++)  {
     Rcpp::IntegerVector m(s) ;
     // insertion of next three lines partially fills the matrix column vectors
     // the same way as previous sample code
     for( int j=0; j<r; j++){
       m(j)=  (i+1)*(j+1);
     }
     Lmv[i] = m;
  }
  // this code does not access the vector elements as I wish
  for(int j=0; j<c; j++)  {
     for( i=0; i<r; i++)  {
        Rcpp::IntegerVector v = Lmv(j);
        v(i) = (i+1)*(j+1);
     }
  }
  return Lmv;
'

fun <- cxxfunction(signature(), src, plugin = "Rcpp")
print(fun())


And please do search the list archives. I explained a few times already why
using [][] cannot work.

| 
| return Lmv;
| 
| '
| 
|  fun <- cxxfunction(signature(),src, plugin = "Rcpp")
| 
| I can create the partially filled matrix as a list of vectors.
| 
| But I don't know how to access the individual vector elements outside of the initialization loop.
| 
| Then, when I tried to perform the vector erasures I had more trouble:
| 
| Adding these lines before the return call does NOT work as I wanted either.
| for(i=0; i<c; i++)  {
| for(int e=s-1; e>r-1;e--)  {
|  Lmv[i].erase(e); }  // redimension the vector
| }
| 
| I'm trying, but still stuck.


Not all STL methods exist in all Rcpp classes.

Dirk



| 
| 
| 
| -----Original Message-----
| From: Silkworth,David J. 
| Sent: Tuesday, June 07, 2011 3:42 PM
| To: 'Douglas Bates'
| Subject: RE: [Rcpp-devel] redimension help
| 
| My apologies, Doug.
| 
| I've tried to distill the issue to a "simple", but complete example.
| 
| src <- '
| int s = 7;  // result of original oversize estimate before process runs
| int c=3;  //known column count established from a list argument (variable to function)
| Rcpp::IntegerVector v(s);
| Rcpp::IntegerMatrix m(s,c);
| 
| int r = 4;  // number of rows that more complex process found necessary to fill
| for(int x=1; x<r+1;x++)  { v[x-1]=x; }  // just partial fill as process would
| for(int j=0; j<r;j++)  { for(int i=0;i<r;i++)  {m(i,j)= (i+1)*(j+1);} }
| 
| for(int e=s-1; e>r-1;e--)  { v.erase(e); }  // redimension the vector
| 
| Rcpp::List L=Rcpp::List::create(v,m);
| return L;
| '
| 
|  fun <- cxxfunction(signature(),src, plugin = "Rcpp")
| 
| The erase loop on the vector performs a redimension "clean-up" so to speak from my over-estimate of required dimension.  The estimate is made at run time, just that it is made before the matrix is dimensioned and the rest of the function has executed.  The reason that there is shrinkage is that there are duplicate entries that the process finds and adjusts its matrix fill operation for.
| 
| I cannot duplicate such an erase operation on the matrix.  But I think that there is probably a different approach that would work on both the vector and matrix, if I was only smart enough.
| 
| I needed the matrix, because its number of columns is only determined at run time, so I need a way to have indexed labels for the column vectors that it creates.
| 
| 
| 
| Douglas Bates replied:
| 
| I think we are stuck too in that we can't tell what you are trying to
| do.  (Well, at least I can't.).
| 
| Why do you bother erasing elements beyond the size() of the vector?
| I'm pretty sure that will have no effect.
| 
| Perhaps if you were a bit more explicit about what you are trying to
| do we could help.  As it stands you are telling us that you have tried
| to use a method that isn't defined and all we can say is, "Yep, it
| isn't defined."
| 
| 
| _______________________________________________
| 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

-- 
Gauss once played himself in a zero-sum game and won $50.
                      -- #11 at http://www.gaussfacts.com


More information about the Rcpp-devel mailing list