title: Returning all rows, or just the last one
body: <p>I'm writing up a bunch (~ a dozen) algorithms that iteratively process a vector in the following pattern:</p>
  
  <pre><code>ArrayXd prev;
  ArrayXd curr;
  
  prev = some_initial_vector();
  for (i = 0; i &lt; N; ++i) {
      // do lots of stuff, many lines to compute curr
      // use lots of algorithm specific variables initialised above
      ...
      prev = curr;
  }
  return curr;
  </code></pre>
  
  <p>I would like to have a way of returning the entire history of the values of <code>curr</code> as well, as rows in an <code>ArrayXXd</code>.</p>
  
  <p>I have tried solving this by writing two classes that exhibit a <code>curr</code> handle, one as an <code>ArrayXd &amp;</code> the other as a <code>Block&lt;ArrayXXd, 1, -1&gt;</code>, but that failed as <a href="https://stackoverflow.com/questions/38705842/how-do-you-change-a-block-in-eigen/">it's not possible to reassign <code>Block</code>s</a>.</p>
  
  <p>What is a good way of solving this problem? Maybe I could store the <code>Block</code>s or the <code>ArrayXd</code>s themselves in a <code>std::vector</code> and then <a href="https://stackoverflow.com/questions/33668485/eigen-and-stdvector/33669013#33669013">convert that to an <code>ArrayXXd</code></a> at the end.</p>
  
  <p><strong>Edit:</strong> Added sample input, output</p>
  
  <pre><code>struct NotAccumulator {
      typedef ArrayXd return_type;
      ArrayXd curr;
      ArrayXd prev;
      void record () {}
      return_type result() {
          return prev;
      }
  };
  
  struct RowAccumulator {
      typedef ArrayXXd return_type;
      ArrayXd curr;
      ArrayXd prev;
      RowAccumulator(const uint N) {
          history.reserve(N);
      }
  
      void record () {
          history.push_back(curr);
      }
      return_type result () {
          uint rows = history.size();
          uint cols = history[0].size();
          ArrayXXd result_matrix (rows, cols);
          for(uint i = 0; i &lt; rows; ++i) {
              result_matrix.row(i) = Map&lt;ArrayXd&gt; (history[i].data(), cols);
          }
          return result_matrix;
      }
  
  private:
      std::vector&lt;ArrayXd&gt; history;
  };
  
  template &lt;typename Accumulator&gt;
  typename Accumulator::return_type add_one(const ArrayXd &amp; start, const uint how_many_times, Accumulator &amp; u) {
      u.prev = start;
      for (uint i = 0; i &lt; how_many_times; ++i) {
          u.curr = 1 + u.prev;
          u.record();
          u.prev = u.curr;
      }
      return u.result();
  }
  
  ArrayXd start (3);
  start &lt;&lt; 1, 0, -1;
  NotAccumulator notAccumulator;
  RowAccumulator rowAccumulator (5);
  cout &lt;&lt; add_one(start, 5, notAccumulator) &lt;&lt; endl;
  // outputs 6 5 4
  cout &lt;&lt; add_one(start, 5, rowAccumulator) &lt;&lt; endl;
  // outputs 2 1 0\n 3 2 1\n ... 6 5 4
  </code></pre>