<div dir="ltr">Thank you for your quick response.  I was only thinking about the element-wise matrix/matrix equivalents that already have vector/vector implementations.  My first look was telling me that this shouldn't be difficult.  That said reading and using templated C++ code is a different story than modifying it!<div><br></div><div>Jason<br><div><br></div></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Sat, Jan 20, 2018 at 5:53 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"><span class=""><br>
On 20 January 2018 at 17:45, Jason Nielsen wrote:<br>
| Hi all,<br>
|<br>
| I was wondering if there is a technical reason that element-wise matrix<br>
| operations between Rcpp's Matrix type aren't implemented.  There is<br>
| vector/vector, vector/scalar and matrix/scalar but no matrix/matrix.  The<br>
| following naive code:<br>
|<br>
| #include <Rcpp.h><br>
| using namespace Rcpp;<br>
|<br>
| NumericMatrix operator+(NumericMatrix A, NumericMatrix B) {<br>
|   if ((A.nrow() != B.nrow()) | (A.ncol() != B.ncol()))<br>
|     stop("Dimensions of the two matrices do not conform!");<br>
|   NumericVector vA = as<NumericVector>(A);<br>
|   NumericVector vB = as<NumericVector>(B);<br>
|   NumericVector vC = vA + vB;<br>
|   NumericMatrix C = NumericMatrix(A.nrow(), A.ncol(), vC.begin());<br>
|   return C;<br>
| }<br>
|<br>
| // [[Rcpp::export]]<br>
| NumericMatrix MatAdd(const NumericMatrix A, const NumericMatrix B) {<br>
|   return A+B;<br>
| }<br>
|<br>
| works.  From my brief fiddling it seems there is no copying going on above<br>
| so I don't think there is too much extra overhead in the abstraction.  My<br>
| guess is that it just hasn't been a priority and "patches happily accepted"<br>
| is the answer but I'm curious if there is some other less obvious reason as<br>
| it seems to me all the heavy lifting was done to implement vector/vector.<br>
<br>
</span>R code:<br>
<br>
   sample(c("History", "Lazyness"), 1)   ## just kidding. or maybe not.<br>
<br>
Years back I usually pointed at RcppArmadillo if you wanted "math with<br>
matrices" as we just didn't have all the operators. Now, we have been filling<br>
them in, so this operator+() makes some sense.  Doing them "well" in the<br>
template code is a bit more involved but mayne you want to poke around?<br>
<br>
I am sure some of the regulars here would be happy to help you along.<br>
<span class="HOEnZb"><font color="#888888"><br>
Dirk<br>
<br>
--<br>
<a href="http://dirk.eddelbuettel.com" rel="noreferrer" target="_blank">http://dirk.eddelbuettel.com</a> | @eddelbuettel | <a href="mailto:edd@debian.org">edd@debian.org</a><br>
</font></span></blockquote></div><br></div>