title: Fastest way to compute angle between 2D vectors
body: <p>I'm looking for efficient alternate ways to compute cosine angle between 2D vectors. Your insights on this problem will be of much help.</p>
  
  <h2>Problem Statement:</h2>
  
  <p><code>vectors</code> is a 2D array where vectors are stored. The shape of the <code>vectors</code> array is <code>(N, 2)</code> where <code>N</code> is the number of vectors. <code>vectors[:, 0]</code> has x-component and <code>vectors[:, 1]</code> has y-component.</p>
  
  <p>I have to find the angle between all the vectors in <code>vectors</code>. For example, if there are three vectors A, B, C in <code>vectors</code>, I need to find the angle between <code>A and B</code>, <code>B and C</code>, and, <code>A and C</code>.</p>
  
  <p>I have implemented it and wants to know alternative ways.</p>
  
  <h2>Current Implementation:</h2>
  
  <pre><code>vectors = np.array([[1, 3], [2, 4], [3, 5]])
  
  vec_x = vectors[:, 0]
  vec_y = vectors[:, 1]
  
  a1 = np.ones([vec_x.shape[0], vec_x.shape[0]]) * vec_x
  a2 = np.ones([vec_x.shape[0], vec_x.shape[0]]) * vec_y
  a1b1 = a1 * a1.T
  a2b2 = a2 * a2.T
  mask = np.triu_indices(a1b1.shape[0], 0) # We are interested in lower triangular matrix
  a1b1[mask] = 0
  a2b2[mask] = 0
  numer = a1b1 + a2b2
  denom = np.ones([vec_x.shape[0], vec_x.shape[0]]) * np.sqrt(np.square(a1) + np.square(a2))
  denom = denom * denom.T
  denom[mask] = 0
  eps = 1e-7
  dot_res = np.rad2deg(np.arccos(np.divide(numer, denom + eps)))
  dot_res[mask] = 0
  print(dot_res)
  </code></pre>
  
  <h2>Output:</h2>
  
  <pre><code>[[ 0.          0.          0.        ]
   [ 8.13010519  0.          0.        ]
   [12.52880911  4.39870821  0.        ]]
  </code></pre>
  
  <h2>Questions:</h2>
  
  <ol>
  <li><p>Is there any alternative way to do this more efficient?</p></li>
  <li><p>Can we improve the speed of the current version in some way?</p></li>
  </ol>