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

Questions about Block

Tags: None
(comma "," separated)
Zmatt
Registered Member
Posts
27
Karma
0
OS

Questions about Block

Tue Jul 27, 2010 8:43 am
Hi !

I have (again ^^) a new question about Eigen. I precise the situation : I'm wrapping Eigen to preserve the compatibility with an older library and it works well, all my functionnalities are there and the result is fast. But there is still a problem and this problem deals with the Block.

How can I write functions that work when I call them with :
f(A.col(1), B.row(2))

Usually I write my functions in a new file using my matrix class (that inherits from MatrixXd) like
matrix f(matrix A, matrix B) { ... }
Nevertheless A.row(1) is a Block (a matrix::Block indeed) so my function doesn't match. One solution could be to defined new functions but it will be very long... I try to redefined row and col in my matrix class but I don't find someting that works.

Has someone an idea ?
Thanks :)
Zmatt
Registered Member
Posts
27
Karma
0
OS

Re: Questions about Block

Wed Jul 28, 2010 8:03 am
EDIT : Well I was a bit wrong a finaly after some tests it works for functions :)
But i had to create special function for make this legal : A.col(1) <= 0.5

But i've still a problem : how can i create a function that returns a block, to create for instance a function sub that makes the same work than .block ?
Thanks for any help ;)

EDIIT BIS : Why can I do :
Code: Select all
typename BlockReturnType< Derived >::Type
cole(int c1, int c2)
{
   return derived().block(0, c1 -1 , cols(), c2);
}

But not
Code: Select all
typename BlockReturnType< Derived >::Type
col(int c1, int c2)
{
   return derived().block(0, c1 -1 , cols(), c2);
}
(i just change col in cole) oO !
Eigen is still full of mysteries :p !
User avatar
bjacob
Registered Member
Posts
658
Karma
3

Re: Questions about Block

Wed Jul 28, 2010 2:48 pm
Zmatt wrote:Hi !

I have (again ^^) a new question about Eigen. I precise the situation : I'm wrapping Eigen to preserve the compatibility with an older library and it works well, all my functionnalities are there and the result is fast. But there is still a problem and this problem deals with the Block.

How can I write functions that work when I call them with :
f(A.col(1), B.row(2))

Usually I write my functions in a new file using my matrix class (that inherits from MatrixXd) like
matrix f(matrix A, matrix B) { ... }


I really hope that you're rather passing A and B by reference!

http://eigen.tuxfamily.org/dox-devel/To ... Value.html

Nevertheless A.row(1) is a Block (a matrix::Block indeed) so my function doesn't match. One solution could be to defined new functions but it will be very long... I try to redefined row and col in my matrix class but I don't find someting that works.

Has someone an idea ?


Classical question but indeed Google doesn't find easily our past answers... definitely need to write a good docs page about that.

Here goes: all expression types T inherit MatrixBase<T> so you can just do:

Code: Select all
template<typename T>
void myfunction(const MatrixBase<T>& x)
{
   ...
}


Join us on Eigen's IRC channel: #eigen on irc.freenode.net
Have a serious interest in Eigen? Then join the mailing list!
User avatar
bjacob
Registered Member
Posts
658
Karma
3

Re: Questions about Block

Wed Jul 28, 2010 2:51 pm
Zmatt wrote:EDIT : Well I was a bit wrong a finaly after some tests it works for functions :)
But i had to create special function for make this legal : A.col(1) <= 0.5

But i've still a problem : how can i create a function that returns a block, to create for instance a function sub that makes the same work than .block ?
Thanks for any help ;)

EDIIT BIS : Why can I do :
Code: Select all
typename BlockReturnType< Derived >::Type
cole(int c1, int c2)
{
   return derived().block(0, c1 -1 , cols(), c2);
}

But not
Code: Select all
typename BlockReturnType< Derived >::Type
col(int c1, int c2)
{
   return derived().block(0, c1 -1 , cols(), c2);
}
(i just change col in cole) oO !
Eigen is still full of mysteries :p !



I guess just because Eigen already defines a templated method col() and your own col() method would shadow it...? C++ definitely allows overloaded template method declarations but I'm not sure if it allows to have one templated and one non-templated overload.


Join us on Eigen's IRC channel: #eigen on irc.freenode.net
Have a serious interest in Eigen? Then join the mailing list!
Zmatt
Registered Member
Posts
27
Karma
0
OS

Re: Questions about Block

Wed Jul 28, 2010 3:15 pm
Ok, but it compiles in fact !

But when I use A.col(1,2) for instance I get a horrible
error C2661: 'matrix::col' : aucune fonction surchargée ne nécessite 2 arguments (no overloaded function takes 2 arguments)
:(
Zmatt
Registered Member
Posts
27
Karma
0
OS

Re: Questions about Block

Thu Jul 29, 2010 2:27 pm
I found a solution to my problem :)

The functions have to be in the matrix class ! (and no "typename" is needed)

I have a "last but not least question" ^^ :

How can i simply add a new operator+ and - for the Scalar type (double in fact) to do A + 1 that does the same work than A.cwise() + 1 ?
I added some functions
Code: Select all
matrix operator+ (double r) const { return derived().cwise() + r; }
matrix operator- (double r) const { return derived().cwise() - r; }

But no result :S

(Nevertheless the addition 1 + A works with the function :
Code: Select all
matrix operator+ (double r, const matrix &x) { return x.cwise() + r; }
)

Thanks for any help and for the patience of bjacob and ggael (I ask lots of questions :< )

NB : tell me if I need the create a new subject
User avatar
bjacob
Registered Member
Posts
658
Karma
3

Re: Questions about Block

Thu Jul 29, 2010 3:06 pm
This really shouldn't go in the matrix class, as that means that you can't call that on an expression...

For your operator +/-, since this is a coefficient-wise unary operator, you could implement these methods as returning a CwiseUnaryOp. Don't return a plain matrix as that will be inefficient (creates a temporary).


Join us on Eigen's IRC channel: #eigen on irc.freenode.net
Have a serious interest in Eigen? Then join the mailing list!
Zmatt
Registered Member
Posts
27
Karma
0
OS

Re: Questions about Block

Mon Aug 02, 2010 3:59 pm
Thanks that works now :) (but it returns still a plain matrix, I need to return a CwiseOp as you said)


New question related to the function with blocks ^^ :
If have a function
Code: Select all
void tralala(const matrix& p) {
   p(0,0) = 42;
}


How can I call it to do :
Code: Select all
tralala(A.col(1));

For the moment it's impossible because Eigen can't convert the parameter
error C2664: 'tralala' : impossible to convert the parameter 1 of 'Eigen::Block<MatrixType,BlockRows,BlockCols>' in 'matrix &'


A solution can be to rewrite col, and col will return a matrix where the datas are just pointing on the matrix A (for A.col(1)) but I don't know how to do that :S
User avatar
bjacob
Registered Member
Posts
658
Karma
3

Re: Questions about Block

Tue Aug 03, 2010 1:12 am
see my first answer above, your function needs to take a matrixbase...


Join us on Eigen's IRC channel: #eigen on irc.freenode.net
Have a serious interest in Eigen? Then join the mailing list!
Zmatt
Registered Member
Posts
27
Karma
0
OS

Re: Questions about Block

Tue Aug 03, 2010 7:34 am
Yes yes, I know :)

But I want to preserve the prototypes of my functions : f(matrix A&)
is it possible ?
User avatar
bjacob
Registered Member
Posts
658
Karma
3

Re: Questions about Block

Tue Aug 03, 2010 1:58 pm
No, it's not possible! Block expressions have a different type than matrices. The only thing they have in common is that they each inherit some specialization of MatrixBase<T> ... but for different types T, whence the need to make that a template parameter.


Join us on Eigen's IRC channel: #eigen on irc.freenode.net
Have a serious interest in Eigen? Then join the mailing list!
Zmatt
Registered Member
Posts
27
Karma
0
OS

Re: Questions about Block

Tue Aug 03, 2010 2:09 pm
Arf :(

I tried to do something with m_data, I code a method that return a part of the matrix but naturally I have memory problems :-\

I will probably stop that idea ^^ and use the right method (but I have lots of lines to rewrite :( )

Thanks for your help !

(and if a specialist of m_data know how to do a "col" method that returns a MatrixXd without a segfault I'm still interrested ^^)


Bookmarks



Who is online

Registered users: Bing [Bot], blue_bullet, Google [Bot], rockscient, Yahoo [Bot]