C++ 如何编写一个函数,返回稠密本征对象上一元表达式的输出?

C++ 如何编写一个函数,返回稠密本征对象上一元表达式的输出?,c++,eigen,C++,Eigen,例如,假设我想编写一个函数,检查密集特征对象中的每个浮点数是否为normal,如果为normal,则返回相应位置的1.0,如果为normal,则返回0.0。输出不能立即求值,最好采用表达式的形式 我已经通读了,这让我尝试了以下几点: #include <functional> #include <Eigen/Dense>

例如,假设我想编写一个函数,检查密集特征对象中的每个浮点数是否为
normal
,如果为normal,则返回相应位置的
1.0
,如果为normal,则返回
0.0
。输出不能立即求值,最好采用表达式的形式

我已经通读了,这让我尝试了以下几点:

#include <functional>                                                              

#include <Eigen/Dense>                                                             

template <typename Derived>                                                        
auto Fun(const Eigen::DenseBase<Derived>& matrix) -> decltype(matrix.unaryExpr(std::ptr_fun<typename Derived::Scalar, bool>(std::isnormal)).template cast<typename Derived::Scalar>())
{                                                                                  
    return matrix.unaryExpr(std::ptr_fun<typename Derived::Scalar, bool>(std::isnormal)).template cast<typename Derived::Scalar>();
}                                                                                  

int main()                                                                         
{                                                                                  
    Eigen::Matrix<double, -1, -1> mat;                                             
    mat.resize(100,100);                                                           
    mat.fill(100);                                                                 
    auto mat2 = Fun(mat);                                                          

    return 0;                                                                      
}                                                                                  
#包括
#包括
模板
auto-Fun(const-Eigen::DenseBase&matrix)->decltype(matrix.unaryExpr(std::ptr_-Fun(std::isnormal)).template cast()
{                                                                                  
return matrix.unaryExpr(std::ptr_fun(std::isnormal)).template cast();
}                                                                                  
int main()
{                                                                                  
本征矩阵矩阵;
调整材料尺寸(100100);
填料(100);
自动mat2=乐趣(mat);
返回0;
}                                                                                  
这失败了,错误是没有为
Eigen::DenseBase
类型的对象定义
unaryExpr
,如果我看一下,肯定没有这样的函数

那么,除了每次我想调用这个函数时强制求值,并将本征对象投射到求值的
矩阵
,我该如何实现这一点?

回答如下: