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

map question

Tags: None
(comma "," separated)
vak
Registered Member
Posts
23
Karma
0
OS

map question

Wed Apr 11, 2012 10:39 am
Hi all,

reading the documentation on the map function ( http://eigen.tuxfamily.org/dox/Tutorial ... alMapTypes ) i'm still a bit confused on two technicalities.


First one:

is

MatrixXf X = Map<MatrixXf>(x,*n,*p);

the eigen equivalent of

arma::mat X(Xr.begin(), *n, *p, false);

here, '[copy_aux_mem=]false' refers to (from arma docs):

"By default the matrix allocates its own memory and copies data from the auxiliary memory (for safety). However, if copy_aux_mem is set to false, the matrix will instead directly use the auxiliary memory (ie. no copying)."


Second one:

Suppose my function alter its arguments. So the content of X will be changed, the rows re-ordered,...ect during the course of the algorithm
(i have no interest in the intermediate values of X).

From a performance point of view, is a mapped matrix a good choice?

As an example:

MatrixXf X = Map<MatrixXf>(x,*n,*p);
MatrixXf X_p=X;

i.e. would it be somehow more efficient to do the computations on X_p rather than on X? (this could be a very naive question).

Thanks in advance,
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS

Re: map question

Thu Apr 12, 2012 7:28 am
For the first question, MatrixXf X = Map<MatrixXf>(x,*n,*p); does a deep copy. To avoid it, directly use a Map object:

Map<MatrixXf> X(x, *n, *p);

I don't get your second issue.
vak
Registered Member
Posts
23
Karma
0
OS

Re: map question

Thu Apr 12, 2012 10:49 am
Dear ggael,

let me try to reformulate: the matrix X will be changed by the code (it's entries will be modified, but also re-sorted using std::nth_element). I cannot pass it as a constant to the sub-functions in my code.

So, now i'am doing a deep copy of X, using:

MatrixXf X = Map<MatrixXf>(x,*n,*p);

and X can be changed by, say, the function StatDist(MatrixXf& X, RowVectorXf& x_cent,int& h).


my second question is:

is "MatrixXf X = Map<MatrixXf>(x,*n,*p);" an efficient way to get the data from x unto X[?], keeping in mind that X will have to be modified during the run (including by non-eigen functions such as std::nth_element).


Thanks in advance for your opinion,


-------------------
VectorXf StatDist(MatrixXf& X, RowVectorXf& x_cent,int& h){
int p=X.cols(),n=X.rows();
X.rowwise()-=x_cent;
ArrayXXf b=((X.topRows(h).transpose()*X.topRows(h))).ldlt().solve(X.transpose())*(h-1);
return (X.array()*b.transpose()).rowwise().sum();
}
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS

Re: map question  Topic is solved

Fri Apr 13, 2012 12:12 pm
Well do as you please. Copies are rarely an issue. I'm just saying that in case you can and/or want to avoid deep copies you have to use a Map object, not a Matrix<>. So you can name it:

Map<MatrixXf> X(x, *n, *p);

and use X just like a Matrix object. However, since a Map is not a Matrix, if you want to pass a Map object to, e.g., StatDist, then this function has to be templated, e.g.:

template<typename MatType, typename VecType>
Matrix<typename MatType::Scalar,Dynamic,1> StatDist(MatType& A, VecType& x_cent,int& h){ .... }
vak
Registered Member
Posts
23
Karma
0
OS

Re: map question

Fri Apr 13, 2012 4:14 pm
thanks! I'm a newbie and your answer really helped me understand the concept of templated functions.


Bookmarks



Who is online

Registered users: Bing [Bot], claydoh, Google [Bot], rblackwell, Yahoo [Bot]