[Rcpp-devel] RcppEigen questions

Dirk Eddelbuettel edd at debian.org
Sat Nov 19 01:55:21 CET 2011


On 18 November 2011 at 16:15, Hao Xiong wrote:
| Hi,
| 
| I have a few questions about RcppEigen. I hope I am posting on the right 
| list.

Sure thing.
 
| 1. const-correctness
| 
| Suppose I have a function
| 
| double fcn(NumericVector const& points){
| ...
| Map<VectorXd > tmp = as<Map<VectorXd > >(points);
| ...
| }
| 
| It compiles fine.
| 
| Changing the mapping line to
| Map<VectorXd const > tmp = as<Map<VectorXd const> >(points);
| and it fails to compile.

Well, are you really sure that is correct C++?  This doesn't compile either

  #include <vector>

  int main(void) {

    std::vector<double> x;
    x.push_back(1);
    x.push_back(42);

    //std::vector<double const> y;   // doesn't work
  }

and one remove the comments on the last line, it fails.  You can only const
in declarations, methinks.

| Eigen is rather strict about const-correctness.
| Map<VectorXd > tmp{const_pointer, 5};
| would fail because the pointer is pointing to read-only memory. I am rather
| confused as which way should be preferred.
| 
| Also the method begin() returns non-const pointer regardless whether the 
| object
| was declared const.

In cases like this I always try to figure out short standalone C++ programs
(such as the one above) to make sure I have the language right. 

Once that is given, move it to the Rcpp* integration.  If the latter fails,
it may be an interface bug on our side.
 
| 2. Still the same setup as above,
| 
| Map<ArrayXd > tmp = as<Map<ArrayXd > >(points);
| fails to compile. Add const still results in failure. Is there anything 
| special about mapping arrays that
| I am not aware of?

There may not be a matching as<> converter yet.  

See the Rcpp-extending vignette and the RcppEigen sources -- maybe you can
work on a contributed patch.  This does exist for Matrices, and that may help
as a framework.
 
| 3. I used to do the above in the following way
| 
| Map<ArrayXd> tmp{points.begin(), points.size()};
| 
| I think this is fine for dense matrix and array, but I am not entirely sure.
| I would welcome any suggestion as to the pros and cons of this vs as<>().

I use Armadillo more than Eigen so I'll pass on recommendations here.

Dirk
 
| 
| Thanks for your help.
| Hao
| _______________________________________________
| 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

-- 
"Outside of a dog, a book is a man's best friend. Inside of a dog, it is too
dark to read." -- Groucho Marx


More information about the Rcpp-devel mailing list