title: Swap Array Data in NumPy
body: <p>I have many large multidimensional NP arrays (2D and 3D) used in an algorithm. There are numerous iterations in this, and during each iteration the arrays are recalculated by performing calculations and saving into temporary arrays of the same size. At the end of a single iteration the contents of the temporary arrays are copied into the actual data arrays.</p>
  
  <p>Example:</p>
  
  <pre><code>global A, B # ndarrays
  A_temp = numpy.zeros(A.shape)
  B_temp = numpy.zeros(B.shape)
  for i in xrange(num_iters):
      # Calculate new values from A and B storing in A_temp and B_temp...
      # Then copy values from temps to A and B
      A[:] = A_temp
      B[:] = B_temp
  </code></pre>
  
  <p>This works fine, however it seems a bit wasteful to copy all those values when A and B could just swap. The following would swap the arrays:</p>
  
  <pre><code>A, A_temp = A_temp, A
  B, B_temp = B_temp, B
  </code></pre>
  
  <p>However there can be other references to the arrays in other scopes which this won't change.</p>
  
  <p>It seems like NumPy could have an internal method for swapping the internal data pointer of two arrays, such as <code>numpy.swap(A, A_temp)</code>. Then all variables pointing to <code>A</code> would be pointing to the changed data.</p>