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 class="h5">#include <RcppArmadillo.h><br>
using namespace Rcpp;<br><br>// [[Rcpp::export]]<br>List foo(NumericVector x, double h) {<br> double r = 0.0;<br> for (int i=0; i<100; i++)<br>
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) {<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)=f(A,1, function(x) foo(x, h=h)$Sum);<br> double r = arma::accu(U);<br> return r;<br>}<br><br>## 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.<br>
<br>A <- matrix(rnorm(8),nrow=4)<br>fpp(apply, A, 3)</div><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 class="h5">
#include <RcppArmadillo.h><br>using namespace Rcpp;<br><br>// [[Rcpp::export]]<br>double foo(NumericVector x) {<br> double r = 0.0;<br> for (int i=0; i<100; i++)<br> r += i;<br>
return r;<br>
}<br><br>// [[Rcpp::export]]<br>double fpp(NumericMatrix Ar) {<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>
U(a,1)=foo(as<NumericVector>(A.row(a))); <br> }<br> double r = arma::accu(U);<br> return r;<br>}<br><br>### 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><br>Thanks.<br></div><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>