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:56:28 -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=r3QTde37P4vDDzxwmTXOKtwxBg+bQpliKH8p4F/XW6w=; b=pP3DI8fdBy4hKO5eOHl0zIzMbLetrrJv8AHNsOqaV9WP/bziNvNlhNR5bsqC2EYLGn jNLF3k/tJn450PkFZimjHX6RMO+7lqTMIncPHpFNzS0RfBgRyOLakFtH765AL95P36Qy AOJFV3TodOeSPuJN64ZUseEqpA00iZjaJhYI0=
- 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=L/2G4njRQRG6dD1uBUfM3v/Slgt68HVE6m9pFXugE2SIDQvXIuzOrXJG3fGcvKNI4z HSmZ1bfcCNBlGlqms39CbiDLQsK5qBYS2hmKRzWAxgIXevFm8e3Ki8fAav9rVvCRdYV/ BnRqiqkO9baDaqFVQZcoA3DF9r+CwrbqulgY0=
another thing:
if the only reason why you need Map's is to address a plain array that
you can pass to another library, wouldn't it be simpler to just use a
MatrixXf (allocating its own data) and then use the .data() method to
address its data?
e.g. in OpenGL apps:
glLoadMatrix( matrix.data() );
The use case for using Map, though, is e.g. if the data array was
allocated by the other library.
2009/10/29 Benoit Jacob <jacob.benoit.1@xxxxxxxxx>:
> 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
>