title: Efficient Numpy computation of pairwise squared differences
body: <p>The following code does exactly what I want, which is to compute the pairwise sum of squares of differences between elements of a vector (length three in the example), of which I have a long series (limited to five here).  The desired result is shown at the bottom.
  But the implementation feels kludgy for two reasons: </p>
  
  <p>1) the need to add a phantom dimension, changing the shape from (5, 3) to (5,1,3) to avoid broadcast problems, and </p>
  
  <p>2) the apparent necessity of an explicit 'for' loop, which I'm sure is why it's taking hours to execute on my much larger data set (a million vectors of length 2904).</p>
  
  <p>Is there a more efficient and/or pythonic way to achieve the same result?</p>
  
  <pre><code>a = np.array([[ 4,  2,  3], [-1, -5,  4], [ 2,  1,  4], [-5, -1,  4], [6, -3,  3]])
  a = a.reshape((5,1,3))
  
  m = a.shape[0]
  n = a.shape[2]
  d = np.zeros((n,n))
  for i in range(m):
      c = a[i,:] - np.transpose(a[i,:])
      c = c**2
      d += c
  
  print d
  
  [[   0.  118.  120.]
   [ 118.    0.  152.]
   [ 120.  152.    0.]]
  </code></pre>