【问题标题】:Template type casting of Eigen Matrix library特征矩阵库的模板类型转换
【发布时间】:2015-08-15 06:09:13
【问题描述】:

我正在尝试使用 Eigen 库对模板化矩阵进行类型转换。

function( const Eigen::MatrixBase < Derived1 > &mat1,
                Eigen::MatrixBase < Derived2 > &mat2 )
{
    mat2 = coefficient * mat1.derived().cast < Derived2::Scalar >();
}

它不工作。 有人可以帮助我正确的语法。

【问题讨论】:

    标签: c++ eigen


    【解决方案1】:

    您的函数签名不完整,但我想您缺少的主要内容是使用 template keyword for the function call

    mat2 = coefficient * mat1.template cast <typename Derived2::Scalar> ();
    

    一个完整的工作示例:

    #include <eigen3/Eigen/Core>
    #include <iostream>
    
    template<typename Derived1, typename Derived2>
    void mul(const typename Derived1::Scalar& coefficient,
             const Eigen::MatrixBase<Derived1>& mat1,
             Eigen::MatrixBase<Derived2>& mat2)
    {
      mat2 = coefficient * mat1.template cast <typename Derived2::Scalar> ();
    }
    
    int main() {
    
      Eigen::Matrix3f a;
      a << 1.0, 0.0, 0.0,
           0.0, 2.0, 0.0,
           0.0, 0.0, 3.0;
    
      Eigen::Matrix3i b;
      b << 1, 0, 0,
           0, 1, 0,
           0, 0, 1;
    
      mul(3.5, a, b);
    
      std::cout << b << "\n";
    
      return 0;
    }
    

    编译和运行时会打印出来

    3 0 0
    0 6 0
    0 0 9
    

    到标准输出。

    【讨论】:

    • obj.cast&lt;double&gt;()obj.template cast&lt;double&gt;() 之间有什么不同吗?
    猜你喜欢
    • 1970-01-01
    • 2021-01-24
    • 1970-01-01
    • 1970-01-01
    • 2012-01-16
    • 2022-12-23
    • 1970-01-01
    • 2017-12-30
    • 1970-01-01
    相关资源
    最近更新 更多