[eigen] Being able to use Eigen::Map<Eigen::Matrix< ... > > everywhere you can use a Eigen::Matrix

[ Thread Index | Date Index | More lists.tuxfamily.org/eigen Archives ]

Hi everyone,

First post to this list, apologise if this is something really obvious but I'm having a bit of trouble using Eigen::Map. As I understand, it allows me to declare an Eigen matrix which reuses some memory already allocated (in my cases I'm gettin the data pointer from Numpy matrices), and then use this value everywhere I can use a regular Eigen matrix? I'm having trouble getting this to work, and potentially what I want to do isn't possible, but I haven't found any explanation in the docs. Please bear in mind I am far from a C++ pro!

I have a function which I'd already written with the signature

predict(const MatrixXd & features, MatrixXd * const labels_out)

following the good practice (afaik) of using const refs for parameters that don't need to be modified, and pointers otherwise. Anyway I'd like to pass in Eigen::Maps for both arguments of this function, as I'm writing a python interface for my library. I'm constructing the maps and storing them in shared_ptrs, then dereferencing the shared pointer for the ref argument and simply passing the pointer for the second argument. Looks a bit like this...

// Get the data from Numpy into Eigen::Maps
boost::shared_ptr<const Eigen::Map<MatrixXd> > features( new Eigen::Map<double>((double*) PyArray_DATA(features_np)));
boost::shared_ptr<Eigen::Map<MatrixXd> > labels( new Eigen::Map<double>((double*) PyArray_DATA(labels_np)));

// call the function
predict(*features, labels.get())

This gives a compiler error about the second argument, saying  cannot initialize a parameter of type
      'label_mtx<double> *' (aka 'Eigen::Matrix<double, -1, -1, 0, -1, -1> *') with an rvalue of type
      'Eigen::Map<Eigen::Matrix<double, -1, -1, 0, -1, -1>, 0, Eigen::Stride<0, 0> > *'
        predict(*features, predict_mean_out.get());

(label_mtx<T> is just a templated typedef I've made for Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>)

I don't understand why what I've done is valid for the first argument but not for the second argument? It seems like both of them are passing in a Map<MatrixXd> to
a function that expects a MatrixXd, so on some level the compiler understands the equivalence of Maps with normal matrices. Given that, why does the first case work and the second one not? Is it to do with the pointer / ref difference, or to do with the const-ness?

Any advice that anyone has would be greatly appreciated! Also have to say a big thankyou to the authors of the library, only been using it for a few weeks but the productivity improvement over ublas is huge.


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/