[Rcpp-devel] redimension success with arma

Silkworth,David J. SILKWODJ at airproducts.com
Sun Jul 3 04:41:47 CEST 2011

I never forgot Christian Gunning's advice on this matter.  Today I revisited the issue with a first attempt at using RcppArmadillo.  (Hey look, the newbe is working with arma!)

It turns out that .set_size(r,c) doesn't return what you might expect.

Neither does .reshape(r,c)!

BUT, there IS a member function shed_rows(r,[end]) that does what I wanted.

It was not intuitive that using the single plugin value "RcppArmadillo" would also permit normal Rcpp processing.  There really were no examples to show this.

Here is my final example case.  (There had been an error in the original redimension help example in that the j index was permitted to exceed the matrix columns. For some reason the Rcpp::IntegerMatrix did not mind about this, but the arma::mat sure put up a fuss until I corrected this!)

src1 <- '
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);
  arma::imat 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<c;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
m.shed_rows(r,s-1);    // redimension the matrix
Rcpp::List L=Rcpp::List::create(v,m);
return L;

 fun1 <- cxxfunction(signature(),src1, plugin = "RcppArmadillo")


I am now fascinated with arma, but confused about LAPACK and BLAS integration on my windows system, but doubt I will have to get that far in the near future.

-----Original Message-----
From: icos.atropa at gmail.com [mailto:icos.atropa at gmail.com] On Behalf Of Christian Gunning
Sent: Wednesday, June 08, 2011 4:55 AM
To: rcpp-devel at r-forge.wu-wien.ac.at
Cc: Silkworth,David J.; Douglas Bates
Subject: Re: [Rcpp-devel] redimension help

> I have developed a function that builds a series of vectors and a matrix
> of undetermined size. ?Rather than attempt dynamic objects (which I
> wouldn't know how to do anyway) I have been able to initialize with row
> dimensions that cannot be exceeded on these objects.

If this is your goal, I recommend an alternate method --
RcppArmadillo.  It offers resizing (e.g.
).  There are several ways to do this with Armadillo, depending on
whether the memory of the object gets preserved or not. Note that you
need to wrap() arma objects before, for example, putting them in lists
(i think) or returning them to R.  Something like this should get you


src = '
mymat = arma::mat(2, 2);
//find dims of mymat, put them in vars nrow and ncol
mymat.set_size(nrow, ncol);
// do things
return wrap(mymat);

fun = cxxfunction('', src, plugin='RcppArmadillo')

As an aside, are you *sure* that you can't allocate after computing
the size?  I'm with Douglas in the confused camp :)
After all, much of Rcpp's magic is via templates, which make objects a
lot more dynamic than you might expect (if you're come from, for
example, a fortran background).


More information about the Rcpp-devel mailing list