title: Most memory-efficient way to compute abs()**2 of complex numpy ndarray
body: <p>I'm looking for the most memory-efficient way to compute the absolute squared value of a complex numpy ndarray</p>
  
  
  
  <pre class="lang-py prettyprint-override"><code>arr = np.empty((250000, 150), dtype='complex128')  # common size
  </code></pre>
  
  <p>I haven't found a ufunc that would do exactly <code>np.abs()**2</code>.</p>
  
  <p>As an array of that size and type takes up around half a GB, I'm looking for a primarily memory-efficient way.</p>
  
  <p>I would also like it to be portable, so ideally some combination of ufuncs.</p>
  
  <p>So far my understanding is that this should be about the best</p>
  
  <pre class="lang-py prettyprint-override"><code>result = np.abs(arr)
  result **= 2
  </code></pre>
  
  <p>It will needlessly compute <code>(**0.5)**2</code>, but should compute <code>**2</code> in-place. Altogether the peak memory requirement is only the original array size + result array size,  which should be 1.5 * original array size as the result is real.</p>
  
  <p>If I wanted to get rid of the useless <code>**2</code> call I'd have to do something like this</p>
  
  <pre class="lang-py prettyprint-override"><code>result = arr.real**2
  result += arr.imag**2
  </code></pre>
  
  <p>but if I'm not mistaken, this means I'll have to allocate memory for <strong>both</strong> the real and imaginary part calculation, so the peak memory usage would be 2.0 * original array size. The <code>arr.real</code> properties also return a non-contiguous array (but that is of lesser concern).</p>
  
  <p>Is there anything I'm missing? Are there any better ways to do this?</p>
  
  <p><em>EDIT 1</em>:
  I'm sorry for not making it clear, I don't want to overwrite arr, so I can't use it as out.</p>