特征,RowXpr的功能

时间:2017-06-03 20:22:36

标签: c++ templates eigen

我正在使用Eigen,我正在尝试编写一个函数来处理矩阵的行。我跟着guidelines in the docs但我没有尝试编译(使用clang或g ++);我的智慧结束了。如何实际编写将占用RowXpr的函数?

作为参考,这是我到目前为止所尝试的内容:

can't compare offset-naive and offset-aware datetimes

谢谢!

修改

使用clang(版本3.8.0-2ubuntu4),我得到的错误如下。该错误与g ++相当。

#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;
}

2 个答案:

答案 0 :(得分:1)

如果您阅读了由clang

突出显示的部分
EIGEN_STATIC_ASSERT((EIGEN_IMPLIES(MaxRowsAtCompileTime==1 && MaxColsAtCompileTime!=1, (Options&RowMajor)==RowMajor)

你知道,如果一个矩阵在编译时有一行而且有一列以上,那么它必须是RowMajor。这意味着,如果您设置了Order = RowMajor或者只是忽略了, Order

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

你的代码应该编译得很好(看起来其他一切都只是后续错误)。

答案 1 :(得分: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;
}

使用您声明为:

的using指令
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 const ref参数的函数MatrixBase<D>但声明预计会返回Matrix<double, 1, 3, Order>

所以我认为编译器很难尝试转换 Matrix<double, Dynamic, 3, Order>Matrix<double, 1, 3, Order>

也许这会帮助您更好地理解编译器错误;正如您在查看编译错误消息的其余部分时所看到的那样,当您尝试执行此模板的特化时,可以看出这是显而易见的。