eigen 中的组件式条件/分段定义运算
有没有办法使用 eigen 来实现类似(伪代码):
A = BooleanExpr(X) ? ExprTrue : ExprFalse;
其中所有变量都是 Eigen::Array 的。也就是说,对于 X 的每个分量,如果该分量上的 BooleanExpr 为 true,则 A 的相应分量计算为 ExprTrue,否则计算为 ExprFalse。
我现在的实现是这样的:
COND = BooleanExpr(X).cast<double>();
A = COND * ExprTrue + (1-COND) * ExprFalse;
但是这会计算每个组件上的 Expr* ,当 Expr 很昂贵时,这感觉不太对劲。
事实上,我想将其推广到 X 的分段定义函数,因此我可以计算类似(伪代码)的内容:
A = PieceExpr[ IntExpr(X) ] ( B );
即 X 分量上的整数表达式的结果确定用于计算 A 的相应分量的表达式。
也许我只是走错了路,并且已经有一种方法可以在特征中实现相同的结果,我只是想不通。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来您需要 .select() 如 http://eigen.tuxfamily.org/dox-devel/classEigen_1_1DenseBase.html#a7c7f8804e216885f49b70f61c7ae3bbb (我认为这适用于数组和矩阵,与文档的建议相反)。
对于更一般的情况,除了嵌套选择之外,我不知道实现此目的的好方法。
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.