component-wise conditional / piecewise-defined operations in eigen
Question
Is there a way to implement using eigen something like (pseudocode):
A = BooleanExpr(X) ? ExprTrue : ExprFalse;
where all variables are Eigen::Array's. That is for each component of X, if BooleanExpr on that component is true, the corresponding component of A is computed as ExprTrue, otherwise it's ExprFalse.
I implement it at the moment like:
COND = BooleanExpr(X).cast<double>();
A = COND * ExprTrue + (1-COND) * ExprFalse;
But this computes both Expr* on each component, which doesn't feel right when Expr's are expensive.
In fact I would like to generalize this to a piecewise-defined function of X, so I can compute something like (pseudocode):
A = PieceExpr[ IntExpr(X) ] ( B );
that is the result of an integer expression on component of X determines expression used to compute the corresponding component of A.
May be I'm just going the wrong way about it and there is a way to achieve the same result in eigen already, I just can't figure it out.
Solution
It looks like you need .select() as documented at http://eigen.tuxfamily.org/dox-devel/classEigen_1_1DenseBase.html#a7c7f8804e216885f49b70f61c7ae3bbb (I think that this works on arrays and matrices, contrary to what the documentation suggests).
For the more general situation, I don't know a nice way to achieve this other than nested selects.