title: Eigen:: Cast sparse matrix as, specifically, row order or col order
body: <p>I have in a function internal, </p>
  
  <pre><code>Eigen::SparseMatrix&lt;double&gt; &amp; M;
  
  if (M.IsRowMajor)
       return my_func_template&lt;Eigen::SparseMatrix&lt;double,Eigen::RowMajor&gt;&amp;&gt;(M,M.rows());
  </code></pre>
  
  <p>However, this does not compile, as the compiler does not believe <code>M</code> is an <code>Eigen::SparseMatrix&lt;double,Eigen::RowMajor&gt;</code>.  How do I cast my reference as, specifically, <code>Eigen::SparseMatrix&lt;double,Eigen::RowMajor&gt;</code>, in the type-safe environment of C++11?</p>
  
  <hr>
  
  <p>For example: </p>
  
  <pre><code>typedef Eigen::SparseMatrix&lt;double&gt; Smat;
  typedef Eigen::SparseMatrix&lt;double,Eigen::RowMajor&gt; RMSmat;
  typedef Eigen::SparseMatrix&lt;double,Eigen::ColMajor&gt; CMSmat;    
  
  enum direction { row, col};
  
  template&lt;class Mat&gt;
  vector&lt;double&gt; sum_along_inner(Mat &amp;M){
      vector&lt;double&gt; sums(M.innerSize(),0);
      for(auto i = 0; i &lt; M.outerSize(); i++){
          for(typename M::InnerIterator it(M,i); it;++it){
              sums[i] += it.value();
          }
      }
  }
  vector&lt;double&gt; sum_along_axis(Smat &amp;M, direction dir){
  
      // If I could solve this problem, 
      // 
      // I could also function off these if components, 
      // and re-use them for other order-dependent functions I write
      // so that my top level functions are only about 2-4 lines long
  
      if(dir == direction::row){
          if(M.IsRowMajor)
              return sum_along_inner&lt;RMSmat&gt;((my question) M);
  
          //else
          RMsmat Mrowmajor = M;
          return sum_along_inner&lt;RMSmat&gt;(Mrowmajor);
      }
      else { 
         if(!M.IsRowMajor)
              return sum_along_inner&lt;CMSmat&gt;(M);
  
         // else
         CMSmat Mcolmajor = M;
         return sum_along_inner&lt;CMSmat&gt;((my_question) Mcolmajor);
      }
  }
  </code></pre>
  
  <p>And if I do more than just <code>sum_along_axis</code>, then the code complexity in terms of number of lines, readability, etc. is double what it needs to be <strong>if only I could solve this problem that I am asking about</strong>. </p>
  
  <p>Otherwise, I can't abstract the loop, and I have to repeat it for column major and row major...because I can't just assume I wont call <code>sum_along_axis</code> from a function that hasn't already swapped the major-order from the default <code>Eigen::ColMajor</code> to <code>Eigen::RowMajor</code>...</p>
  
  <p>Further, if I am operating at the order of mb-sized sparse matrices with dimensions too unwieldy to represent in dense matrix form, I am going to notice a major slowdown (which defeats the purpose of using a sparse matrix to begin with) if I don't write <strong>composable</strong> functions which are <strong>order agnostic</strong>, and transition the major-order only when needed. </p>
  
  <p>So, unless I solve for this, my line count and/or function count, more or less, starts to go combinatorial.  </p>