<span style="color: rgb(0, 0, 0); font-family: arial,helvetica,sans-serif;">Hi list,</span><br style="color: rgb(0, 0, 0); font-family: arial,helvetica,sans-serif;"><br style="color: rgb(0, 0, 0); font-family: arial,helvetica,sans-serif;">
<span style="color: rgb(0, 0, 0); font-family: arial,helvetica,sans-serif;">I'm struggling with the copy of my parameters. If I understand correctly, if you ship an int matrix to the function, you'll get a deep copy. However if it's a numeric matrix, then there's no copy and you get a matrix pointing on your R matrix.</span> That's funny when you try to debug as one might work and the other not.<br>
<br style="color: rgb(0, 0, 0); font-family: arial,helvetica,sans-serif;"><span style="color: rgb(0, 0, 0); font-family: arial,helvetica,sans-serif;">Here's an illustration</span><br>library(Rcpp); library(inline)<br>
<br style="color: rgb(0, 0, 0); font-family: arial,helvetica,sans-serif;"><span style="color: rgb(0, 0, 0); font-family: arial,helvetica,sans-serif;">f1 <- cxxfunction(signature(origin="numeric"), body = "</span><br style="color: rgb(0, 0, 0); font-family: arial,helvetica,sans-serif;">
<span style="color: rgb(0, 0, 0); font-family: arial,helvetica,sans-serif;"> Rcpp::NumericMatrix m_source(origin);</span><br style="color: rgb(0, 0, 0); font-family: arial,helvetica,sans-serif;"><span style="color: rgb(0, 0, 0); font-family: arial,helvetica,sans-serif;"> m_source(0,0) = 0;</span><br style="color: rgb(0, 0, 0); font-family: arial,helvetica,sans-serif;">
<span style="color: rgb(0, 0, 0); font-family: arial,helvetica,sans-serif;"> cout << origin << endl << m_source << endl ; ", </span><br style="color: rgb(0, 0, 0); font-family: arial,helvetica,sans-serif;">
<span style="color: rgb(0, 0, 0); font-family: arial,helvetica,sans-serif;"> include = "using namespace std;",</span><br style="color: rgb(0, 0, 0); font-family: arial,helvetica,sans-serif;"><span style="color: rgb(0, 0, 0); font-family: arial,helvetica,sans-serif;"> plugin="Rcpp")</span><br style="color: rgb(0, 0, 0); font-family: arial,helvetica,sans-serif;">
<br style="color: rgb(0, 0, 0); font-family: arial,helvetica,sans-serif;"><span style="color: rgb(0, 0, 0); font-family: arial,helvetica,sans-serif;">m <- matrix(1:4,2,2)</span><br style="color: rgb(0, 0, 0); font-family: arial,helvetica,sans-serif;">
<span style="color: rgb(0, 0, 0); font-family: arial,helvetica,sans-serif;">x <- f1(m)</span><br style="color: rgb(0, 0, 0); font-family: arial,helvetica,sans-serif;"><span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: arial,helvetica,sans-serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; font-size: medium;"><span class="Apple-style-span" style="font-family: Monospace; font-size: 13px; line-height: 17px; white-space: pre-wrap;"><pre tabindex="0" class="GD40030CKR" style="font-family: Monospace; font-size: 10pt !important; outline-style: none; outline-width: initial; outline-color: initial; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; white-space: pre-wrap !important; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; line-height: 1.3; ">
#0x9c7fe18
#0xb416a1a8 # different
<span class="GD40030CNR ace_keyword" style="white-space: pre;"></span><span class="GD40030CBR ace_keyword">m
</span># [,1] [,2]
#[1,] 1 3
#[2,] 2 4
<span class="GD40030CNR ace_keyword" style="white-space: pre;"></span><span class="GD40030CBR ace_keyword">m <- matrix(1:4/2,2,2)
</span>x <- <span class="GD40030CNR ace_keyword" style="white-space: pre;"></span><span class="GD40030CBR ace_keyword">f1(m)
</span>#0xb416a090
#0xb416a090 # no different
<span class="GD40030CNR ace_keyword" style="white-space: pre;"></span><span class="GD40030CBR ace_keyword">m
</span># [,1] [,2]
#[1,] 0 1.5
#[2,] 1 2.0</pre></span></span><br style="font-family: arial,helvetica,sans-serif; color: rgb(0, 0, 0);"><span style="color: rgb(0, 0, 0); font-family: arial,helvetica,sans-serif;">Now, what I'd like is to have a (deep) copy of that matrix, whatever the type. I've tried</span><br style="color: rgb(0, 0, 0); font-family: arial,helvetica,sans-serif;">
<span style="color: rgb(0, 0, 0); font-family: arial,helvetica,sans-serif;"> Rcpp::NumericMatrix m_copy = m_source; // of course it did not work, but never know with vectorisation</span> ;-)<br style="color: rgb(0, 0, 0); font-family: arial,helvetica,sans-serif;">
<span style="color: rgb(0, 0, 0); font-family: arial,helvetica,sans-serif;">sdt::copy(m_source.begin(), m_source.end(), std::back_inserter(m_copy)) // nope</span><br style="color: rgb(0, 0, 0); font-family: arial,helvetica,sans-serif;">
<br style="color: rgb(0, 0, 0); font-family: arial,helvetica,sans-serif;"><span style="color: rgb(0, 0, 0); font-family: arial,helvetica,sans-serif;">The closer I got was with</span><br style="color: rgb(0, 0, 0); font-family: arial,helvetica,sans-serif;">
<span style="color: rgb(0, 0, 0); font-family: arial,helvetica,sans-serif;">f2 <- cxxfunction(signature(origin="numeric"), body = "</span><br style="color: rgb(0, 0, 0); font-family: arial,helvetica,sans-serif;">
<span style="color: rgb(0, 0, 0); font-family: arial,helvetica,sans-serif;"> Rcpp::NumericMatrix m_source(origin);</span><br style="color: rgb(0, 0, 0); font-family: arial,helvetica,sans-serif;"><span style="color: rgb(0, 0, 0); font-family: arial,helvetica,sans-serif;"> Rcpp::NumericMatrix m_copy;</span><br style="color: rgb(0, 0, 0); font-family: arial,helvetica,sans-serif;">
<span style="color: rgb(0, 0, 0); font-family: arial,helvetica,sans-serif;"> Rcpp::NumericMatrix::iterator itr;</span><br style="color: rgb(0, 0, 0); font-family: arial,helvetica,sans-serif;"><span style="color: rgb(0, 0, 0); font-family: arial,helvetica,sans-serif;"> cout << m_copy << endl << endl;</span><br style="color: rgb(0, 0, 0); font-family: arial,helvetica,sans-serif;">
<span style="color: rgb(0, 0, 0); font-family: arial,helvetica,sans-serif;"> m_source(0,0) = 0;</span><br style="color: rgb(0, 0, 0); font-family: arial,helvetica,sans-serif;"><span style="color: rgb(0, 0, 0); font-family: arial,helvetica,sans-serif;"> for(itr = m_source.begin(); itr != m_source.end(); ++itr)</span><br style="color: rgb(0, 0, 0); font-family: arial,helvetica,sans-serif;">
<span style="color: rgb(0, 0, 0); font-family: arial,helvetica,sans-serif;"> m_copy.push_back(*itr);</span><br style="color: rgb(0, 0, 0); font-family: arial,helvetica,sans-serif;"><span style="color: rgb(0, 0, 0); font-family: arial,helvetica,sans-serif;"> m_copy[3] = 1; // matrix structure is lost or never existed;</span><br style="color: rgb(0, 0, 0); font-family: arial,helvetica,sans-serif;">
<span style="color: rgb(0, 0, 0); font-family: arial,helvetica,sans-serif;"> cout << origin << endl << m_source << endl << m_copy ;</span><br style="color: rgb(0, 0, 0); font-family: arial,helvetica,sans-serif;">
<span style="color: rgb(0, 0, 0); font-family: arial,helvetica,sans-serif;"> return wrap(m_copy);", </span><br style="color: rgb(0, 0, 0); font-family: arial,helvetica,sans-serif;"><span style="color: rgb(0, 0, 0); font-family: arial,helvetica,sans-serif;"> include = "using namespace std;",</span><br style="color: rgb(0, 0, 0); font-family: arial,helvetica,sans-serif;">
<span style="color: rgb(0, 0, 0); font-family: arial,helvetica,sans-serif;"> plugin="Rcpp", verbose= FALSE)</span><span style="color: rgb(0, 0, 0); font-family: arial,helvetica,sans-serif;"></span><span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: arial,helvetica,sans-serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; font-size: medium;"><span class="Apple-style-span" style="font-family: Monospace; font-size: 13px; line-height: 17px; white-space: pre-wrap;"><span style="font-family: arial,helvetica,sans-serif;"><br>
<br>Main drawback is that I loose the matrix structure. I'm sure there's a correct way to do this, but I couldn't find it.<br><br>Etienne<br style="color: rgb(0, 0, 0); font-family: arial,helvetica,sans-serif;">
</span></span></span>