Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiplying a column vector by a numeric scalar in RcppArmadillo

I am having some trouble compiling this simple c++ code using Rcpp and the RcppArmadillo package. Take the following simple example to multiply each column of a matrix by a numeric scalar:

code <- 'arma::mat out = Rcpp::as<arma::mat>(m);
for(int i = 0; i < out.n_cols; ++i){
  out.col(i) *= v;
}
return Rcpp::wrap( out );'

Trying to compile this using...

require( RcppArmadillo )
armMult <- cxxfunction( signature( m = "numeric" , v = "numeric" ),
                        code , plugin = "RcppArmadillo" )

Results in the compile error....

#error: no match for 'operator*=' in 'arma::Mat<eT>::col(arma::uword) [with eT = double, arma::uword = unsigned int](((unsigned int)i)) *= v'

However, if we swap the numeric variable v for 2.0 as below....

code <- 'arma::mat out = Rcpp::as<arma::mat>(m);
for(int i = 0; i < out.n_cols; ++i){
  out.col(i) *= 2.0; //Notice we use 2.0 instead of a variable
}
return Rcpp::wrap( out );'

It compiles just fine....

armMult <- cxxfunction( signature(m="numeric"),
                        code,plugin="RcppArmadillo")

And we can then do...

m <- matrix( 1:4 , 2 , 2 )

armMult( m )
     [,1] [,2]
[1,]    2    6
[2,]    4    8

What am I missing here? How can I make this work with a simple numeric scalar. I would like to be able to pass a scalar like...

armMult( m , 2.0 )

And return the same result as above.

like image 751
Simon O'Hanlon Avatar asked Aug 31 '25 05:08

Simon O'Hanlon


1 Answers

If you want to multiply each column of a matrix A by the corresponding element of a vector x then try this:

Rcpp:::cppFunction(
    "arma::mat fun(arma::mat A, arma::rowvec x) 
    { 
        A.each_row() %= x;
        return A;
    }", depends = "RcppArmadillo"
)

fun(matrix(rep(1, 6), 3, 2), c(5, 1))

     [,1] [,2]
[1,]    5    1
[2,]    5    1
[3,]    5    1
like image 63
chris Avatar answered Sep 02 '25 19:09

chris