Registered Member
|
[SOLVED] The "right" way to make functions that take as input N MatrixXf and return N MatrixXf
Wed Jan 14, 2009 1:16 pm
Hi there,
This has been puzzling me for some time and I'm guessing it's a bit of a newbie question. I'm often writing functions that take as input several MatrixXf and return several MatrixXf. Until now I have been doing that the following way (simplified here for the example): Contents of addsub.h
Contents of my_program.cpp:
And here is the command to compile I use:
I would appreciate a lot any help to making work the last part (commented out) in the program. The reason I want to do this, is that I want my function to be able to work on both: - uninitialized matrices (by resizing them and therefore allocating itself the memory) - initialized matrices (where the memory has been previously allocated (like when working on buffers)) Thanks in advance. ricard
Last edited by bjacob on Sat Jan 17, 2009 7:30 pm, edited 1 time in total.
|
Registered Member
|
RE: The "right" way to make functions that take as input N MatrixXf and return N MatrixXf
Wed Jan 14, 2009 2:11 pm
Here your addSub function takes a and b by value which means that when you call addSub, copies of a and b are made and addSub works on these copies. That's useless, inefficient. Moreover it would give you bad errors if instead of MatrixXd you had, say, Matrix4d. So you should rewrite this as:
and read this page: http://eigen.tuxfamily.org/dox/PassingByValue.html
In order to make this work with both initialized and non-initialized matrices,you need to use set() like this:
Notice set() takes care of resizing for you. Also notice that in C/C++ instead of (*c). you can just do c->. Contents of my_program.cpp:
Aaaah but then what you are passing to addSub aren't matrices, these are expressions. So you need to write addSub like that:
However if addSub is declared like that, you can no longer call set() and resize() on c and d, because these methods are only of plain matrices and now c and d are expressions. So you'd have to initialize c and d with the correct size before calling addSub.
You have to make a compromise here: - if you do like the first version i give you, then it works on both initialized and uninitialized matrices, but these are plain matrices not expressions so you can't let c and d be corner's. - if you do like the 2nd version, c and d are expressions so they can be corner's but the thing is, even before you can call corner() on a matrix, this matrix has to be initialized.
Join us on Eigen's IRC channel: #eigen on irc.freenode.net
Have a serious interest in Eigen? Then join the mailing list! |
Registered Member
|
RE: The "right" way to make functions that take as input N MatrixXf and return N MatrixXf
Thu Jan 15, 2009 4:49 pm
Great! the explanation was fantastic and it helped me better understand how to use Eigen. Now I remade the example in order to fit my needs. But I get a compile warning which I don't understand, maybe you can help me get rid of it:
First of all the warning:
Contents of my_program.cpp:
And to compile:
I think I've addressed all your comments Benoit. PS: is there a better (as in more often used) name than *Buffer for these kind of methods that work on a preallocated matrix? EDIT: Fix that I had forgotten the const MatrixXf& in the addSub() parameters.
Last edited by rikrd on Thu Jan 15, 2009 4:52 pm, edited 1 time in total.
|
Registered Member
|
RE: The "right" way to make functions that take as input N MatrixXf and return N MatrixXf
Thu Jan 15, 2009 5:16 pm
Your compiler doesn't like these lines:
You can fix that by letting this function take references instead of pointers:
about the naming question, I would call that function matrixAddSub or something like that.
Join us on Eigen's IRC channel: #eigen on irc.freenode.net
Have a serious interest in Eigen? Then join the mailing list! |
Registered Member
|
RE: The "right" way to make functions that take as input N MatrixXf and return N MatrixXf
Mon Jan 19, 2009 7:56 pm
I hope not to be abusing from this forum, but just for the record...
With the help of ggael and bjacob I finally got this to work by turning into const references the output arguments of addSubBuffer() and using const_cast inside the function to actually modify them. This is not ideal, but it permits to do the calls I wanted to allow (calling a function on a part (Block) of a Matrix as well as on the whole Matrix). Actually this is far from ideal, because to a user of the function it will be confusing that the const reference will not be treated as const and will actually be modified (it has to be clear in the doc!). Contents of addsub.h:
contents of my_program.cpp:
Hope this helps the next one running into this problem. ricard |
Registered users: Bing [Bot], Evergrowing, Google [Bot], rblackwell