title: Armadillo matrix transpose
body: <p>I have a huge m*n matrix A (where the number of rows m is much larger then the number of columns n) which is stored in my c++ program as an <code>armadillo</code> <code>mat</code> type. Now I have a vector <code>w</code> for which I have to calculate <code>w=w-A*A^T*w</code> where <code>A^T</code> means the transpose of the matrix A. </p>
  
  <p>As the matrix A is very large and consumes a lot of memory, the usual fast approach with armadillo <code>w=w-A*A.t()*w</code> does not work, since armadillo consumes a lot of memory in this case (cf. <a href="https://github.com/mlpack/mlpack/issues/203" rel="nofollow">github</a>). The way they resolved this, was by introducing the function <code>inplace_trans( A, method )</code>, which can use the method "lowmem" which consumes less memory but needs more time.</p>
  
  <p>My problem is now, that <code>inplace_trans( A, method )</code> is a void function, so that I have to create a copy of my matrix first, before I can calculate the new <code>w</code>:</p>
  
  <pre><code>mat Q = A;
  inplace_trans(Q, 'lowmmem');
  w=w-A*Q*w;
  </code></pre>
  
  <p>This however is of course not the desired result, since I need a full copy of my matrix, which I wanted to avoid in the first place (RAM problem!).
  So, how can I get the transpose of my matrix in an efficient (=fast and low memory demanding) way in order to calculate the new <code>w</code>?</p>
  
  <p>If I do it element wise like in</p>
  
  <pre><code>mat A(m,n); //huge matrix, initialized before
  vec temp(m);
  temp.fill(0.0);
  for (unsigned long int ii=0; ii&lt;m; ii++){
  
      for (unsigned long int ll=0; ll&lt;m; ll++){
          temp(ii)+=dot(A.row(ii),A.row(ll))*w(ll);
      }
  }
  w=w-temp;
  </code></pre>
  
  <p>I have to iterate twice over the number of rows m, which is very costly.</p>
  
  <p><strong>Edit:</strong>
  Up to now the fastest method is the following:</p>
  
  <pre><code>vec temp(m);
  inplace_trans(A, "lowmem");
  temp = A * w;
  inplace_trans(A, "lowmem");
  temp = A * temp;
  </code></pre>
  
  <p>I have to transpose the matrix twice, because I need it back in its original state afterwards. I cannot believe that this should be the fastest way, since it takes way to much operations, imho.</p>