title: Is there a numpy/scipy dot product, calculating only the diagonal entries of the result?
body: <p>Imagine having 2 numpy arrays:</p>
  
  <pre><code>&gt; A, A.shape = (n,p)
  &gt; B, B.shape = (p,p)
  </code></pre>
  
  <p>Typically p is a smaller number (p &lt;= 200), while n can be arbitrarily large.</p>
  
  <p>I am doing the following:</p>
  
  <pre><code>result = np.diag(A.dot(B).dot(A.T))
  </code></pre>
  
  <p>As you can see, I am keeping only the n diagonal entries, however there is an intermediate (n x n) array calculated from which only the diagonal entries are kept.</p>
  
  <p>I wish for a function like diag_dot(), which only calculates the diagonal entries of the result and does not allocate the complete memory.</p>
  
  <p>A result would be:</p>
  
  <pre><code>&gt; result = diag_dot(A.dot(B), A.T)
  </code></pre>
  
  <p>Is there a premade functionality like this and can this be done efficiently without the need for allocating the intermediate (n x n) array?</p>