本征系数自定义函数 [英] Coefficient-wise custom functions in Eigen
本文介绍了本征系数自定义函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个 do_magic
方法,它需要加倍并加42。我想将此方法应用于 Eigen :: Matrix
或 Eigen :: Array
的每个系数(即意思是,我不介意是否只有两种类型都可以。)
I have a do_magic
method which takes a double and adds 42 to it. I'd like to apply this method to each coefficient of a Eigen::Matrix
or Eigen::Array
(that means, I wouldn't mind if it's only possible with one of both types).
有可能吗?
Like this:
Like this:
Eigen::MatrixXd m(2, 2);
m << 1,2,1,2;
m.applyCoefficientWise(do_magic);
// m is now 43, 44, 43, 44
推荐答案
您可以使用 unaryExpr
,尽管这将返回矩阵的新视图,而不是允许您修改适当的元素。
You can use unaryExpr
, though this returns a new view onto the matrix, rather than allowing you to modify the elements in place.
将示例复制到文档之外:
Copying the example out of the documentation:
double ramp(double x)
{
if (x > 0)
return x;
else
return 0;
}
int main(int, char**)
{
Matrix4d m1 = Matrix4d::Random();
cout << m1 << endl << "becomes: " << endl << m1.unaryExpr(ptr_fun(ramp)) << endl;
return 0;
}
这篇关于本征系数自定义函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文