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;
}