From <a href="http://dirk.eddelbuettel.com/code/rcpp/html/Matrix_8h_source.html#l00058">http://dirk.eddelbuettel.com/code/rcpp/html/Matrix_8h_source.html#l00058</a><div>The constructor for matrix is Matrix(nrow, ncol, iterator), there is no constructor it seems for initializing the default value. So your problem is </div>
<div>Rcpp::NumericMatrix output(x_s + x_k - 1, y_s + y_k - 1, 0.0);</div><div>should be</div><div>Rcpp::NumericMatrix output(x_s + x_k - 1, y_s + y_k - 1);<br><br>and then a separate loop for initialization. I'd like to know if someone knows of another way of initialization.</div>
<div><br></div><div>yasir</div><div><br><div class="gmail_quote">On Wed, Jan 4, 2012 at 3:13 PM, Hadley Wickham <span dir="ltr"><<a href="mailto:hadley@rice.edu">hadley@rice.edu</a>></span> wrote:</div><div class="gmail_quote">
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi all,<br>
<br>
As I'm (slowly) getting more proficient with Rcpp, I'm attempting to<br>
write a 2d convolution function. My latest attempt:<br>
<br>
convolve2d <- cxxfunction(signature(sampleS = "numeric", kernelS =<br>
"numeric"), plugin = "Rcpp", '<br>
Rcpp::NumericMatrix sample(sampleS), kernel(kernelS);<br>
int x_s = sample.nrow(), x_k = kernel.nrow();<br>
int y_s = sample.ncol(), y_k = kernel.ncol();<br>
<br>
Rcpp::NumericMatrix output(x_s + x_k - 1, y_s + y_k - 1, 0.0);<br>
for (int row = 0; row < x_s; row++) {<br>
for (int col = 0; col < y_s; col++) {<br>
for (int i = 0; i < x_k; i++) {<br>
for (int j = 0; j < y_k; j++) {<br>
output(row, col) += sample(row + i, col + j) * kernel(i, j);<br>
}<br>
}<br>
}<br>
}<br>
return output;<br>
')<br>
<br>
fails to compile with a not very informative error message:<br>
<br>
Error in compileCode(f, code, language = language, verbose = verbose) :<br>
Compilation ERROR, function(s)/method(s) not created!<br>
/usr/include/c++/4.2.1/bits/stl_iterator_base_types.h: In<br>
instantiation of ‘std::iterator_traits<double>’:<br>
/Users/hadley/R/Rcpp/include/Rcpp/internal/wrap.h:346: instantiated<br>
from ‘SEXPREC* Rcpp::internal::range_wrap(InputIterator,<br>
InputIterator) [with InputIterator = double]’<br>
/Users/hadley/R/Rcpp/include/Rcpp/internal/wrap.h:847: instantiated<br>
from ‘SEXPREC* Rcpp::wrap(InputIterator, InputIterator) [with<br>
InputIterator = double]’<br>
/Users/hadley/R/Rcpp/include/Rcpp/vector/Vector.h:418: instantiated<br>
from ‘void Rcpp::Vector<RTYPE>::assign(InputIterator, InputIterator)<br>
[with InputIterator = double, int RTYPE = 14]’<br>
/Users/hadley/R/Rcpp/include/Rcpp/vector/Vector.h:249: instantiated<br>
from ‘Rcpp::Vector<RTYPE>::Vector(InputIterator, InputIterator) [with<br>
InputIterator = double, int RTYPE = 14]’<br>
/Users/hadley/R/Rcpp/include/Rcpp/vector/Matrix.h:60: instantiated<br>
from ‘Rcpp::Matrix<RTYPE>::Matrix(const i<br>
<br>
How can I use that error message to figure out what I'm doing wrong?<br>
<br>
I'm also wondering if since I'm using () to index, should I be using 1<br>
based indices? I think that's the difference for () and [] for<br>
vectors, but I might just be confused.<br>
<br>
(Of course a separate question is whether this code actually<br>
implements 2d convolution or not, but it should hopefully be close)<br>
<span class="HOEnZb"><font color="#888888"><br>
Hadley<br>
<br>
<br>
--<br>
Assistant Professor / Dobelman Family Junior Chair<br>
Department of Statistics / Rice University<br>
<a href="http://had.co.nz/" target="_blank">http://had.co.nz/</a><br>
_______________________________________________<br>
Rcpp-devel mailing list<br>
<a href="mailto:Rcpp-devel@lists.r-forge.r-project.org">Rcpp-devel@lists.r-forge.r-project.org</a><br>
<a href="https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel" target="_blank">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel</a><br>
</font></span></blockquote></div><br></div>