title: Vectorizing a Physics Simulation?
body: <p>I'm trying to simulate some 2-dimensional particles. Each particle is a circle that has an orientation. The orientation is specified by a 2-dimensional unit vector.</p>
  <p>In one part of my simulation I'd like to calculate a function of the angle between particle pairs and each particle's orientation. This should be done for each particle pair. Visually, I'd like to calculate a function of $\theta_i$ and $\theta_j$ (see image link).</p>
  <p>I've calculated the pairwise displacement unit-vectors for each particle pair. This is in a numpy array called r of shape (N, N, 2) where N is the total number of particles. I've also calculated the orientation for each particle in cartesian coordinates. This is a numpy array called orientation of shape (N, 2).</p>
  <p>I've been able to write the code I need as a double for-loop.</p>
  <pre><code>output = np.zeros(r.shape)
  for i in range(len(r)):
        for j in range(len(r[i])):
          if (i &lt; j):
            theta_i = angle_between(orientation[i], r[i,j])
            theta_j = angle_between(orientation[j], r[i,j])
            output[i][j] = (1 / (1 + np.exp(-w*(np.cos(theta_i) - np.cos(alpha))))) * (1 / (1 + np.exp(-w*(np.cos(theta_j) - np.cos(alpha)))))
  </code></pre>
  <p>It takes far to long to run this code though, especially for large systems. Is there a way to vectorize the double for loop so that it runs faster?</p>