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 Robert Lupton the Good <rhl@xxxxxxxxxxxxxxxxxxx>:
> I must be missing something.  The following code prints:
> [1 2 3 4]
> 1 0
> 0 1
>
> although I thought that the point of Map was to create an object that shared
> memory with an underlying array.  What am I Missing?
>
>                                R
>
> #include <iostream>
> #include "Eigen/Core.h"
>
> using namespace std;
> using namespace Eigen;
>
> void identity(MatrixXf *mat) {
>    (*mat)(0, 0) = (*mat)(1, 1) = 1;
>    (*mat)(1, 0) = (*mat)(0, 1) = 0;
> }
>
> int main() {
>    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.

>
>    identity(&mat2x2);

Here you are calling identity on that matrix, not on the Map.

If you want to initialize an array as the identity matrix, do:
Map<MatrixXf>(data, 2, 2).setIdentity();

if you want to write your own function, make it like this:
template<typename Derived>
void identity(MatrixBase<Derived> *x)


>
>    cout << "[" << data[0] << " " << data[1] <<  " " << data[2] <<  " " <<
> data[3] << "]\n" << mat2x2 << endl;
> }
>
>
>
>



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