[Rcpp-devel] Connection between arma and rcpp

Honglang Wang wanghonglang2008 at gmail.com
Wed Jan 30 00:51:12 CET 2013


Thanks, Kevin. Your last comment saves me a day, lol. Since I do not know
how to learn Rcpp effectively, there is no big pic of Rcpp in my mind. This
is the first time I know that we could change the type of the input into
arma. Thanks a lot. That's what I want.

Best wishes!

Honglang Wang

Office C402 Wells Hall
Department of Statistics and Probability
Michigan State University
1579 I Spartan Village, East Lansing, MI 48823
wangho16 at msu.edu


On Tue, Jan 29, 2013 at 6:32 PM, Kevin Ushey <kevinushey at gmail.com> wrote:

> Sorry, I misunderstood your questions on first read. I have some replies
> in-line.
>
> On Tue, Jan 29, 2013 at 11:20 AM, Honglang Wang <
> wanghonglang2008 at gmail.com> wrote:
>
>> Thanks, I am sorry I did not make my point clear. Let me describe it
>> clearly:
>> 1) The first question is about using "apply":
>>
>> // [[Rcpp::depends(RcppArmadillo)]]
>> #include <RcppArmadillo.h>
>> using namespace Rcpp;
>>
>> // [[Rcpp::export]]
>> List foo(NumericVector x, double h) {
>>
>>   double r = 0.0;
>>   for (int i=0; i<100; i++)
>>     r += i;
>>   r=r+h;
>>   return List::create(Named("Sum") = r,
>>                             Named("h")=h);
>> }
>>
>> ## For this function, I use two inputs in order to illustrate the problem
>> for calling this function by using apply.
>>
>> // [[Rcpp::export]]
>> double fpp(Function f, NumericMatrix Ar, double h) {
>>
>>   int n = Ar.nrow(), m = Ar.ncol();
>>   arma::mat A(Ar.begin(),n,m,false);
>>   A.reshape(m,n);
>>   arma::mat U(m,1)=f(A,1, function(x) foo(x, h=h)$Sum);
>>
>>   double r = arma::accu(U);
>>   return r;
>> }
>>
>> ## what is the correct format to using f=apply here? exactly the same as
>> in R?  The error here is "x" is out of scope.
>>
>
>> A <- matrix(rnorm(8),nrow=4)
>> fpp(apply, A, 3)
>>
>
> There may be a way to do this with some functions in the C++ standard
> library but unfortunately I'm not well versed enough. Maybe std::transform?
> std::for_each? This entry in the Rcpp gallery might illuminate a bit:
> http://gallery.rcpp.org/articles/stl-for-each/
>
>
>>
>> 2) The second question is if i replace using apply by using for loop, how
>> to transfer between arma objects and Rcpp objects:
>>
>> // [[Rcpp::depends(RcppArmadillo)]]
>>  #include <RcppArmadillo.h>
>> using namespace Rcpp;
>>
>> // [[Rcpp::export]]
>> double foo(NumericVector x) {
>>
>>   double r = 0.0;
>>   for (int i=0; i<100; i++)
>>     r += i;
>>
>>   return r;
>> }
>>
>> // [[Rcpp::export]]
>> double fpp(NumericMatrix Ar) {
>>
>>   int n = Ar.nrow(), m = Ar.ncol();
>>   arma::mat A(Ar.begin(),n,m,false);
>>   A.reshape(m,n);
>>   arma::mat U(m,1);
>>   for(int a=0; a<m; a++){
>>    U(a,1)=foo(as<NumericVector>(A.row(a)));
>>   }
>>   double r = arma::accu(U);
>>   return r;
>> }
>>
>> ### how to deal with the arma object A.row(a) for the input of foo, which
>> need rcpp object?
>>
>> A <- matrix(rnorm(8),nrow=4)
>> fpp(mean,A)
>>
>
> I was able to find this link:
> http://stackoverflow.com/questions/14253069/convert-rcpparmadillo-vector-to-rcpp-vector ;
> essentially, you're going to have to generate the 'view' first, wrap that
> view into a SEXP, and then use 'as' to convert that to a NumericVector. Try:
>
> arma::rowvec tmp = A.row(a);
> U(a,1) = foo( as<NumericVector>( wrap(tmp) ) );
>
> (aside -- I think you want to use index 0, rather than 1? Armadillo and
> Rcpp matrices/vectors index from 0 rather than 1.)
>
> It may be preferable to just define the function foo in terms of an
> arma::vec rather than a NumericVector to avoid the excess 'as'ing and
> 'wrap'ping though, unless you absolutely need some functionality specific
> to Rcpp::NumericVector.)
>
>
>>
>> Thanks.
>>
>> Best wishes!
>>
>> Honglang Wang
>>
>> Office C402 Wells Hall
>> Department of Statistics and Probability
>> Michigan State University
>> 1579 I Spartan Village, East Lansing, MI 48823
>> wangho16 at msu.edu
>>
>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20130129/a0ed9387/attachment.html>


More information about the Rcpp-devel mailing list