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

Two questions: cwise division and passing matrix column to function

Tags: None
(comma "," separated)
yesint
Registered Member
Posts
7
Karma
0
Dear All,
I'm new to eigen, and I'm completely amazed by its performance and design.
However I'm stuck with few things.

1) I can't figure out how to perform an element-wise division. vec.cwise()/scalar
does not work and I have to write a loop for this. Is there any technical reason why "/" operator does not work with cwise()? If not, then this would be a good idea to include it in the next version.

2) I have a function which takes a vector like this:

void some_func(VectorXd& vec);

and I want to pass a column of the matrix to it, but some_func(matr.col(i)) does not work because of type mismatch. Is there any intelligent way of doing this, or I have to make a copy of the column in vector object first?

Many thanks in advance for the answers!

Last edited by yesint on Wed Apr 15, 2009 6:40 pm, edited 1 time in total.
User avatar
bjacob
Registered Member
Posts
658
Karma
3
yesint wrote:Dear All,
I'm new to eigen, and I'm completely amazed by its performance and design.
However I'm stuck with few things.

1) I can't figure out how to perform an element-wise division. vec.cwise()/scalar
does not work


You want to divide all coeffs of a vector by the same scalar? Then that's a plain division, no need for cwise, do:

vec / scalar;

Otherwise, to perform a cwise quotient by another vector, you would do vec.cwise()/othervector, and that would require you to #include.

and I have to write a loop for this. Is there any technical reason why "/" operator does not work with cwise()?


The reason why it works but doesn't want you to write "cwise()" is that it is a basic linear-algebraic operation. Cwise is for operations that don't make much sense from the point of view of linear algebra but are useful for dealing with arrays.

2) I have a function which takes a vector like this:

void some_func(VectorXd& vec);

and I want to pass a column of the matrix to it, but some_func(matr.col(i)) does not work because of type mismatch. Is there any intelligent way of doing this, or I have to make a copy of the column in vector object first?


Yes,

Code: Select all
template
void some_func(const MatrixBase& vec);


Notice I made the reference constant because C++ doesn't want non-constant references to temporaries. You can always const-cast if you must.

Or if you don't want to make your function templated, you can always keep your old function and call it like this:
Code: Select all
some_func(matr.col(i).eval())

However this kills the possibility of lazy evaluation and doesn't give write access.


Join us on Eigen's IRC channel: #eigen on irc.freenode.net
Have a serious interest in Eigen? Then join the mailing list!
yesint
Registered Member
Posts
7
Karma
0
bjacob wrote:
yesint wrote:Dear All,
I'm new to eigen, and I'm completely amazed by its performance and design.
However I'm stuck with few things.

1) I can't figure out how to perform an element-wise division. vec.cwise()/scalar
does not work


You want to divide all coeffs of a vector by the same scalar? Then that's a plain division, no need for cwise, do:

vec / scalar;

Otherwise, to perform a cwise quotient by another vector, you would do vec.cwise()/othervector, and that would require you to #include.

and I have to write a loop for this. Is there any technical reason why "/" operator does not work with cwise()?


The reason why it works but doesn't want you to write "cwise()" is that it is a basic linear-algebraic operation. Cwise is for operations that don't make much sense from the point of view of linear algebra but are useful for dealing with arrays.

2) I have a function which takes a vector like this:

void some_func(VectorXd& vec);

and I want to pass a column of the matrix to it, but some_func(matr.col(i)) does not work because of type mismatch. Is there any intelligent way of doing this, or I have to make a copy of the column in vector object first?


Yes,

Code: Select all
template
void some_func(const MatrixBase& vec);


Notice I made the reference constant because C++ doesn't want non-constant references to temporaries. You can always const-cast if you must.

Or if you don't want to make your function templated, you can always keep your old function and call it like this:
Code: Select all
some_func(matr.col(i).eval())

However this kills the possibility of lazy evaluation and doesn't give write access.


Thank you very much for reply! Now it's clear.


Bookmarks



Who is online

Registered users: Bing [Bot], Evergrowing, Google [Bot], rblackwell