Actually, <br><font color="#3333ff">Rcpp::NumericMatrix output(x_s + x_k - 1, y_s + y_k - 1); output.fill(0.0);</font><br>should do it.<br><br>Perhaps the following could be added to Matrix.h?<br><br><font color="#3333ff">template <typename U> Matrix( const int& nrows_, const int& ncols, const U& u) : <br>
VECTOR( Dimension( nrows_, ncols ) ), <br> nrows(nrows_)<br> { fill(u); }</font><br>for completeness? Or does this conflict with the other constructor?<br><br>On Wed, Jan 4, 2012 at 3:44 PM, Yasir Suhail <<a href="mailto:yusuhail@gmail.com">yusuhail@gmail.com</a>> wrote:<br>
><br>> 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><br>> The constructor for matrix is Matrix(nrow, ncol, iterator), there is no constructor it seems for initializing the default value. So your problem is <br>
> Rcpp::NumericMatrix output(x_s + x_k - 1, y_s + y_k - 1, 0.0);<br>> should be<br>> 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.<br>
><br>> yasir<br>><br>> On Wed, Jan 4, 2012 at 3:13 PM, Hadley Wickham <<a href="mailto:hadley@rice.edu">hadley@rice.edu</a>> wrote:<br>>><br>>> 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>>><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/">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">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel</a><br>
><br>><br>