【问题标题】:Eigen Array indexing特征数组索引
【发布时间】:2018-09-12 07:06:32
【问题描述】:

我想使用索引创建新的Eigen::Array

我知道Eigen::Matrix 是可能的,给了一个代码here

stackoverflow也发布了类似的问题

问题是如何更新以下代码以使用Eigen::Array

#include <iostream>
#include <stdio.h>
#include <Eigen/Core>

using namespace Eigen;

template<class ArgType, class RowIndexType, class ColIndexType>
class indexing_functor {
  const ArgType &m_arg;
  const RowIndexType &m_rowIndices;
  const ColIndexType &m_colIndices;
public:
  typedef Matrix<typename ArgType::Scalar,
                 RowIndexType::SizeAtCompileTime,
                 ColIndexType::SizeAtCompileTime,
                 ArgType::Flags&RowMajorBit?RowMajor:ColMajor,
                 RowIndexType::MaxSizeAtCompileTime,
                 ColIndexType::MaxSizeAtCompileTime> MatrixType;
  indexing_functor(const ArgType& arg, const RowIndexType& row_indices, const ColIndexType& col_indices)
    : m_arg(arg), m_rowIndices(row_indices), m_colIndices(col_indices)
  {}
  const typename ArgType::Scalar& operator() (Index row, Index col) const {
    return m_arg(m_rowIndices[row], m_colIndices[col]);
  }
};

template <class ArgType, class RowIndexType, class ColIndexType>
CwiseNullaryOp<indexing_functor<ArgType,RowIndexType,ColIndexType>, typename indexing_functor<ArgType,RowIndexType,ColIndexType>::MatrixType>
indexing(const Eigen::MatrixBase<ArgType>& arg, const RowIndexType& row_indices, const ColIndexType& col_indices)
{
  typedef indexing_functor<ArgType,RowIndexType,ColIndexType> Func;
  typedef typename Func::MatrixType MatrixType;
  return MatrixType::NullaryExpr(row_indices.size(), col_indices.size(), Func(arg.derived(), row_indices, col_indices));
}

【问题讨论】:

  • 如果你可以使用默认分支,那么直接写ArrayXXf a; a(row_indices,col_indices) 其中*_indices 可以是很多东西,包括序列、全部、符号等。请参阅在线文档。否则,您需要将Matrix 替换为Array,将MatrixBase 替换为ArrayBase
  • 谢谢@ggael。使用Default 分支,实际上匹配master 分支就像一个魅力。你知道什么时候会发布一个带有这些杀戮功能的新标签吗? (版本:我用过github.com/eigenteam/eigen-git-mirror/commit/…

标签: c++ eigen3


【解决方案1】:

在@ggael 的帮助下,我制作了这个使用 Eigen master 分支的小测试程序。

首先我获取 eigen repo,它指向 master 分支,并包含新的索引功能,这些功能在标记版本 3.3.5 中不存在。

git clone https://github.com/eigenteam/eigen-git-mirror

这是我用g++ -O3 -isystem eigen-git-mirror -o eigen_indexing eigen_indexing.cpp编译的程序

#include <iostream>
#include <Eigen/Dense>
int main()
{
    Eigen::ArrayX3d array = Eigen::ArrayX3d::Random(8, 3);
    Eigen::ArrayXi indices(5);
    indices << 4, 7, 0, 2, 1;
    Eigen::Array3i cols(0,1,2);
    std::cout << "array = " << std::endl << array << std::endl << std::endl;
    std::cout << "indices = " << std::endl << indices << std::endl << std::endl;
    std::cout << "cols = " << std::endl << cols << std::endl << std::endl;
    std::cout << "array(indices,cols) = " << std::endl << array(indices,cols) << std::endl << std::endl;
    std::cout << "Equivalent but Eigen::placeholders::all syntax may evolve " << std::endl << std::endl;
    std::cout << "array(indices, Eigen::placeholders::all)  = " << std::endl << array(indices,Eigen::placeholders::all) << std::endl << std::endl;
    std::cout << "Assignment also works array(indices,cols) = 0.0;" << std::endl<< std::endl;
    array(indices,cols) = 0.0;
    std::cout << "array = " << std::endl << array << std::endl << std::endl;

    std::cout << "Assignment also works array(indices,cols) = 10 * Eigen::ArrayX3d::Random(5, 3);" << std::endl<< std::endl;
    array(indices,cols) = 10 * Eigen::ArrayX3d::Random(5, 3);
    std::cout << "array = " << std::endl << array << std::endl << std::endl;
    return 0;
}

如果程序不能编译,这里是我用过的版本

 cd eigen-git-mirror
 # git checkout master
 git checkout a31719c75f244673c962ee65f279606bee6fc7ef
 cd ..

这是程序的输出

array =
-0.999984   0.358593   0.342299
-0.736924   0.869386  -0.984604
0.511211  -0.232996  -0.233169
-0.0826997  0.0388327  -0.866316
0.0655345   0.661931  -0.165028
-0.562082  -0.930856   0.373545
-0.905911  -0.893077   0.177953
0.357729  0.0594004   0.860873

indices =
4
7
0
2
1

cols =
0
1
2

array(indices,cols) =
0.0655345  0.661931 -0.165028
0.357729 0.0594004  0.860873
-0.999984  0.358593  0.342299
0.511211 -0.232996 -0.233169
-0.736924  0.869386 -0.984604

Equivalent but Eigen::placeholders::all syntax may evolve

array(indices, Eigen::placeholders::all)  =
0.0655345  0.661931 -0.165028
0.357729 0.0594004  0.860873
-0.999984  0.358593  0.342299
0.511211 -0.232996 -0.233169
-0.736924  0.869386 -0.984604

Assignment also works array(indices,cols) = 0.0;

array =
        0          0          0
        0          0          0
        0          0          0
-0.0826997  0.0388327  -0.866316
        0          0          0
-0.562082  -0.930856   0.373545
-0.905911  -0.893077   0.177953
        0          0          0

Assignment also works array(indices,cols) = 10 * Eigen::ArrayX3d::Random(5, 3);

array =
-8.1607    5.24396    2.65277
-1.68001   -9.05071    9.82075
3.07838   -4.75094    5.12821
-0.0826997  0.0388327  -0.866316
6.92334    4.02381    4.72164
-0.562082  -0.930856   0.373545
-0.905911  -0.893077   0.177953
0.538576    8.20642   -3.43532

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-10
    • 1970-01-01
    • 2018-06-03
    • 2016-09-10
    • 1970-01-01
    • 2018-12-04
    • 1970-01-01
    • 2021-11-05
    相关资源
    最近更新 更多