I ran into the same kind of problem again, but this time I just can't find what the correct syntax is:<br>I need to update a matrix stored in a list, by summing with an arma:: mat object<br>the syntax MyList[j] += ArmaObject does not work.<br>
On the other hand I can access the matrix object stored in those list and use them as arma::mat.<br>I tried various syntax using wrap or as but I can't get it working.<br>The error message is:<br>error: no match for 'operator+=' in 'Rcpp::Vector<RTYPE>::operator[](int) [with int RTYPE = 19, Rcpp::Vector<RTYPE>::Proxy = Rcpp::internal::generic_proxy<19>](k) += arma::operator-(const arma::Base<typename T1::elem_type, T1>&, const arma::Base<typename T1::elem_type, T2>&) [with T1 = arma::Glue<arma::eOp<arma::subview<double>, arma::eop_sc<br>
<br>And the full inline code is:<br><br>## loop code<br>src <- '<br>// n markers<br>int P = as<int>(P_);<br>// n individuals<br>int N = as<int>(N_);<br>// marker polynome degree<br>int degM = as<int>(degM_);<br>
// variance<br>//int sigma2 = as<int>(sigma2_);<br>//Rcpp::NumericVector sigma2(sigma2_);<br>double sigma2 = as<double>(sigma2_);<br>// design matrix<br>arma::mat gen_a = Rcpp::as<arma::mat>(gen_a_);<br>
// covariate X<br>arma::mat X = Rcpp::as<arma::mat>(X_);<br>// alpha<br>arma::mat alpha = Rcpp::as<arma::mat>(alpha_);<br>// mu<br>arma::mat mu = Rcpp::as<arma::mat>(mu_);<br>// a_old<br>arma::mat a_old = Rcpp::as<arma::mat>(a_old_);<br>
// tau2<br>arma::mat tau2 = Rcpp::as<arma::mat>(tau2_);<br>// list<br>List all_ui = as<List>(all_ui_);<br>List all_ui2 = as<List>(all_ui2_);<br>List all_ui3 = as<List>(all_ui3_);<br>List all_corMat = as<List>(all_corMat_);<br>
List all_rd = as<List>(all_rd_);<br>arma::mat tmp;<br>arma::mat tmp2 = arma::zeros<arma::mat>((degM+1),(degM+1));<br>arma::mat tmp3 = arma::zeros<arma::mat>(1,(degM+1));<br>arma::mat ui;<br>arma::mat ui2;<br>
arma::mat ui3;<br>arma::mat sig;<br>arma::mat sigma;<br>arma::mat sigma3;<br>arma::mat rdi;<br>arma::mat all_corMati;<br>arma::mat diag;<br>arma::mat diag2 = arma::zeros<arma::mat>((degM+1),(degM+1));<br>arma::mat sol;<br>
arma::mat aVar_j;<br>arma::mat aMu_j;<br>int n = 1;<br>int p = 0;<br>arma::mat U;<br>arma::vec s;<br>arma::mat V;<br>arma::mat rdn;<br>arma::mat ans;<br>arma::mat vsqrt;<br>arma::mat a = arma::zeros<arma::mat>(P,(degM+1));<br>
List test(N);<br> for (int j = 0; j < P; j++) {<br> tmp2.fill(0);<br> tmp3.fill(0);<br> for (int i = 0; i < N; i++) {<br> ui = Rcpp::as<arma::mat>(all_ui[i]);<br>
ui2 = Rcpp::as<arma::mat>(all_ui2[i]);<br> ui3 = Rcpp::as<arma::mat>(all_ui3[i]);<br> rdi = Rcpp::as<arma::mat>(all_rd[i]);<br> all_corMati = Rcpp::as<arma::mat>(all_corMat[i]);<br>
sigma = sigma2*all_corMati;<br> // calculate mean and variance<br> sigma3 = sigma;<br> diag=diagmat(sigma3.fill(1));<br> tmp = (solve(sigma,diag)* gen_a(i,j))*ui3;<br>
tmp2 += (gen_a(i,j)*trans(ui))*tmp;<br> tmp3 += (rdi - mu*trans(ui3)-(X(i)*alpha)*trans(ui2) + gen_a(i,j)*a.row(j)*trans(ui))*tmp;<br> };<br> sol = 1/sigma2 *diagmat(diag2.fill(1/tau2(j)))+tmp2;<br>
aVar_j= solve(sol,diagmat(diag2.fill(1)) );<br> aMu_j = aVar_j*trans(tmp3);<br> ///////<br> p = aVar_j.n_cols;<br> svd(U,s,V,aVar_j);<br> vsqrt = trans(V*(trans(U)%repmat(trans(sqrt(trans(s))),1,p)));<br>
rdn.randn(n,p);<br> ans =rdn*vsqrt + trans(repmat(aMu_j,1,n));<br> a.row(j) = ans;<br> for (int k = 0; k < N; k++) {<br> int k=1;<br> ui = Rcpp::as<arma::mat>(all_ui[k]);<br>
//////////////////////// Here is the issue!<br> all_rd[k] += gen_a(k,j)*a_old.row(j)*trans(ui)- gen_a(k,j)*a.row(j)*trans(ui);<br> };<br> };<br>return Rcpp::List::create(wrap(a),wrap(all_rd));'<br>
####<br>fun <- cxxfunction(signature(P_ = "integer",N_ = "integer",degM_ = "integer", sigma2_ = "double", gen_a_="matrix", X_="matrix",alpha_="matrix",<br>
mu_="matrix", a_old_="matrix",tau2_="matrix", all_ui_="List", all_ui2_="List", all_ui3_="List", all_corMat_="List", all_rd_="List"),body=src,plugin="RcppArmadillo")<br>
<br>Thank you very much for your help<br><br>Nicolas<br><br><br><div class="gmail_quote">On Wed, Jun 15, 2011 at 11:57 PM, Nicolas Heslot <span dir="ltr"><<a href="mailto:nh269@cornell.edu">nh269@cornell.edu</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">I finally figured out, it was a syntax error, as the new assigned object was not an arma:: mat but an element from a list<br>
I took care of the problem by using ui = Rcpp::as<arma::mat>(all_ui[i]) in the reaffectation<br>
<br>Thank you again all for your help<div><div></div><div class="h5"><br><br><div class="gmail_quote">On Wed, Jun 15, 2011 at 11:00 PM, Dirk Eddelbuettel <span dir="ltr"><<a href="mailto:edd@debian.org" target="_blank">edd@debian.org</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div><br>
On 15 June 2011 at 20:17, Nicolas Heslot wrote:<br>
| Any idea about this variable reaffectation thing?<br>
<br>
</div>Well ... that still make little sense. Variables are not protected; there is<br>
nothing stopping you from re-assigning values, be it element-wise, row or<br>
column-wise, or as a whole matrix. So I suspect you have a simple syntax<br>
error.<br>
<br>
But as you still have not shown an actualy error it remains idle speculation<br>
on our side.<br>
<div><br>
| It seems to be specific to arma classes like arma::mat<br>
<br>
</div>Not really. We all use these and read values from / assign values into at<br>
will. There are numerous examples in the archives of this mailing list, in<br>
the unit tests, and in some of the slides from past presentations.<br>
<br>
Dirk<br>
<font color="#888888"><br>
--<br>
</font><div><div></div><div>Gauss once played himself in a zero-sum game and won $50.<br>
-- #11 at <a href="http://www.gaussfacts.com" target="_blank">http://www.gaussfacts.com</a><br>
</div></div></blockquote></div><br>
</div></div></blockquote></div><br>