nullary_indexing.cpp
Go to the documentation of this file.
1 #include <Eigen/Core>
2 #include <iostream>
3 
4 // [functor]
5 template<class ArgType, class RowIndexType, class ColIndexType>
6 class indexing_functor {
7  const ArgType &m_arg;
8  const RowIndexType &m_rowIndices;
9  const ColIndexType &m_colIndices;
10 public:
11  typedef Eigen::Matrix<typename ArgType::Scalar,
12  RowIndexType::SizeAtCompileTime,
13  ColIndexType::SizeAtCompileTime,
15  RowIndexType::MaxSizeAtCompileTime,
16  ColIndexType::MaxSizeAtCompileTime> MatrixType;
17 
18  indexing_functor(const ArgType& arg, const RowIndexType& row_indices, const ColIndexType& col_indices)
19  : m_arg(arg), m_rowIndices(row_indices), m_colIndices(col_indices)
20  {}
21 
22  const typename ArgType::Scalar& operator() (Eigen::Index row, Eigen::Index col) const {
23  return m_arg(m_rowIndices[row], m_colIndices[col]);
24  }
25 };
26 // [functor]
27 
28 // [function]
29 template <class ArgType, class RowIndexType, class ColIndexType>
31 mat_indexing(const Eigen::MatrixBase<ArgType>& arg, const RowIndexType& row_indices, const ColIndexType& col_indices)
32 {
33  typedef indexing_functor<ArgType,RowIndexType,ColIndexType> Func;
34  typedef typename Func::MatrixType MatrixType;
35  return MatrixType::NullaryExpr(row_indices.size(), col_indices.size(), Func(arg.derived(), row_indices, col_indices));
36 }
37 // [function]
38 
39 
40 int main()
41 {
42  std::cout << "[main1]\n";
44  Eigen::Array3i ri(1,2,1);
45  Eigen::ArrayXi ci(6); ci << 3,2,1,0,0,2;
46  Eigen::MatrixXi B = mat_indexing(A, ri, ci);
47  std::cout << "A =" << std::endl;
48  std::cout << A << std::endl << std::endl;
49  std::cout << "A([" << ri.transpose() << "], [" << ci.transpose() << "]) =" << std::endl;
50  std::cout << B << std::endl;
51  std::cout << "[main1]\n";
52 
53  std::cout << "[main2]\n";
54  B = mat_indexing(A, ri+1, ci);
55  std::cout << "A(ri+1,ci) =" << std::endl;
56  std::cout << B << std::endl << std::endl;
57  B = mat_indexing(A, Eigen::ArrayXi::LinSpaced(13,0,12).unaryExpr([](int x){return x%4;}), Eigen::ArrayXi::LinSpaced(4,0,3));
58  std::cout << "A(ArrayXi::LinSpaced(13,0,12).unaryExpr([](int x){return x%4;}), ArrayXi::LinSpaced(4,0,3)) =" << std::endl;
59  std::cout << B << std::endl << std::endl;
60  std::cout << "[main2]\n";
61 }
62 
const ArgReturnType arg() const
RowXpr row(Index i)
This is the const version of row(). *‍/.
ColXpr col(Index i)
This is the const version of col().
const CwiseUnaryOp< CustomUnaryOp, const Derived > unaryExpr(const CustomUnaryOp &func=CustomUnaryOp()) const
Apply a unary operator coefficient-wise.
MatrixXcf A
MatrixXf B
IndexedView_or_Block operator()(const RowIndices &rowIndices, const ColIndices &colIndices)
Matrix< float, 1, Dynamic > MatrixType
General-purpose arrays with easy API for coefficient-wise operations.
Definition: Array.h:49
Generic expression of a matrix where all coefficients are defined by a functor.
static EIGEN_DEPRECATED const RandomAccessLinSpacedReturnType LinSpaced(Sequential_t, Index size, const Scalar &low, const Scalar &high)
TransposeReturnType transpose()
Definition: Transpose.h:184
static const RandomReturnType Random()
Definition: Random.h:114
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:52
The matrix class, also used for vectors and row-vectors.
Definition: Matrix.h:182
@ ColMajor
Definition: Constants.h:321
@ RowMajor
Definition: Constants.h:323
const unsigned int RowMajorBit
Definition: Constants.h:68
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:82
Eigen::CwiseNullaryOp< indexing_functor< ArgType, RowIndexType, ColIndexType >, typename indexing_functor< ArgType, RowIndexType, ColIndexType >::MatrixType > mat_indexing(const Eigen::MatrixBase< ArgType > &arg, const RowIndexType &row_indices, const ColIndexType &col_indices)
int main()