2017-06-03 36 views
2

我正在使用Eigen,我正在尝试编写一个函数来操作矩阵的行。我遵循guidelines in the docs但我没有尝试编译(使用clang或g ++);我在我的智慧结束。实际上应该如何编写将使用RowXpr的函数?本征,函数RowXpr

仅供参考,这里是我到目前为止已经试过:

#include <iostream> 
#include <Eigen/Core> 

using namespace std; 
using namespace Eigen; 

constexpr Eigen::StorageOptions Order = ColMajor; 

using vect_t = Matrix<double, 1, 3, Order>; 
using matr_t = Matrix<double, Dynamic, 3, Order>; 

#define FUNC 3 

#if FUNC == 1 

vect_t func(const vect_t& f) 
{ 
    return f; 
} 

#elif FUNC == 2 

vect_t func(const Ref<vect_t>& f) 
{ 
    return f; 
} 

#elif FUNC == 3 

template<class D> vect_t func(const MatrixBase<D>& f) 
{ 
    return f; 
} 

#endif 

int main() 
{ 
    matr_t M = matr_t::Random(5,3); 
    cout << M << endl; 
    cout << func(M.row(2)) << endl; 

    return 0; 
} 

谢谢!

编辑:

铿锵(3.8.0-2ubuntu4版)我得到的错误是如下。该错误与g ++相当。

[email protected]:~/workspace/scratch$ clang++ eigen_func_test.cpp -I /home/dan/Downloads/eigen_3.3.3/ --std=c++11 && ./a.out 
In file included from eigen_func_test.cpp:2: 
In file included from /home/dan/Downloads/eigen_3.3.3/Eigen/Core:436: 
/home/dan/Downloads/eigen_3.3.3/Eigen/src/Core/PlainObjectBase.h:899:7: error: static_assert failed 
     "INVALID_MATRIX_TEMPLATE_PARAMETERS" 
    ...EIGEN_STATIC_ASSERT((EIGEN_IMPLIES(MaxRowsAtCompileTime==1 && MaxColsAtCompileTime!=1, (Options&RowMajor)==RowMajor) 
    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
/home/dan/Downloads/eigen_3.3.3/Eigen/src/Core/util/StaticAssert.h:32:40: note: expanded from macro 
     'EIGEN_STATIC_ASSERT' 
    #define EIGEN_STATIC_ASSERT(X,MSG) static_assert(X,#MSG); 
            ^   ~ 
/home/dan/Downloads/eigen_3.3.3/Eigen/src/Core/PlainObjectBase.h:535:7: note: in instantiation of member function 
     'Eigen::PlainObjectBase<Eigen::Matrix<double, 1, 3, 0, 1, 3> >::_check_template_params' requested here 
     _check_template_params(); 
    ^
/home/dan/Downloads/eigen_3.3.3/Eigen/src/Core/Matrix.h:379:9: note: in instantiation of function template 
     specialization 'Eigen::PlainObjectBase<Eigen::Matrix<double, 1, 3, 0, 1, 3> 
     >::PlainObjectBase<Eigen::Block<Eigen::Matrix<double, -1, 3, 0, -1, 3>, 1, 3, false> >' requested here 
     : Base(other.derived()) 
     ^
eigen_func_test.cpp:32:9: note: in instantiation of function template specialization 'Eigen::Matrix<double, 1, 3, 0, 
     1, 3>::Matrix<Eigen::Block<Eigen::Matrix<double, -1, 3, 0, -1, 3>, 1, 3, false> >' requested here 
     return f; 
      ^
eigen_func_test.cpp:41:10: note: in instantiation of function template specialization 
     'func<Eigen::Block<Eigen::Matrix<double, -1, 3, 0, -1, 3>, 1, 3, false> >' requested here 
     cout << func(M.row(2)) << endl; 
       ^
1 error generated. 
+0

你得到了什么编译器错误?我所看到的唯一可能存在问题的两件事就是你的函数模板期望返回一个'vect_t'类型,并且你返回'MatrixBase '的'const ref'。同样在你的主函数中,另外一个问题可能是你试图打印'matr_t'类型的'M',而不看文档,我不知道它们是否有一个重载的输出流操作符。 –

+0

嗨@FrancisCugler,这与cout无关,如果我注释掉函数调用,此行将起作用。我已经添加了一个编译器输出的例子。 – Dan

+0

好吧,我现在可以看到它不是来自流操作符,而是来自模板参数本身。这似乎来自您的函数模板。我可以添加一个答案,因为我不知道这个库,所以我可以尽我所能去尝试给你一些关于我认为编译器试图对你的源代码进行尝试的知识。 –

回答

1

如果你读通过铛

EIGEN_STATIC_ASSERT((EIGEN_IMPLIES(MaxRowsAtCompileTime==1 && MaxColsAtCompileTime!=1, (Options&RowMajor)==RowMajor) 

你看,如果一个矩阵有在编译时一行和多列,它必须是RowMajor突出的部分。这意味着,如果设置Order = RowMajor或者只是离开了, Order

using vect_t = Matrix<double, 1, 3, Order>; 

你的代码应该编译罚款(它看起来像一切都只是后续的错误)。

0

不完全确定什么是直接导致编译器错误,而是源于您在源代码中显示的内容以及编译器错误。这是编译器错误消息的一部分,我基于此。

/home/dan/Downloads/eigen_3.3.3/Eigen/src/Core/PlainObjectBase.h:899:7: error: static_assert failed 
    "INVALID_MATRIX_TEMPLATE_PARAMETERS" 

因此,在源代码中寻找您要使用#define func3

你已经宣布为在:与使用

template<class D> vect_t func(const MatrixBase<D>& f) { 
    return f; 
} 

您声明的指令为:

constexpr Eigen::StorageOptions Order = ColMajor; 

using vect_t = Matrix<double, 1, 3, Order>; 
using matr_t = Matrix<double, Dynamic, 3, Order>; 

因此,让我们这个扩展为完整的表格,看看编译器试图解析或推断为您的参数和返回类型。

template<class D> 
Matrix<double, 1, 3, constexpr Eigen::StorageOptions Order = ColMajor> 
func(const MatrixBase<D>& f) { 
    return f; 
} 

然后在你的主,你使用这个具有:

int main() { 
    // matr_t M = matr_t::Random(5,3); 
    Matrix<double, Dynamic, 3, constexpr Eigen::StorageOptions Order = ColMajor> M 
     = Matrix<double, Dynamic, 3, constexpr Eigen::StorageOptions Order = ColMajor>::Random(5, 3); 

    // Then you call your function passing it this: 
    std::cout << func(M.row(2)) << std::endl; 


    return 0; 
} 

func()将返回Matrix<double, 1, 3, Order>; ,它需要const MatrixBase<D>& 但是似乎你路过它:M.row(2) 是从构造:Matrix<double, Dynamic, 3, constexpr Eigen::StorageOptions Order = ColMajor>::Random(5, 3)

在函数本身中你将返回f w HICH恰好是 函数的MatrixBase<D>参数const ref尚未申报期待返回一个Matrix<double, 1, 3, Order>

所以我觉得编译器有一个很难尝试转换 Matrix<double, Dynamic, 3, Order>Matrix<double, 1, 3, Order>

也许这将帮助你更好地理解编译器错误;正如您在查看编译错误消息的其余部分所看到的,您可以看到,在尝试执行此模板的特化时,这很明显。