gpt4 book ai didi

c++ - 具有特征类型输出的特征 binaryExpr

转载 作者:行者123 更新时间:2023-11-30 05:08:16 26 4
gpt4 key购买 nike

我在尝试使用 binaryExpr 时遇到问题。这是我第一次使用它,所以我一直在关注 Eigen documentation

对于我的使用,我需要一个具有 Eigen 类型输入和输出的仿函数,但这不想编译,我不明白为什么。我查看了代码中的解释,但我认为这不适用于此处,因为我使用了 float 和一组 float

// We require Lhs and Rhs to have "compatible" scalar types.
// It is tempting to always allow mixing different types but remember that this is often impossible in the vectorized paths.
// So allowing mixing different types gives very unexpected errors when enabling vectorization, when the user tries to
// add together a float matrix and a double matrix.

这是一个简短的使用示例,我需要它来获得相同的编译错误:

#include <eigen3/Eigen/Dense>

using namespace std;
using namespace Eigen;

struct myBinaryFunctor {
EIGEN_EMPTY_STRUCT_CTOR(myBinaryFunctor)
typedef Vector2f result_type;
Vector2f operator()(const Matrix<float,9,1>& a,const float& f) const
{
float x = a.head(4).sum()*f;
float y = a.tail(5).sum()/f;
return Vector2f(x,y);
}
};

int main()
{
constexpr int n = 3;
Matrix<Matrix<float,9,1>,n,n> Ma;
Matrix<float,n,n> F;
Matrix<Vector2f,n,n> R;

for(size_t i = 0, sizeMa = Ma.size(); i<sizeMa; i++)
{
Ma(i).setOnes();
}

F.setConstant(n,n,2);

R = Ma.binaryExpr(F,myBinaryFunctor());

return 0;
}

编译输出为:

/usr/local/include/eigen3/Eigen/src/Core/CwiseBinaryOp.h:107: erreur : static assertion failed: YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY
EIGEN_CHECK_BINARY_COMPATIBILIY(BinaryOp,typename Lhs::Scalar,typename Rhs::Scalar);
^

如果您有一个可以使这项工作成功的解决方案,这对我来说将是一个巨大的帮助 :) 如果没有,我仍然会喜欢解释以了解正在发生的事情。非常感谢。

最佳答案

添加:

namespace Eigen {
template<>
struct ScalarBinaryOpTraits<Matrix<float,9,1>,float,myBinaryFunctor> {
typedef Vector2f ReturnType;
};
}

会完成这项工作。这是因为 Eigen 中明确不允许隐式标量转换,因此您必须明确说明两种不同的标量类型是兼容的。例如,不允许将 VectorXd 添加到 VectorXf

尽管如此,在我看来,您在这里滥用了 Eigen 的功能。

关于c++ - 具有特征类型输出的特征 binaryExpr,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46940914/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com