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
_______________________________________________
Rcpp-devel mailing list
Rcpp-devel@lists.r-forge.r-project.org
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel

Reply via email to