将任意Eigen对象写入行主要普通存储

问题描述 投票:2回答:2

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

目前我首先使用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.
c++ eigen eigen3
2个回答
4
投票

理解静态断言

不幸的是,断言实际上并不是不言自明的,你唯一能从中得到的就是提示,你的模板参数有问题。如果我们查看Eigen的源代码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. SFIN

您可以使用write在编译时使用SFINAE将调用发送到std::enable_if。以下示例使用原始代码的略微修改版本,但所有内容都应该从上下文中清楚:

// 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


3
投票

更简单的解决方案是让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)
{...}
© www.soinside.com 2019 - 2024. All rights reserved.