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.