Re: [eigen] Using Eigen::Map to use eigen operations on my data

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


2009/10/29 Benoit Jacob <jacob.benoit.1@xxxxxxxxx>:
> 2009/10/29 Robert Lupton the Good <rhl@xxxxxxxxxxxxxxxxxxx>:
>>>>   float data[4] = { 1, 2, 3, 4 };
>>>>   MatrixXf mat2x2 = MatrixXf(Map<MatrixXf>(data, 2, 2));
>>>
>>> Here you are constructing a new MatrixXf, mat2x2, from the map.
>>>
>>> after this line, mat2x2 is just another matrix and doesn't even
>>> remember that it was copied from the Map.
>>
>> I realised that that must be the problem, but the documentation implied (to
>> me) that it ought to work:
>>
>>> Map
>>> Any memory buffer can be mapped as an Eigen expression:
>>
>> ...
>>>
>>> int data[4] = 1, 2, 3, 4;
>>> MatrixXi mat2x2 = Map<MatrixXi>(data,2,2);
>>
>> Whereas this is in fact performing a copy.
>>
>>> If you want to initialize an array as the identity matrix, do:
>>> Map<MatrixXf>(data, 2, 2).setIdentity();
>>
>> No, that's just the example.  I'm actually working on a swig interface from
>> numpy, based on the one in rikrd-loudia
>>
>>> if you want to write your own function, make it like this:
>>> template<typename Derived>
>>> void identity(MatrixBase<Derived> *x)
>>
>> Now I'm confused.  I want a MatrixXf that shares memory with data[] that I
>> can pass to a pre-existing library.  How does this help?
>
> A MatrixXf allocates its own buffer. If you want to have an object
> that shares memory with data[] and can be used like a MatrixXf, then
> Map<MatrixXf> is exactly that.
>
> What is this pre-existing library you're talking about? Do you have
> control over it? If that library will only take a MatrixXf, and you
> want to use it on preexisting data, then it needs to be adapted to
> take a MatrixBase<Derived> instead.

oh, i understand what you meant now: you want to map existing data
that you can pass to a preexisting library.

ok so here's how you do that.

// allocate plain array
float data = new float[n];

// to use Eigen on your data:
MatrixXf::Map(data,n).setIdentity(); // can use any Eigen function here

some_preexisting_library_function(data);

and so on...
so Map is all you need.
If you want to write a function that works with both MatrixXf and Map,
let it take a MatrixBase as I explained above.

Benoit



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