[Rcpp-devel] row iterators in RcppArmadillo
Sokol Serguei
serguei.sokol at gmail.com
Thu Oct 13 00:27:29 CEST 2016
Hi Jonathan,
Jonathan Fintzi has written at Wed, 12 Oct 2016 13:43:20 -0700
> Hello,
>
> I am trying to use the std::copy() function to copy elements from an
> Rcpp::NumericVector into rows of armadillo matrices. A simplified
> version of my code is as follows:
>
> cppFunction('void copyinto(Rcpp::NumericVector& v, arma::mat& d1,
> arma::mat& d2) {
> std::copy(v.begin(), v.begin() + 3, d1.begin_row(0));
> std::copy(v.begin() + 3, v.end(), d2.begin_row(0));
> }', depends = "RcppArmadillo")
>
> v <- runif(6)
> d1 <- matrix(0.0, 3,3)
> d2 <- matrix(0.0, 3,3)
>
> copyinto(v, d1, d2)
>
> However, when I attempt to compile the code, I receive a list of
> compilation errors including "no type named 'XXXXXX' in 'class
> arma::Mat<double>row_iterator", where XXXXXX is iterator_category,
> value_type, difference_type, pointer, and reference.
>
> If I change the iterators to column iterators (e.g. d1.begin_col(0))
> everything compiles and works just fine. I feel that I must be missing
> something quite basic here, but in searching around I haven't been
> able to identify my mistake.
May be the problem is that you are trying to use std::copy for writing
to non contiguous memory when you use a row pointer and the class writers
were smart enough to prevent such situation.
The following code works as expected, may be it can do the job for you:
cppFunction('void copy2(arma::rowvec& v, arma::mat& d1, arma::mat& d2) {
d1.row(0)=v(arma::span(0,2));
d2.row(0)=v(arma::span(3,5));
}', depends = "RcppArmadillo")
v <- runif(6)
d1 <- matrix(0.0, 3,3)
d2 <- matrix(0.0, 3,3)
copy2(v, d1, d2)
>
> Thank you for your help!
> Jon
>
>
> _______________________________________________
> 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/20161012/c9882005/attachment.html>
More information about the Rcpp-devel
mailing list