nullary_indexing.cpp
Go to the documentation of this file.
1 #include <Eigen/Core>
2 #include <iostream>
3 
4 using namespace Eigen;
5 
6 // [functor]
7 template<class ArgType, class RowIndexType, class ColIndexType>
9  const ArgType &m_arg;
10  const RowIndexType &m_rowIndices;
11  const ColIndexType &m_colIndices;
12 public:
13  typedef Matrix<typename ArgType::Scalar,
14  RowIndexType::SizeAtCompileTime,
15  ColIndexType::SizeAtCompileTime,
16  ArgType::Flags&RowMajorBit?RowMajor:ColMajor,
17  RowIndexType::MaxSizeAtCompileTime,
18  ColIndexType::MaxSizeAtCompileTime> MatrixType;
19 
20  indexing_functor(const ArgType& arg, const RowIndexType& row_indices, const ColIndexType& col_indices)
21  : m_arg(arg), m_rowIndices(row_indices), m_colIndices(col_indices)
22  {}
23 
24  const typename ArgType::Scalar& operator() (Index row, Index col) const {
25  return m_arg(m_rowIndices[row], m_colIndices[col]);
26  }
27 };
28 // [functor]
29 
30 // [function]
31 template <class ArgType, class RowIndexType, class ColIndexType>
33 mat_indexing(const Eigen::MatrixBase<ArgType>& arg, const RowIndexType& row_indices, const ColIndexType& col_indices)
34 {
36  typedef typename Func::MatrixType MatrixType;
37  return MatrixType::NullaryExpr(row_indices.size(), col_indices.size(), Func(arg.derived(), row_indices, col_indices));
38 }
39 // [function]
40 
41 
42 int main()
43 {
44  std::cout << "[main1]\n";
45  Eigen::MatrixXi A = Eigen::MatrixXi::Random(4,4);
46  Array3i ri(1,2,1);
47  ArrayXi ci(6); ci << 3,2,1,0,0,2;
48  Eigen::MatrixXi B = mat_indexing(A, ri, ci);
49  std::cout << "A =" << std::endl;
50  std::cout << A << std::endl << std::endl;
51  std::cout << "A([" << ri.transpose() << "], [" << ci.transpose() << "]) =" << std::endl;
52  std::cout << B << std::endl;
53  std::cout << "[main1]\n";
54 
55  std::cout << "[main2]\n";
56  B = mat_indexing(A, ri+1, ci);
57  std::cout << "A(ri+1,ci) =" << std::endl;
58  std::cout << B << std::endl << std::endl;
59 #if EIGEN_COMP_CXXVER >= 11
60  B = mat_indexing(A, ArrayXi::LinSpaced(13,0,12).unaryExpr([](int x){return x%4;}), ArrayXi::LinSpaced(4,0,3));
61  std::cout << "A(ArrayXi::LinSpaced(13,0,12).unaryExpr([](int x){return x%4;}), ArrayXi::LinSpaced(4,0,3)) =" << std::endl;
62  std::cout << B << std::endl << std::endl;
63 #endif
64  std::cout << "[main2]\n";
65 }
66 
Eigen
Namespace containing all symbols from the Eigen library.
Definition: jet.h:637
B
Matrix< SCALARB, Dynamic, Dynamic, opt_B > B
Definition: bench_gemm.cpp:49
indexing_functor::m_arg
const ArgType & m_arg
Definition: nullary_indexing.cpp:9
col
m col(1)
MatrixType
MatrixXf MatrixType
Definition: benchmark-blocking-sizes.cpp:52
x
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy x
Definition: gnuplot_common_settings.hh:12
Eigen::RowMajorBit
const unsigned int RowMajorBit
Definition: Constants.h:66
Eigen::RowMajor
@ RowMajor
Definition: Constants.h:321
A
Matrix< SCALARA, Dynamic, Dynamic, opt_A > A
Definition: bench_gemm.cpp:48
Eigen::CwiseNullaryOp
Generic expression of a matrix where all coefficients are defined by a functor.
Definition: CwiseNullaryOp.h:60
indexing_functor::m_rowIndices
const RowIndexType & m_rowIndices
Definition: nullary_indexing.cpp:10
operator()
internal::enable_if< internal::valid_indexed_view_overload< RowIndices, ColIndices >::value &&internal::traits< typename EIGEN_INDEXED_VIEW_METHOD_TYPE< RowIndices, ColIndices >::type >::ReturnAsIndexedView, typename EIGEN_INDEXED_VIEW_METHOD_TYPE< RowIndices, ColIndices >::type >::type operator()(const RowIndices &rowIndices, const ColIndices &colIndices) EIGEN_INDEXED_VIEW_METHOD_CONST
Definition: IndexedViewMethods.h:73
unaryExpr
const EIGEN_DEVICE_FUNC CwiseUnaryOp< CustomUnaryOp, const Derived > unaryExpr(const CustomUnaryOp &func=CustomUnaryOp()) const
Apply a unary operator coefficient-wise.
Definition: CommonCwiseUnaryOps.h:135
arg
Definition: cast.h:1412
indexing_functor::MatrixType
Matrix< typename ArgType::Scalar, RowIndexType::SizeAtCompileTime, ColIndexType::SizeAtCompileTime, ArgType::Flags &RowMajorBit?RowMajor:ColMajor, RowIndexType::MaxSizeAtCompileTime, ColIndexType::MaxSizeAtCompileTime > MatrixType
Definition: nullary_indexing.cpp:18
row
m row(1)
mat_indexing
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)
Definition: nullary_indexing.cpp:33
Eigen::Matrix
The matrix class, also used for vectors and row-vectors.
Definition: 3rdparty/Eigen/Eigen/src/Core/Matrix.h:178
indexing_functor::indexing_functor
indexing_functor(const ArgType &arg, const RowIndexType &row_indices, const ColIndexType &col_indices)
Definition: nullary_indexing.cpp:20
main
int main()
Definition: nullary_indexing.cpp:42
Eigen::MatrixBase
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:48
indexing_functor
Definition: nullary_indexing.cpp:8
Eigen::ColMajor
@ ColMajor
Definition: Constants.h:319
indexing_functor::m_colIndices
const ColIndexType & m_colIndices
Definition: nullary_indexing.cpp:11
Scalar
SCALAR Scalar
Definition: bench_gemm.cpp:46
Eigen::Index
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:74


gtsam
Author(s):
autogenerated on Fri Nov 1 2024 03:33:51