[Rcpp-devel] RcppEigen questions

Dirk Eddelbuettel edd at debian.org
Sat Nov 19 02:42:44 CET 2011


On 18 November 2011 at 17:22, Hao Xiong wrote:
| On 11/18/2011 04:55 PM, Dirk Eddelbuettel wrote:
| > 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.
| The following code compiles fine as it is
| 
| #include <Eigen/Dense>
| #include <iostream>
| using namespace Eigen;
| 
| int main(){
|    double const a[2] = {1., 2.};
|    double b[2] = {3., 4.};
|    Map<RowVector2d const> amc{a};
|    //Map<RowVector2d> am{a};
|    Map<RowVector2d const> bmc{b};
|    Map<RowVector2d> bm{b};
| 
|    std::cout << "Constant vector a: " << amc << std::endl;
|    std::cout << "Non-constant vector b(mapped constant): " << bmc << 
| std::endl;
|    std::cout << "Non-constant vector b (mapped non-constant): " << bm << 
| std::endl;
|    return 0;
| }
| 
| Uncomment line 9 and it reports
| Eigen_Map.cpp:9:24: error: no matching function for call to 
| 'Eigen::Map<Eigen::Matrix<double, 1, 2>, 0, Eigen::Stride<0, 0> 
|  >::Map(<brace-enclosed initializer list>)'
| ...
| 
|  From this I inferred about the return value of method begin(). I could 
| be wrong, of course.

1) You are using initializer lists for your two vectors a and b -- these are
   a C++11 extension; we generally don't enable these extensions yet as not
   all compilers used with R support them.

   That said, this should do no harm.

2) I have never seen an idiom

      SomeType amc{a};

   using curly braces for initializers. I may be behind the times here.  Can
   you point me to some documentation which shows this to be legit?

3) As I said before, I'd try to first compile against Eigen itself. Once that
   works I'd try the same code with RcppEigen.

Dirk


| 
| Hao
| 
| >
| > | 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