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

CwiseNullaryOp for mixed-type operations?

Tags: None
(comma "," separated)
Ian Mackenzie
Registered Member
Posts
15
Karma
0
I'm using Eigen vector/matrix types with both doubles and Interval objects as scalar types, and I'd like to implement a couple of special interval-related methods. For instance, I'd like to implement a hull() method such that I can write
Code: Select all
VectorXd a;
VectorXd b;
VectorXI c; // vector of Intervals
// initialize a and b
c = a.hull(b)

which would result in c having each coefficient be the hull (Interval containing both) of the corresponding coefficients in a and b. The trick, of course, is that while a and b are double-valued, the result is Interval-valued, so as I understand it there's no way to use the CwiseBinaryOp class.

I was thinking of implementing a class based on CwiseNullaryOp that would take a couple const reference parameters (a and b) at construction and specify an Interval return type...it seems to me this should work, but I thought I should check to see if there was something I was missing. For instance, does hiding the two arguments this way mean that there could be issues with aliasing?
User avatar
bjacob
Registered Member
Posts
658
Karma
3
Ian Mackenzie wrote:I was thinking of implementing a class based on CwiseNullaryOp that would take a couple const reference parameters (a and b) at construction


So what you're trying to do here is not a nullary operation, it's a binary operation. See CwiseBinaryOp. There is a function already to apply a custom functor as binary op, see binaryExpr().

and specify an Interval return type...it seems to me this should work, but I thought I should check to see if there was something I was missing. For instance, does hiding the two arguments this way mean that there could be issues with aliasing?


There's no aliasing problem in coeff-wise operations. Aliasing problems arise in non-coeff-wise operations, when reading back an already overwritten value.


Join us on Eigen's IRC channel: #eigen on irc.freenode.net
Have a serious interest in Eigen? Then join the mailing list!
Ian Mackenzie
Registered Member
Posts
15
Karma
0
bjacob wrote:So what you're trying to do here is not a nullary operation, it's a binary operation. See CwiseBinaryOp. There is a function already to apply a custom functor as binary op, see binaryExpr().

You're right, that was a bad example...I should have mentioned that I also would ideally like to be able to mix double/Interval vectors, so it's possible to write things like
Code: Select all
Vector3d a, b, c;
Vector3I d;
d = a.hull(b).hull(c)

since the result of the first hull() would be an Interval-valued expression, and as I understand it it's not possible to have a binary expression with different scalar types. But perhaps the simplicity of just using binaryExpr() makes up for the slight loss in syntactic sugar of not being able to mix scalar types...
User avatar
bjacob
Registered Member
Posts
658
Karma
3
OK, it's true that CwiseBinaryOp requires same scalar type (except for a few ops where it allows mixing real and complex).

So, yes your idea of doing a nullary/unary op and "artificially" passing the other(s) operand(s) would work.

But you can also simply write your new expression class from scratch (hint: a good simple example of an expression class is Minor in Eigen 2) and construct objects of it with either a global function or a new matrixbase method that you inject with EIGEN_MATRIXBASE_PLUGIN.


Join us on Eigen's IRC channel: #eigen on irc.freenode.net
Have a serious interest in Eigen? Then join the mailing list!
Ian Mackenzie
Registered Member
Posts
15
Karma
0
Thanks! And yes, Minor does seem like a nice example...


Bookmarks



Who is online

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