Note that there are some explanatory texts on larger screens.

plurals
  1. POMultiplying a column vector by a numeric scalar in RcppArmadillo
    text
    copied!<p>I am having some trouble compiling this simple <code>c++</code> code using <code>Rcpp</code> and the <code>RcppArmadillo</code> package. Take the following simple example to multiply each column of a matrix by a numeric scalar:</p> <pre><code>code &lt;- 'arma::mat out = Rcpp::as&lt;arma::mat&gt;(m); for(int i = 0; i &lt; out.n_cols; ++i){ out.col(i) *= v; } return Rcpp::wrap( out );' </code></pre> <p>Trying to compile this using...</p> <pre><code>require( RcppArmadillo ) armMult &lt;- cxxfunction( signature( m = "numeric" , v = "numeric" ), code , plugin = "RcppArmadillo" ) </code></pre> <p>Results in the compile error....</p> <pre><code>#error: no match for 'operator*=' in 'arma::Mat&lt;eT&gt;::col(arma::uword) [with eT = double, arma::uword = unsigned int](((unsigned int)i)) *= v' </code></pre> <p>However, if we swap the <code>numeric</code> variable <code>v</code> for <code>2.0</code> as below....</p> <pre><code>code &lt;- 'arma::mat out = Rcpp::as&lt;arma::mat&gt;(m); for(int i = 0; i &lt; out.n_cols; ++i){ out.col(i) *= 2.0; //Notice we use 2.0 instead of a variable } return Rcpp::wrap( out );' </code></pre> <p>It compiles just fine....</p> <pre><code>armMult &lt;- cxxfunction( signature(m="numeric"), code,plugin="RcppArmadillo") </code></pre> <p>And we can then do...</p> <pre><code>m &lt;- matrix( 1:4 , 2 , 2 ) armMult( m ) [,1] [,2] [1,] 2 6 [2,] 4 8 </code></pre> <p>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...</p> <pre><code>armMult( m , 2.0 ) </code></pre> <p>And return the same result as above.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload