title: Further optimization of simple cython code
body: <p>I have a function written in cython that computes a certain measure of correlation (distance correlation) via a double for loop:</p>
  
  <pre><code>%%cython -a
  import numpy as np
  
  def distances_Matrix(X):
      return np.array([[np.linalg.norm(xi-xj) for xi in X] for xj in X])
  
  def c_dCov(double[:, :] a, double[:, :] b, int n):
      cdef int i
      cdef int j
      cdef double U       =  0
      cdef double W1      =  n/(n-1)
      cdef double W2      =  2/(n-2)
      cdef double[:] a_M  =  np.mean(a,axis=1)
      cdef double    a_   =  np.mean(a)
      cdef double[:] b_M  =  np.mean(b,axis=1)
      cdef double    b_   =  np.mean(b)
  
      for i in range(n):
          for j in range(n):
              if i != j:
                  U = U + (a[i][j] + W1*(-a_M[i]-a_M[j]+a_)) * (b[i][j] +   W1*(-b_M[i]-b_M[j]+b_))
              else:
                  U = U - W2*(W1**2)*(a_M[i] - a_) * (b_M[i] - b_)
      return U/(n*(n-3))
  
  def c_dCor(X,Y):
      n     =  len(X)
      a     =  distances_Matrix(X)
      b     =  distances_Matrix(Y)
      V_XX  =  c_dCov(a,a,n) 
      V_YY  =  c_dCov(b,b,n)
      V_XY  =  c_dCov(a,b,n)
      return V_XY/np.sqrt(V_XX*V_YY)
  </code></pre>
  
  <p>When I compile this fragment of code I get the following report of optimization by the compiler:</p>
  
  <p><a href="https://i.stack.imgur.com/uxF8k.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/uxF8k.png" alt="enter image description here"></a></p>
  
  <p>Line 23 is still quite yellow, which indicates significant python interactions, how can I make that line further optimized?.</p>
  
  <p>The operations done on that line are quite trivial, just products and sums, since I did specify the types of every array and variable used in that function, why do I get such a bad performance on that line?</p>
  
  <p>Thanks in advance.</p>