[Rcpp-devel] Problems indexing matrics

Yasir Suhail yusuhail at gmail.com
Wed Jan 4 21:59:34 CET 2012


Actually,
Rcpp::NumericMatrix output(x_s + x_k - 1, y_s + y_k - 1); output.fill(0.0);
should do it.

Perhaps the following could be added to Matrix.h?

template <typename U> Matrix( const int& nrows_, const int& ncols, const U&
u) :
        VECTOR( Dimension( nrows_, ncols ) ),
        nrows(nrows_)
    { fill(u); }
for completeness? Or does this conflict with the other constructor?

On Wed, Jan 4, 2012 at 3:44 PM, Yasir Suhail <yusuhail at gmail.com> wrote:
>
> From
http://dirk.eddelbuettel.com/code/rcpp/html/Matrix_8h_source.html#l00058
> The constructor for matrix is Matrix(nrow, ncol, iterator), there is no
constructor it seems for initializing the default value. So your problem is
> Rcpp::NumericMatrix output(x_s + x_k - 1, y_s + y_k - 1, 0.0);
> should be
> Rcpp::NumericMatrix output(x_s + x_k - 1, y_s + y_k - 1);
>
> and then a separate loop for initialization. I'd like to know if someone
knows of another way of initialization.
>
> yasir
>
> On Wed, Jan 4, 2012 at 3:13 PM, Hadley Wickham <hadley at rice.edu> wrote:
>>
>> 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/
>> _______________________________________________
>> Rcpp-devel mailing list
>> Rcpp-devel at lists.r-forge.r-project.org
>> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20120104/1701e54d/attachment.htm>


More information about the Rcpp-devel mailing list