将任意的特征对象写入行 - 马约尔平原存储

Write arbitrary Eigen object to row-major plain storage

本文关键字:马约尔 存储 任意 特征 对象      更新时间:2023-10-16

我正在编写一个模块,将数据写入仅使用Rodunt Row-Major存储的文件。我希望我的功能能够允许列 - 列和行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的源代码,我们会在第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

解决您的问题

现在的问题是,即使您可以检查输入矩阵是向量,行-major或column-major,您也无法声明

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

输出: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 << " ";
}

输出: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 << " ";
}

输出: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 << " ";
}

输出: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<...>,还可以是sub-matrices,Map,另一个Ref,等等。

要处理任何表达式,只需替换 RowsXprType::RowsAtCompileTime,其中 XprTypematrix的类型。

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