title: Calculate the triangular matrix of distances between NumPy array of coordinates
body: <p>I have an NumPy array of coordinates.  For example purposes, I will use this</p>
  
  <pre><code>In [1]: np.random.seed(123)
  In [2]: coor = np.random.randint(10, size=12).reshape(-1,3)
  In [3]: coor
  Out[3]: array([[2, 2, 6],
                 [1, 3, 9],
                 [6, 1, 0],
                 [1, 9, 0]])
  </code></pre>
  
  <p>I want the triangular matrix of distances between all coordinates.  A  simple approach would be to code a double loop over all coordinates </p>
  
  <pre><code>In [4]: n_coor = len(coor)
  In [5]: dist = np.zeros((n_coor, n_coor))
  In [6]: for j in xrange(n_coor):
              for k in xrange(j+1, n_coor):
                  dist[j, k] = np.sqrt(np.sum((coor[j] - coor[k]) ** 2))
  </code></pre>
  
  <p>with the result being an upper triangular matrix of the distances</p>
  
  <pre><code>In [7]: dist
  Out[7]: array([[  0.        ,   3.31662479,   7.28010989,   9.2736185 ],
                 [  0.        ,   0.        ,  10.48808848,  10.81665383],
                 [  0.        ,   0.        ,   0.        ,   9.43398113],
                 [  0.        ,   0.        ,   0.        ,   0.        ]])
  </code></pre>
  
  <p>Leveraging NumPy, I can avoid looping using</p>
  
  <pre><code>In [8]: dist = np.sqrt(((coor[:, None, :] - coor) ** 2).sum(-1))
  </code></pre>
  
  <p>but the result is the entire matrix</p>
  
  <pre><code>In [9]: dist
  Out[9]: array([[  0.        ,   3.31662479,   7.28010989,   9.2736185 ],
                 [  3.31662479,   0.        ,  10.48808848,  10.81665383],
                 [  7.28010989,  10.48808848,   0.        ,   9.43398113],
                 [  9.2736185 ,  10.81665383,   9.43398113,   0.        ]])
  </code></pre>
  
  <p>This one line version takes roughly half the time when I use 2048 coordinates (4 s instead of 10 s) but this is doing twice as many calculations as it needs in order to get the symmetric matrix. Is there a way to adjust the one line command to only get the triangular matrix (and the additional 2x speedup, i.e. 2 s)?</p>