[Rcpp-devel] RcppEigen questions
Hao Xiong
hao at biostat.ucsf.edu
Sat Nov 19 02:22:24 CET 2011
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.
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
>
More information about the Rcpp-devel
mailing list