This forum has been archived. All content is frozen. Please use KDE Discuss instead.

Map only immutable?

Tags: None
(comma "," separated)
di4jc8732
Registered Member
Posts
11
Karma
0

Map only immutable?

Fri Jul 08, 2011 6:47 pm
The following code produces

Code: Select all
#include <Eigen/Dense>
#include <iostream>

using namespace Eigen;
using namespace std;

int main(){
  float af[]= {0.1, 0.2, 0.3, 0.4, 0.5, 0.6};
  VectorXf av = VectorXf::Map(af, 6);

  cout << "Mapped vector is \n"<< av << endl;
  av(1) = 2.0;
  cout << "After changing the second element: \n" << av << endl;
  cout << "The original array is :\n";
  for (int i=0; i<6; i++)
    cout << af[i] << "\n";
  cout << endl;
}


Mapped vector is
0.1
0.2
0.3
0.4
0.5
0.6
After changing the second element:
0.1
2
0.3
0.4
0.5
0.6
The original array is :
0.1
0.2
0.3
0.4
0.5
0.6


Does this mean the mapped vector/matrix are read-only? And any attempt assigning elements or whole vector/matrix results in new memory allocation?
di4jc8732
Registered Member
Posts
11
Karma
0

Re: Map only immutable?

Fri Jul 08, 2011 7:00 pm
I just saw in a previous thread that
Code: Select all
VectorXf av = VectorXf::Map(af, 6);

results in a deep copy. But if I change the type to
Code: Select all
Map<VectorXf>
, I cannot pass the variable
to a function taking VectorXf&, unless I change everything
to "const". Why is that?
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS

Re: Map only immutable?

Sat Jul 09, 2011 4:45 am
if you add a "const" then the compiler automatically convert your Map<VectorXf> into a temporary VectorXf. To write generic functions read this page:

http://eigen.tuxfamily.org/dox/TopicFun ... Types.html
di4jc8732
Registered Member
Posts
11
Karma
0

Re: Map only immutable?

Mon Jul 11, 2011 10:18 pm
I see. So we need to be prepared for matrix, matrix expression, and possibly resizing matrix expression. I have a couple of more questions:

1. Do this mean any function we write taking matrix need to be templated? Even methods in a class?

2. What about returning a matrix by value? In particle, if I want to write to Map<VectorXd> with an assignment with rhs a function call, how would I go about that?

Thanks for a great library and answering my questions.
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS

Re: Map only immutable?

Tue Jul 12, 2011 6:12 am
1. if you want it to accept any kind of "matrix" objects then yes.
2. you can return a Matrix and pay for the cost of an extra copy. As an alternative we offer a ReturnByValue mechanism which transform something like:

res = foo(...);

to:

foo_impl(..., res);

the function foo will simply return a simple proxy object that encode the actual foo_impl function as well as the default evaluation type in case someone do, e.g.:

res = A + foo(...);


Bookmarks



Who is online

Registered users: bartoloni, Bing [Bot], Google [Bot], Yahoo [Bot]