title: How can I apply a matrix transform to each row of a NumPy array efficiently?
body: <p>Let's say I have a 2d NumPy ndarray, like so:</p>
  
  <pre><code>[[ 0, 1, 2, 3 ],
   [ 4, 5, 6, 7 ],
   [ 8, 9, 10, 11 ]]
  </code></pre>
  
  <p>Conceptually speaking, what I want to do is this:</p>
  
  <pre><code>For each row:
      Transpose the row
      Multiply the transposed row by a transformation matrix
      Transpose the result
      Store the result in the original ndarray, overwriting the original row data
  </code></pre>
  
  <p>I have an <em>extremely</em> slow, brute-force method which functionally achieves this:</p>
  
  <pre><code>import numpy as np
  transform_matrix = np.matrix( /* 4x4 matrix setup clipped for brevity */ )
  for i, row in enumerate( data ):
      tr = row.reshape( ( 4, 1 ) )
      new_row = np.dot( transform_matrix, tr )
      data[i] = new_row.reshape( ( 1, 4 ) )
  </code></pre>
  
  <p>However, this seems like the sort of operation that NumPy should do well with.  I assume that - as someone new to NumPy - I'm just missing something fundamental in the documentation.  Any pointers?</p>
  
  <p>Note that if it's faster to create a new ndarray rather than edit it in-place, that can work for what I'm doing, too; speed of the operation is the primary concern.</p>