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

Porting from uBLAS: empty matrix and block

Tags: None
(comma "," separated)
ben2610
Registered Member
Posts
7
Karma
0
I am porting a software from uBLAS to Eigen2. So far so good but I noticed that one feature of uBLAS is not supported in Eigen2: in uBLAS you can define empty matrices and blocks. Using such entity in any operation is equivalent to a no-op.
in Eigen2, any attempt to create an empty matrix or block causes an assert.

I use empty block in my application to populate and use larger matrices from objects that may or may not implement certain features: an object that does not implement a feature will take an empty block from the matrix and this will have no effect on the operations in which the block is used (assignement, product, etc). The advantage is that I don't need to check in the application before calling the uBLAS operation.

Any chance this feature is supported in Eigen2? I would find consistent that Eigen2 can handle empty matrices and blocks. After all, 0 is the first positive number.

Last edited by ben2610 on Fri Mar 06, 2009 3:18 pm, edited 1 time in total.
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
hm... actually I think that's better and cleaner to explicitly check whether the object has the feature or not before using it, no?

anyway, about relaxing the assertions why not, but note that some operations will still raise an assertion on a null matrix, typically all reduction functions (minCoeff(), prod(), sum(), etc.). Well for sum it would make sense to return 0 but I don't want to add a runtime "if" in sum() just for that case. Benoit, what is your opinion ?

Actually, you can already try to compile with -DNDEBUG (and -DEIGEN_NO_STATIC_ASSERT if you have compile time assertion) and see if that works right away (I guess it should).
ben2610
Registered Member
Posts
7
Karma
0
Indeed, compiling in Release mode removes the assert but it doesn't work as expected. For example this code:
S += A.block(0,1,3,0) * T.block(1,0,0,1);
operates the same as this one:
S += A.block(0,1,3,1) * T.block(1,0,1,1);
i.e. length 0 is promoted to 1.
In this case I would expect S to be left untouched.

I really think that empty matrix and blocks should be a valid entity in Eigen2. It's like the memset() function, you can call it with length 0, it just doesn't do anything.
Empty matrix and block should automatically be handled in matrix expressions as no-op. I agree that any member function returning a scalar should assert if called on an empty matrix because you have no control on what the application does with the scalar and you have no way to return a valid scalar. But for members that return a matrix, you can handle the empty case correctly, just like uBLAS does.

In my code, I added many checks to go around the empty cases. Not a big deal but it doesn't look clean.
User avatar
bjacob
Registered Member
Posts
658
Karma
3
ben2610 wrote:Indeed, compiling in Release mode removes the assert but it doesn't work as expected. For example this code:
S += A.block(0,1,3,0) * T.block(1,0,0,1);
operates the same as this one:
S += A.block(0,1,3,1) * T.block(1,0,1,1);


This is because the per-coefficient loop in matrix product is already a reduction, and as Gael mentioned, these indeed would require special care and a if() to support the zero case.

Like Gael, I'm against that because it adds a constant overhead.

Given that matrix products are such a basic operation, I don't think then that it's worth changing the rest.


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
And, notice that these are really small blocks you're handling here, so, just as performance tip: use block(i,j) if nrows and ncols are known at compile time.


Join us on Eigen's IRC channel: #eigen on irc.freenode.net
Have a serious interest in Eigen? Then join the mailing list!


Bookmarks



Who is online

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