| Re: [eigen] Using Eigen::Map to use eigen operations on my data | 
[ Thread Index | 
Date Index
| More lists.tuxfamily.org/eigen Archives
] 
- To: eigen <eigen@xxxxxxxxxxxxxxxxxxx>
 
- Subject: Re: [eigen] Using Eigen::Map to use eigen operations on my data
 
- From: Benoit Jacob <jacob.benoit.1@xxxxxxxxx>
 
- Date: Thu, 29 Oct 2009 17:41:53 -0400
 
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed;        d=gmail.com; s=gamma;        h=domainkey-signature:mime-version:received:in-reply-to:references         :date:message-id:subject:from:to:content-type         :content-transfer-encoding;        bh=C8tm39J7RAV/tl9lRrecfiBlVKViTOa257gca7dVMRU=;        b=jqY9PlcenzfpDeu9sM27dHtAAQbMZ0yZ6wQW3NAumJMp8ejQknsXcJVKW0S1M7fzW6         KjlsbBRPIDhiJ/XsTarehRfSVSsfWveBmpeXYNdAe1p9f4VHUtpCQjhSBZOac722scO+         fCqbpuFWW1d7bEhvWBSzbNvNkiWeEsbkmHLLM=
 
- Domainkey-signature: a=rsa-sha1; c=nofws;        d=gmail.com; s=gamma;        h=mime-version:in-reply-to:references:date:message-id:subject:from:to         :content-type:content-transfer-encoding;        b=SRpJscNkzEtZHjoKVRZ2vkfhiEuoRVi+KzKS2yvevgr9+lAn5a9viZo8pj5R1v+tlP         jmzLRoCNL/yRNsdnH9qVx8yzcXUmcb3y/6s8ftSP2qprisHNb8eNbOOvq/RTBzrFxciF         ahrmUmDuvHdIKgquffiYt0CejbaKGz214tM9A=
 
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