[Rcpp-devel] Fwd: Element-wise matrix operations
Jason Nielsen
drjdnielsen at gmail.com
Sat Jan 20 23:45:01 CET 2018
Hi all,
I was wondering if there is a technical reason that element-wise matrix
operations between Rcpp's Matrix type aren't implemented. There is
vector/vector, vector/scalar and matrix/scalar but no matrix/matrix. The
following naive code:
#include <Rcpp.h>
using namespace Rcpp;
NumericMatrix operator+(NumericMatrix A, NumericMatrix B) {
if ((A.nrow() != B.nrow()) | (A.ncol() != B.ncol()))
stop("Dimensions of the two matrices do not conform!");
NumericVector vA = as<NumericVector>(A);
NumericVector vB = as<NumericVector>(B);
NumericVector vC = vA + vB;
NumericMatrix C = NumericMatrix(A.nrow(), A.ncol(), vC.begin());
return C;
}
// [[Rcpp::export]]
NumericMatrix MatAdd(const NumericMatrix A, const NumericMatrix B) {
return A+B;
}
works. From my brief fiddling it seems there is no copying going on above
so I don't think there is too much extra overhead in the abstraction. My
guess is that it just hasn't been a priority and "patches happily accepted"
is the answer but I'm curious if there is some other less obvious reason as
it seems to me all the heavy lifting was done to implement vector/vector.
Many thanks,
Jason
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20180120/57106c8e/attachment.html>
More information about the Rcpp-devel
mailing list