title: Eigen Array indexing
body: <p>I would like to create new <code>Eigen::Array</code> using an index.</p>
  
  <p>I know it is possible for <code>Eigen::Matrix</code>, a code is given <a href="http://eigen.tuxfamily.org/dox/TopicCustomizing_NullaryExpr.html#NullaryExpr_Indexing" rel="nofollow noreferrer">here</a>.</p>
  
  <p>A similar question has also been posted on <a href="https://stackoverflow.com/questions/43749673/extending-eigen-with-cwisenullaryop-cannot-reproduce-example">stackoverflow</a></p>
  
  <p>The question is how to update the following code to work with <code>Eigen::Array</code></p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;stdio.h&gt;
  #include &lt;Eigen/Core&gt;
  
  using namespace Eigen;
  
  template&lt;class ArgType, class RowIndexType, class ColIndexType&gt;
  class indexing_functor {
    const ArgType &amp;m_arg;
    const RowIndexType &amp;m_rowIndices;
    const ColIndexType &amp;m_colIndices;
  public:
    typedef Matrix&lt;typename ArgType::Scalar,
                   RowIndexType::SizeAtCompileTime,
                   ColIndexType::SizeAtCompileTime,
                   ArgType::Flags&amp;RowMajorBit?RowMajor:ColMajor,
                   RowIndexType::MaxSizeAtCompileTime,
                   ColIndexType::MaxSizeAtCompileTime&gt; MatrixType;
    indexing_functor(const ArgType&amp; arg, const RowIndexType&amp; row_indices, const ColIndexType&amp; col_indices)
      : m_arg(arg), m_rowIndices(row_indices), m_colIndices(col_indices)
    {}
    const typename ArgType::Scalar&amp; operator() (Index row, Index col) const {
      return m_arg(m_rowIndices[row], m_colIndices[col]);
    }
  };
  
  template &lt;class ArgType, class RowIndexType, class ColIndexType&gt;
  CwiseNullaryOp&lt;indexing_functor&lt;ArgType,RowIndexType,ColIndexType&gt;, typename indexing_functor&lt;ArgType,RowIndexType,ColIndexType&gt;::MatrixType&gt;
  indexing(const Eigen::MatrixBase&lt;ArgType&gt;&amp; arg, const RowIndexType&amp; row_indices, const ColIndexType&amp; col_indices)
  {
    typedef indexing_functor&lt;ArgType,RowIndexType,ColIndexType&gt; Func;
    typedef typename Func::MatrixType MatrixType;
    return MatrixType::NullaryExpr(row_indices.size(), col_indices.size(), Func(arg.derived(), row_indices, col_indices));
  }
  </code></pre>