【发布时间】: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/…)