2017-06-03 35 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>

也許這將幫助你更好地理解編譯器錯誤;正如您在查看編譯錯誤消息的其餘部分所看到的,您可以看到,在嘗試執行此模板的特化時,這很明顯。