将任意 Eigen 对象写入行优先普通存储

Write arbitrary Eigen object to row-major plain storage

我正在编写一个模块来将数据写入一个文件,该文件按照惯例仅使用行优先存储。我希望我的函数能够允许列优先和行优先的 Eigen 对象作为输入。

目前我在写之前首先使用 Eigen 将列优先对象复制到行优先对象。我的代码在大多数情况下运行良好,但对于 Eigen::VectorXi 编译失败并出现我不理解的断言。 我该如何解决这个问题? 我可以避免创建很多案例吗?

代码(通过输出一个std::vector模仿写法):

#include <vector>
#include <iostream>
#include <Eigen/Eigen>

template <class T, int Rows, int Cols, int Options, int MaxRows, int MaxCols>
std::vector<T> write(const Eigen::Matrix<T,Rows,Cols,Options,MaxRows,MaxCols>& matrix)
{
    std::vector<T> data(static_cast<size_t>(matrix.size()));

    if (matrix.IsRowMajor) {
        std::copy(matrix.data(), matrix.data()+matrix.size(), data.begin());
        return data;
    } else {
        Eigen::Matrix<T, Rows, Cols, Eigen::RowMajor, MaxRows, MaxCols> tmp = matrix;
        return write(tmp);
    }
}

int main()
{
    Eigen::VectorXi matrix = Eigen::VectorXi::LinSpaced(10, 0, 9);

    std::vector<int> output = write(matrix);
}

编译错误:

In file included from test.cpp:3:
In file included from /usr/local/Cellar/eigen/3.3.7/include/eigen3/Eigen/Eigen:1:
In file included from /usr/local/Cellar/eigen/3.3.7/include/eigen3/Eigen/Dense:1:
In file included from /usr/local/Cellar/eigen/3.3.7/include/eigen3/Eigen/Core:457:
/usr/local/Cellar/eigen/3.3.7/include/eigen3/Eigen/src/Core/PlainObjectBase.h:903:7: error: static_assert failed "INVALID_MATRIX_TEMPLATE_PARAMETERS"
      EIGEN_STATIC_ASSERT((EIGEN_IMPLIES(MaxRowsAtCompileTime==1 && MaxColsAtCompileTime!=1, (Options&RowMajor)==RowMajor)
      ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/local/Cellar/eigen/3.3.7/include/eigen3/Eigen/src/Core/util/StaticAssert.h:33:40: note: expanded from macro 'EIGEN_STATIC_ASSERT'
    #define EIGEN_STATIC_ASSERT(X,MSG) static_assert(X,#MSG);
                                       ^             ~
/usr/local/Cellar/eigen/3.3.7/include/eigen3/Eigen/src/Core/PlainObjectBase.h:535:7: note: in instantiation of member function 'Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 1, -1, 1>
      >::_check_template_params' requested here
      _check_template_params();
      ^
/usr/local/Cellar/eigen/3.3.7/include/eigen3/Eigen/src/Core/Matrix.h:377:9: note: in instantiation of function template specialization 'Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 1, -1, 1>
      >::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >' requested here
      : Base(other.derived())
        ^
test.cpp:14:79: note: in instantiation of function template specialization 'Eigen::Matrix<int, -1, 1, 1, -1, 1>::Matrix<Eigen::Matrix<int, -1, 1, 0, -1, 1> >' requested here
        Eigen::Matrix<T, Rows, Cols, Eigen::RowMajor, MaxRows, MaxCols> tmp = matrix;
                                                                              ^
test.cpp:23:31: note: in instantiation of function template specialization 'write<int, -1, 1, 0, -1, 1>' requested here
    std::vector<int> output = write(matrix);
                              ^
1 error generated.

理解静态断言

不幸的是,这个断言真的不是不言自明的,你唯一能从中得到的是提示,你的模板参数有问题。如果我们查看 Eigen's source code,我们会在第 903 行找到以下开头:

EIGEN_STATIC_ASSERT((EIGEN_IMPLIES(MaxRowsAtCompileTime==1 && MaxColsAtCompileTime!=1, (Options&RowMajor)==RowMajor)
                        && EIGEN_IMPLIES(MaxColsAtCompileTime==1 && MaxRowsAtCompileTime!=1, (Options&RowMajor)==0)
                        && ((RowsAtCompileTime == Dynamic) || (RowsAtCompileTime >= 0))
                        && ((ColsAtCompileTime == Dynamic) || (ColsAtCompileTime >= 0))
                        && ((MaxRowsAtCompileTime == Dynamic) || (MaxRowsAtCompileTime >= 0))
                        && ((MaxColsAtCompileTime == Dynamic) || (MaxColsAtCompileTime >= 0))
                        && (MaxRowsAtCompileTime == RowsAtCompileTime || RowsAtCompileTime==Dynamic)
                        && (MaxColsAtCompileTime == ColsAtCompileTime || ColsAtCompileTime==Dynamic)
                        && (Options & (DontAlign|RowMajor)) == Options),
        INVALID_MATRIX_TEMPLATE_PARAMETERS)

即使编译器指出

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

导致错误,以下行确实如此:

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

了解是什么触发了断言

您提供 Eigen::VectorXi 作为 write 的输入。 Eigen::VectorXi 实际上只是

的类型定义
Eigen::Matrix<int, Eigen::Dynamic, 1, Eigen::ColMajor, Eigen::Dynamic, 1>

因此行

Eigen::Matrix<T, Rows, Cols, Eigen::RowMajor, MaxRows, MaxCols> tmp = matrix;

write 扩展为

Eigen::Matrix<int, Eigen::Dynamic, 1, Eigen::RowMajor, Eigen::Dynamic, 1> tmp = matrix;

触发断言,因为具有 MaxColsAtCompileTime==1MaxRowsAtCompileTime!=1 的矩阵不能是 RowMajor.

解决你的问题

现在的问题是,即使您可以检查输入矩阵是向量、行优先还是列优先,您也不能声明

Eigen::Matrix<T, Rows, Cols, Eigen::RowMajor, MaxRows, MaxCols>

如果在编译时这样做是不合法的(并且不是由于静态断言)。

您可以通过以下选项使代码正常工作:

1. if constexpr (C++17)

C++17 提供了一种在编译时检测是否采用某个条件分支的方法。这种方法的缺点(除 C++17 编译器的要求外)是您只能测试常量表达式。

在具体示例中,它看起来像这样:

template <class T, int Rows, int Cols, int Options, int MaxRows, int MaxCols>
std::vector<T> write(const Eigen::Matrix<T, Rows, Cols, Options, MaxRows, MaxCols>& matrix)
{
  typedef Eigen::Matrix<T, Rows, Cols, Options, MaxRows, MaxCols> MatrixType;
  std::vector<T> data(static_cast<size_t>(matrix.size()));

  if constexpr (MatrixType::MaxRowsAtCompileTime == 1 || 
                MatrixType::MaxColsAtCompileTime ==1 ||
               (MatrixType::Options&Eigen::RowMajor) == Eigen::RowMajor) {
    std::copy(matrix.data(), matrix.data() + matrix.size(), data.begin());
    return data;
  } else {
    Eigen::Matrix<T, Rows, Cols, Eigen::RowMajor, MaxRows, MaxCols> tmp = matrix;
    return write(tmp);
  }
}

2。 SFINAE

您可以通过使用 std::enable_if 在编译时使用 SFINAE 分派对 write 的调用。以下示例使用原始代码的略微修改版本,但从上下文中应该清楚一切:

// matrix is either a vector or in row-major
template <typename Derived>
std::vector<typename Derived::Scalar> write(const Eigen::MatrixBase<Derived>& matrix,
  typename std::enable_if<Derived::MaxRowsAtCompileTime == 1 ||
                          Derived::MaxColsAtCompileTime == 1 ||
                          (Derived::Options & Eigen::RowMajor) == Eigen::RowMajor,
                          Derived>::type* = 0)
{
  std::vector<typename Derived::Scalar> data(
      static_cast<size_t>(matrix.size()));
  std::copy(matrix.derived().data(), matrix.derived().data() + matrix.size(),
            data.begin());
  return data;
}

// matrix is neither a vector nor in row-major
template <typename Derived>
std::vector<typename Derived::Scalar> write(const Eigen::MatrixBase<Derived>& matrix,
  typename std::enable_if<Derived::MaxRowsAtCompileTime != 1 &&
                          Derived::MaxColsAtCompileTime != 1 &&
                          (Derived::Options & Eigen::RowMajor) == 0,
                          Derived>::type* = 0)
{
  Eigen::Matrix<typename Derived::Scalar, Derived::RowsAtCompileTime,
                Derived::ColsAtCompileTime, Eigen::RowMajor,
                Derived::MaxRowsAtCompileTime, Derived::MaxColsAtCompileTime> tmp = matrix;
  return write(tmp);
}

这适用于 C++11 编译器。

其他选项是专门化模板,但它会比 SFINAE 方法更长。

部分测试用例:

Eigen::Matrix<int, 3, 3, Eigen::RowMajor> m;
m << 1, 2, 3, 
     1, 2, 3,
     1, 2, 3;

std::vector<int> output = write(m);

for (const auto& element : output) {
  std::cout << element << " ";
}

Output: 1 2 3 1 2 3 1 2 3

Eigen::Matrix<int, 3, 3, Eigen::ColMajor> m;
m << 1, 2, 3, 
     1, 2, 3,
     1, 2, 3;

std::vector<int> output = write(m);

for (const auto& element : output) {
  std::cout << element << " ";
}

Output: 1 2 3 1 2 3 1 2 3

Eigen::VectorXi m = Eigen::VectorXi::LinSpaced(10, 0, 9);

std::vector<int> output = write(m);

for (const auto& element : output) {
  std::cout << element << " ";
}

Output: 0 1 2 3 4 5 6 7 8 9

Eigen::RowVectorXi m = Eigen::RowVectorXi::LinSpaced(10, 0, 9);

std::vector<int> output = write(m);

for (const auto& element : output) {
  std::cout << element << " ";
}

Output: 0 1 2 3 4 5 6 7 8 9

一个更简单的解决方案是让 Eigen::Ref 为您完成所有工作:

Ref<const Matrix<T,Rows,Cols,Cols==1?ColMajor:RowMajor,MaxRows,MaxCols>,0, InnerStride<1> > row_maj(matrix);

row_maj将保证按行优先顺序存储。如果 matrix 兼容,则不会发生复制。无分支,无SFINAE等

这里的matrix可以是任意表达式,不仅可以是一个Matrix<...>,还可以是子矩阵,Map,另一个Ref,等等

要处理任何表达式,只需将 Rows 等替换为 XprType::RowsAtCompileTime,其中 XprTypematrix 的类型。

template <class XprType>
std::vector<typename XprType::Scalar> write(const Eigen::MatrixBase<XprType>& matrix)
{...}