【问题标题】:Using Eigen::Geometry module with custom scalar type使用具有自定义标量类型的 Eigen::Geometry 模块
【发布时间】:2013-11-15 21:30:30
【问题描述】:

II 想在我目前正在从事的几何项目中将 Eigen 用于线性代数。这是一个很棒的库,相对较小且易于使用且足够流行。

但是,我还想使用自定义定义的“Double”类来比较计算机精度中的两个浮点值(例如两者之间的差异必须小于给定精度)。对于我的自定义类型,我已经实现了大部分 std::math c++11 函数和所有运算符(包括一元 -、conj、imag、abs2...)。我做了这些链接中列出的所有事情:

http://eigen.tuxfamily.org/dox-devel/TopicCustomizingEigen.html

https://forum.kde.org/viewtopic.php?f=74&t=26631

但是,我仍然在 Eigen Jacobi.h 文件中遇到编译错误,更具体地说是在第 340,341 行:

x[i] =  c * xi + numext::conj(s) * yi;
y[i] = -s * xi + numext::conj(c) * yi;

我从编译器收到以下错误(Vs 2012、Win32、发布和调试配置)

eigen\src/Jacobi/Jacobi.h(341): error C3767: '*': candidate function(s) not accessible

operator* 在我的自定义类型中定义为以下情况:

CustomType operator*(CustomType const &_other);
CustomType operator*(double const &_other);
double operator*(CustomType const &_other);

我尝试用以下方式定义 conj :

CustomType conj(CustomType const &_type){return _type;}
double conj(customType const &_type){return _type.get();}

我尝试在 Eigen::numext 命名空间以及我的 CustomType 命名空间中定义 conj,但没有成功。 任何人都有提示、链接、建议或知道 Eigen 需要的东西我可能忘记了吗?

【问题讨论】:

    标签: c++ c++11 geometry eigen


    【解决方案1】:

    很可能是因为您的代码不是const correct

    您的运算符重载应该是:

    CustomType operator*(CustomType const &_other) **const**;
    CustomType operator*(double const &_other) **const**;
    double operator*(CustomType const &_other) **const**;
    

    如果 eigen 对 CustomType 类型的对象有 const 引用,则它不能调用您的运算符,因为它们没有声明为 const。

    void foo(const CustomType& x, const CustomType& y){
        x * y; // Compile error, cannot call non-const operator * on const object.
    }
    

    【讨论】:

    • 令人印象深刻,我完全错过了。好吧,这有点晚了,但它仍然是一个很好的答案。
    【解决方案2】:

    不确定这是否是这里的问题,但您必须专攻Eigen::NumTraits<T>。看到这个manpage

    另外我不明白你怎么能有两个 conj 函数,它们具有相同的参数,但返回不同的类型。

    【讨论】:

    • 我的意思是,我尝试了函数 conj 的两个版本。以防万一。我确实将 Eigen::NumTraits 专门化在 Eigen 命名空间中的一个单独的头文件中。我复制了第一个链接中的示例。也许它需要更多?
    猜你喜欢
    • 2017-07-18
    • 1970-01-01
    • 2018-05-19
    • 2020-03-19
    • 2019-01-03
    • 2018-11-03
    • 2016-04-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多