<div dir="ltr"><div><span style class=""><br>arma</span>::mat pointer method worked; while I trying to get the same result with <span style class="">arma</span>::mat view without copying data, or passing the entire matrix seemed to be laborious. <br>
</div>The following patter provides read/write access to some memory location using armadillo matrix operations.<br><br><div><br>/*<br> * Contact: Steven <span style class="">Varga</span> <br> * <span style class="">steven</span>.<span style class="">varga</span>@<a href="http://gmail.com">gmail.com</a><br>
* 2013 Toronto, On Canada <br> */<br><br>#include <R.h><br>#include <<span style class="">stdlib</span>.h><br>#include <<span style class="">RcppArmadillo</span>.h><br>using <span style class="">namespace</span> std;<br>
<br>// [[<span style class="">Rcpp</span>::depends(<span style class="">RcppArmadillo</span>)]] <br><span style class="">struct</span> Example {<br><br> Example(double *<span style class="">ptr</span>) {<br> <br> A= new <span style class="">arma</span>::mat(<span style class="">ptr</span>, 3,2, false, true ); // no copy contstructor<br>
<span style class="">arma</span>::mat &a = *A; // <span style class="">dereferencing</span> A gets rid of A-> syntax<br> a(1,1) = 1; // convenient <span style class="">matlab</span> like syntax<br>
} <br> <br> ~Example(){ delete A; } // clean up<br> <span style class="">arma</span>::mat *A; <br>};<br><br>// [[<span style class="">Rcpp</span>::depends(<span style class="">RcppArmadillo</span>)]] <br>
// [[<span style class="">Rcpp</span>::export]] <br>void <span style class="">arma</span>_example() {<br><br> <span style class="">arma</span>::mat M(3,10); // implicit memory allocation<br> <br> M.zeros(); // paint with zeros<br>
M.print(); <span style class="">cout</span><<<span style class="">endl</span><<<span style class="">endl</span>;<br><br> Example E(M.<span style class="">memptr</span>());<br> M.print(); // altered as planned<br>
}<br>// <span style class="">EOF</span><br></div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Tue, Nov 26, 2013 at 8:11 AM, Steven Varga <span dir="ltr"><<a href="mailto:steven.varga@gmail.com" target="_blank">steven.varga@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><p>Thank you Simon for suggesting pointer then dereferencing it later in class methods.</p>
<p>Dirk also suggested using matrix views which may have the same performance while keeping syntax neat.</p>
<p>Will repost both working solution later on for the record.</p>
<p>Thanks again, I found all suggestion very helpful,<br>
Steven</p><div class="HOEnZb"><div class="h5">
<div class="gmail_quote">On Nov 26, 2013 6:19 AM, <<a href="mailto:szehnder@uni-bonn.de" target="_blank">szehnder@uni-bonn.de</a>> wrote:<br type="attribution"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Steve,<br>
<br>
If you want to use a variable being an arma::mat obejct with varying memory, you could use an arma::mat pointer. Otherwise if memory stays the same but should be reusable in R, Dirk's suggestion initialising memory in R and passing it to C++ is the most practicable way and very fast.<br>
<br>
Best<br>
Simon<br>
Gesendet über den BlackBerry® Service von E-Plus.<br>
<br>
-----Original Message-----<br>
From: Steven Varga <<a href="mailto:steven.varga@gmail.com" target="_blank">steven.varga@gmail.com</a>><br>
Sender: rcpp-devel-bounces@lists.r-forge.r-project.orgDate: Mon, 25 Nov 2013 23:23:24<br>
To: <<a href="mailto:rcpp-devel@lists.r-forge.r-project.org" target="_blank">rcpp-devel@lists.r-forge.r-project.org</a>><br>
Reply-To: <a href="mailto:steven.varga@gmail.com" target="_blank">steven.varga@gmail.com</a><br>
Subject: Re: [Rcpp-devel] RcppArmadillo matrix with writeable auxiliary<br>
memory<br>
<br>
_______________________________________________<br>
Rcpp-devel mailing list<br>
<a href="mailto:Rcpp-devel@lists.r-forge.r-project.org" target="_blank">Rcpp-devel@lists.r-forge.r-project.org</a><br>
<a href="https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel" target="_blank">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel</a><br>
</blockquote></div>
</div></div></blockquote></div><br></div>