Sorry, I misunderstood your questions on first read. I have some replies in-line.<br><br><div class="gmail_quote">On Tue, Jan 29, 2013 at 11:20 AM, Honglang Wang <span dir="ltr"><<a href="mailto:wanghonglang2008@gmail.com" target="_blank">wanghonglang2008@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Thanks, I am sorry I did not make my point clear. Let me describe it clearly:<br>1) The first question is about using "apply":<br>
<br>// [[Rcpp::depends(RcppArmadillo)]]<br><div><div class="im">#include <RcppArmadillo.h><br>
using namespace Rcpp;<br><br>// [[Rcpp::export]]<br></div>List foo(NumericVector x, double h) {<div class="im"><br>  double r = 0.0;<br>  for (int i=0; i<100; i++)<br></div>


    r += i;<br>  r=r+h;<br>  return List::create(Named("Sum") = r,<br>                            Named("h")=h);<br>}<br><br>## For this function, I use two inputs in order to illustrate the problem for calling this function by using apply. <br>

<br>// [[Rcpp::export]]<br>double fpp(Function f, NumericMatrix Ar, double h) {<div class="im"><br>  int n = Ar.nrow(), m = Ar.ncol();<br>


  arma::mat A(Ar.begin(),n,m,false);<br>  A.reshape(m,n);<br></div>  arma::mat U(m,1)=f(A,1, function(x) foo(x, h=h)$Sum);<div class="im"><br>  double r = arma::accu(U);<br>  return r;<br>}<br><br></div>## 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. </div>
</blockquote><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div>
<br>A <- matrix(rnorm(8),nrow=4)<br>fpp(apply, A, 3)</div></blockquote><div><br></div><div>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: <a href="http://gallery.rcpp.org/articles/stl-for-each/">http://gallery.rcpp.org/articles/stl-for-each/</a></div>
<div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br>2) The second question is if i replace using apply by using for loop, how to transfer between arma objects and Rcpp objects:<br>
<br>// [[Rcpp::depends(RcppArmadillo)]]<br>
<div><div class="im">


#include <RcppArmadillo.h><br>using namespace Rcpp;<br><br>// [[Rcpp::export]]<br></div>double foo(NumericVector x) {<div class="im"><br>  double r = 0.0;<br>  for (int i=0; i<100; i++)<br></div>    r += i;<div class="im">
<br>

  return r;<br>
}<br><br>// [[Rcpp::export]]<br></div>double fpp(NumericMatrix Ar) {<div class="im"><br>  int n = Ar.nrow(), m = Ar.ncol();<br>  arma::mat A(Ar.begin(),n,m,false);<br>  A.reshape(m,n);<br>  arma::mat U(m,1);<br>  for(int a=0; a<m; a++){<br>
</div>



  U(a,1)=foo(as<NumericVector>(A.row(a)));  <br><div class="im">  }<br>  double r = arma::accu(U);<br>  return r;<br>}<br><br></div>### how to deal with the arma object A.row(a) for the input of foo, which need rcpp object?<br>
<br>A <- matrix(rnorm(8),nrow=4)<br>
fpp(mean,A)<br></div></blockquote><div><br></div><div>I was able to find this link: <a href="http://stackoverflow.com/questions/14253069/convert-rcpparmadillo-vector-to-rcpp-vector">http://stackoverflow.com/questions/14253069/convert-rcpparmadillo-vector-to-rcpp-vector</a> ; 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:</div>
<div><br></div><div>arma::rowvec tmp = A.row(a);</div><div>U(a,1) = foo( as<NumericVector>( wrap(tmp) ) );</div><div><br></div><div>(aside -- I think you want to use index 0, rather than 1? Armadillo and Rcpp matrices/vectors index from 0 rather than 1.)</div>
<div><br></div><div>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.)</div>
<div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><br>Thanks.<br></div><div class="HOEnZb"><div class="h5"><br clear="all"><div><div>Best wishes!</div><div>
 </div><div>Honglang Wang</div><div> </div><div>Office C402 Wells Hall</div><div>Department of Statistics and Probability</div><div>Michigan State University</div>
<div>1579 I Spartan Village, East Lansing, MI 48823</div><div><a href="mailto:wangho16@msu.edu" target="_blank">wangho16@msu.edu</a></div></div>
<br><br>
</div></div></blockquote></div><br>