Epic_Doughnut
Epic_Doughnut

Reputation: 23

How can I replicate R's functionality with multiplying a matrix by a vector element-wise in Rcpp or Armadillo?

In R, multiplying a matrix by a vector is element-wise by default, and works like this:

A <- matrix(c(1,2,3,4,5,6), nrow=2)
b <- c(1,3)
A * b
     [,1] [,2] [,3]
[1,]    1    3    5
[2,]    6   12   18

Essentially, each element of the matrix is multiplied by an element of the vector round-robin. I want to recreate this functionality in C++, using Rcpp or its extensions like RcppEigen or RcppArmadillo.

With Armadillo, I've tried the following:

arma::mat X ;
arma::vec beta ;

beta.resize ( 2 ) ;

beta (0) = 1.0 ;
beta (1) = 3.0 ;

X.resize ( 3, 2 ) ;

X (0,0) = 1.0 ;
X (0,1) = 2.0 ;
X (1,0) = 3.0 ;
X (1,1) = 4.0 ;
X (2,0) = 5.0 ;
X (2,1) = 6.0 ;

Rcout << X * beta << std::endl ;

Which results in a vector: [7, 15, 23], as if this were matrix multiplication. Substituting the * with a % for element-wise multiplication results in an error: "error: element-wise multiplication: incompatible matrix dimensions: 3x2 and 2x1"

Is there any built-in function for this R-like behavior, or will I need to create my own function (which I have, but I'd prefer an "official" way of doing it)

NumericMatrix matrixTimesVector(NumericMatrix mat, NumericVector vec){
    NumericMatrix res(mat.rows(), mat.cols());
    int index = 0;
    for (int i = 0; i < mat.cols(); ++i){
        for (int j = 0; j < mat.rows(); ++j){
            res( j , i ) = mat( j , i ) * vec[index++ % vec.size()];
        }
    }
    return res;
}

Upvotes: 1

Views: 452

Answers (1)

user20650
user20650

Reputation: 25854

Here are a few options. I suppose the third is closest to what you want -- documented at http://arma.sourceforge.net/docs.html#each_colrow

#include <RcppArmadillo.h>
// [[Rcpp::depends(RcppArmadillo)]]

// looping through each column and element wise multiplication
// [[Rcpp::export]]
arma::mat matTimesVec(arma::mat mat, arma::vec v) {
  for(int i; i < mat.n_cols; i++){
    mat.col(i)  %=  v;
  }
  return mat;
}

// form a diagonal matrix with the vector and then use matrix multiplication
// [[Rcpp::export]]
arma::mat matTimesVec2(arma::mat mat, arma::vec v) {
  return arma::diagmat(v) * mat;
}

// use the functionality described at http://arma.sourceforge.net/docs.html#each_colrow 
// to "Apply a vector operation to each column or row of a matrix "
// [[Rcpp::export]]
arma::mat matTimesVec3(arma::mat mat, arma::vec v) {
  mat.each_col() %= v;
  return mat; 
}

/*** R
A <- matrix(c(1,2,3,4,5,6), nrow=2)
b <- c(1,3)
A * b
matTimesVec(A, b)
matTimesVec2(A, b)
matTimesVec3(A, b)
*/

Upvotes: 2

Related Questions