<div dir="ltr">> <span style="color:rgb(0,0,0);font-family:Calibri,Arial,Helvetica,sans-serif;font-size:16px">The `mode()` function was pretty useless for determining a matrix object's type</span><div><span style="color:rgb(0,0,0);font-family:Calibri,Arial,Helvetica,sans-serif;font-size:16px"><br></span></div><div><font color="#000000" face="Calibri, Arial, Helvetica, sans-serif"><span style="font-size:16px">Use the storage.mode() function.</span></font></div></div><div class="gmail_extra"><br clear="all"><div><div class="gmail_signature">Bill Dunlap<br>TIBCO Software<br>wdunlap <a href="http://tibco.com" target="_blank">tibco.com</a></div></div>
<br><div class="gmail_quote">On Wed, Nov 26, 2014 at 9:06 AM, David Shih <span dir="ltr"><<a href="mailto:david.shih@mail.utoronto.ca" target="_blank">david.shih@mail.utoronto.ca</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">




<div dir="ltr">
<div style="font-size:12pt;color:#000000;background-color:#ffffff;font-family:Calibri,Arial,Helvetica,sans-serif">
<p>Hi Yixuan,<br>
</p>
<p><br>
</p>
<p>You're right!</p>
<p><br>
</p>
<p>Whether the input matrix can be modified by a function that takes NumericMatrix is determined by whether the input matrix is a NumericMatrix or an IntegerMatrix (the latter needs to be copied and converted). The `mode()` function was pretty useless for determining
 a matrix object's type, but as at least `is.integer()` is predictive.<br>
</p>
<p><br>
</p>
<p>When I wrote another function that takes an IntegerMatrix, it was able to modify an input IntegerMatrix in-place.<br>
</p>
<p><br>
</p>
<p>Thank you,<br>
</p>
<p>David<br>
</p>
<p><br>
</p>
<div style="color:rgb(33,33,33)">
<hr style="display:inline-block;width:98%">
<div dir="ltr"><font face="Calibri, sans-serif" color="#000000" style="font-size:11pt"><b>From:</b> Yixuan Qiu <<a href="mailto:yixuan.qiu@cos.name" target="_blank">yixuan.qiu@cos.name</a>><br>
<b>Sent:</b> November 26, 2014 2:13 AM<br>
<b>To:</b> David Shih<br>
<b>Cc:</b> <a href="mailto:rcpp-devel@lists.r-forge.r-project.org" target="_blank">rcpp-devel@lists.r-forge.r-project.org</a><br>
<b>Subject:</b> Re: [Rcpp-devel] Rcpp: Modification of input argument: Undefined behaviour</font>
<div> </div>
</div><div><div class="h5">
<div>
<p dir="ltr">Hello David,<br>
The general answer to your question is, if the type of your matrix (integer or numeric) in R is different from the one you declare in Rcpp, Rcpp will make a copy and cast it to the appropriate type.<br>
For example, 1:12 is of type integer, and if you pass it as a NumericMatrix, Rcpp will implicitly copy the whole matrix, so no matter what modification you did, the original matrix will not change.<br>
</p>
<p dir="ltr">Best,<br>
Yixuan</p>
<div class="gmail_quote">On Nov 26, 2014 1:33 AM, "David Shih" <<a href="mailto:david.shih@mail.utoronto.ca" target="_blank">david.shih@mail.utoronto.ca</a>> wrote:<br type="attribution">
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Hello,<br>
<br>
There was an earlier post on this subject, but based on my experimentation, the behaviour of modifying input argument is different depending on how the matrix was initialization and other factors...<br>
<br>
I wrote a Rcpp function to modify an input matrix. After calling this function, the input matrix is modified under some circumstances and not modified under others. The behaviour is the same on repeat runs and on both Linux (3.16.3-1-ARCH) and Mac (OSX 10.9).<br>
<br>
The R script, C++ code, and the results are available on Bitbucket:<br>
<br>
<a href="https://bitbucket.org/dshih/rcpp_inplace" target="_blank">https://bitbucket.org/dshih/rcpp_inplace</a><br>
<br>
I don't quite understand when the input matrix is modified in place by the Rcpp function and when the input matrix is be copied on write in the Rcpp function.<br>
<br>
When I stay within Rcpp/C++, a input argument can be modified in-place by a function. (This feature was critical to my optimization.)<br>
<br>
After the code returns to R, I would expect either copy-on-write or in-place modification but not both.<br>
<br>
What is the correct behaviour?<br>
<br>
<br>
Thank you,<br>
<br>
David J. H. Shih<br>
<br>
The Hospital for Sick Children<br>
Peter Gilgan Centre for Research and Learning<br>
686 Bay St<br>
17th floor, Room 17.9707<br>
Toronto, ON  M5G 0A4<br>
Canada<br>
Tel:  <a href="tel:%28416%29%20813-7654%20x309157" value="+14168137654" target="_blank">(416) 813-7654 x309157</a><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></div></div>
</div>
</div>

<br>_______________________________________________<br>
Rcpp-devel mailing list<br>
<a href="mailto:Rcpp-devel@lists.r-forge.r-project.org">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><br></div>