[Rcpp-devel] Problems indexing matrics
Hadley Wickham
hadley at rice.edu
Wed Jan 4 21:13:40 CET 2012
Hi all,
As I'm (slowly) getting more proficient with Rcpp, I'm attempting to
write a 2d convolution function. My latest attempt:
convolve2d <- cxxfunction(signature(sampleS = "numeric", kernelS =
"numeric"), plugin = "Rcpp", '
Rcpp::NumericMatrix sample(sampleS), kernel(kernelS);
int x_s = sample.nrow(), x_k = kernel.nrow();
int y_s = sample.ncol(), y_k = kernel.ncol();
Rcpp::NumericMatrix output(x_s + x_k - 1, y_s + y_k - 1, 0.0);
for (int row = 0; row < x_s; row++) {
for (int col = 0; col < y_s; col++) {
for (int i = 0; i < x_k; i++) {
for (int j = 0; j < y_k; j++) {
output(row, col) += sample(row + i, col + j) * kernel(i, j);
}
}
}
}
return output;
')
fails to compile with a not very informative error message:
Error in compileCode(f, code, language = language, verbose = verbose) :
Compilation ERROR, function(s)/method(s) not created!
/usr/include/c++/4.2.1/bits/stl_iterator_base_types.h: In
instantiation of ‘std::iterator_traits<double>’:
/Users/hadley/R/Rcpp/include/Rcpp/internal/wrap.h:346: instantiated
from ‘SEXPREC* Rcpp::internal::range_wrap(InputIterator,
InputIterator) [with InputIterator = double]’
/Users/hadley/R/Rcpp/include/Rcpp/internal/wrap.h:847: instantiated
from ‘SEXPREC* Rcpp::wrap(InputIterator, InputIterator) [with
InputIterator = double]’
/Users/hadley/R/Rcpp/include/Rcpp/vector/Vector.h:418: instantiated
from ‘void Rcpp::Vector<RTYPE>::assign(InputIterator, InputIterator)
[with InputIterator = double, int RTYPE = 14]’
/Users/hadley/R/Rcpp/include/Rcpp/vector/Vector.h:249: instantiated
from ‘Rcpp::Vector<RTYPE>::Vector(InputIterator, InputIterator) [with
InputIterator = double, int RTYPE = 14]’
/Users/hadley/R/Rcpp/include/Rcpp/vector/Matrix.h:60: instantiated
from ‘Rcpp::Matrix<RTYPE>::Matrix(const i
How can I use that error message to figure out what I'm doing wrong?
I'm also wondering if since I'm using () to index, should I be using 1
based indices? I think that's the difference for () and [] for
vectors, but I might just be confused.
(Of course a separate question is whether this code actually
implements 2d convolution or not, but it should hopefully be close)
Hadley
--
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/
More information about the Rcpp-devel
mailing list