Re: [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 ]

*To*: eigen <eigen@xxxxxxxxxxxxxxxxxxx>*Subject*: Re: [eigen] Being able to use Eigen::Map<Eigen::Matrix< ... > > everywhere you can use a Eigen::Matrix*From*: Malcolm Reynolds <malcolm.reynolds@xxxxxxxxx>*Date*: Tue, 19 Feb 2013 14:02:34 +0000*Dkim-signature*: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=x-received:mime-version:in-reply-to:references:from:date:message-id :subject:to:content-type; bh=o/HTHf54uVsaTlUiMAZm4aJXSgy902EGWlD9uCaCu7s=; b=C7aUgNvjfGqHdtSIKLLQaGE3/JLmviGYYLq7W+9pKgI01ILD2as1OsNtTmW5+2a0rQ DcBBRe8L5Ho8pgqZvUOrfe5TlmxfM58VLy6PBQqRQFmS0B6FmtGgw2M/4UIr0pkwm2m+ sitVkY3XueL+/93tE3GL3jJwICR817uEWZ0JxvDa7RW+Fc9q9hA4S5APrXXl7uHfGz7F R+7RlqpZwe/2lEjBock06KZVrs5Bn/zUmYMmzK052Bl8flgUuOd1GCvLkZvXQtgEyydK g/2v8yhrLKdS3lWPKaYdLI9RLidl1dgE3wUmLiFrQ1jBG9cuak7xaWHUmVqXOdJ50K21 WAfg==

Now that I'm running through the code changes necessary to implement this - assuming my current functions are templated on the datatype of the matrix, how can I change them to use MatrixBase, but still make sure the type is correct?

Eg:

template<typename T>

class Foo {

public:

inline void fit_params(const Matrix<T, Dynamic, Dynamic> & input_data,

const Matrix<long, Dynamic, Dynamic> & valid_indices) {

// stuff

}

};

I want to make sure that the first argument is a matrix of whatever type the class is instantiated with, and the second matrix contains types which are valid for indexing. I can see that the function can be written as

template<typename T>

class Foo {

public:

template<typename Da, typename Db>

inline void fit_params(const MatrixBase<Da> & input_data,

const MatrixBase<Db> & valid_indices) {

// stuff

}

};

in order to get the efficiency advantages of MatrixBase, but now I can't specify that the second argument must have a scalar type suitable for indexing. Is there a way around this?

Thanks again!

On Tue, Feb 19, 2013 at 1:08 PM, Malcolm Reynolds <malcolm.reynolds@xxxxxxxxx> wrote:

Hi Jitse,Thanks for your quick answer - excellent explanation. The way that C++ can implicitly create temporary objects is definitely something I need to read up more on. One final question, why do you recommend I use MatrixBase rather than the more general DenseBase or EigenBase? Will using the more general template types infer a performance hit?The only suggestion I would have for the documentation is maybe adding some note to the "Mapping external arrays" section of http://eigen.tuxfamily.org/dox-devel/group__QuickRefPage.html as that is the main page I was using for reference. A little note there saying "to use Maps in place of Matrices, please see considerations in <link>", with the link pointing to the TopicFunctionTakingEigenTypes.html page, would be a good idea I think.Thanks again!MalcolmOn Tue, Feb 19, 2013 at 10:30 AM, Jitse Niesen <jitse@xxxxxxxxxxxxxxxxxx> wrote:

On Mon, 18 Feb 2013, Malcolm Reynolds wrote:All Eigen functions are written such that a Map can be used in the same way as a regular Eigen matrix. If you write your own functions, you may need to do something extra if you want your own functions to accept a Map instead of a regular matrix. This is explained at

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?

http://eigen.tuxfamily.org/dox-devel/TopicFunctionTakingEigenTypes.html

I have a function which I'd already written with the signatureinterface for my library. [...]

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

You are calling this function like:

Map<MatrixXd> mapFeatures = ...;

Map<MatrixXd> mapLabels = ...;

predict(mapFeatures, &mapLabels);

For the first argument, the compiler converts the Map<MatrixXd> into a MatrixXd by introducing a temporary object, so it is as if you had written:

MatrixXd tempObject(mapFeatures);

predict(tempObject, &mapLabels);

This does what you want, but it induces a performance penalty because when tempObject is constructed, all the data from mapFeatures is copied into the matrix tempObject.

However, this does not work for the second argument. If the compiler were to introduce another temporary object and pass that as the second argument, then predict() would change the data in the temporary object and not in mapLabels.

The solution is to have predict() accept arguments of the template type MatrixBase, as explained in the link I included above. If you still have questions after reading that, do not hesitate to ask. Any suggestions on how we can improve the explanations would be very welcome.

Good luck,

Jitse

**Follow-Ups**:

**References**:**[eigen] Being able to use Eigen::Map<Eigen::Matrix< ... > > everywhere you can use a Eigen::Matrix***From:*Malcolm Reynolds

**Re: [eigen] Being able to use Eigen::Map<Eigen::Matrix< ... > > everywhere you can use a Eigen::Matrix***From:*Jitse Niesen

**Re: [eigen] Being able to use Eigen::Map<Eigen::Matrix< ... > > everywhere you can use a Eigen::Matrix***From:*Malcolm Reynolds

**Messages sorted by:**[ date | thread ]- Prev by Date:
- Next by Date:
- Previous by thread:
- Next by thread:

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