title: Difference between scipy pairwise distance and X.X+Y.Y - X.Y^t
body: <p>Let's imagine we have data as</p>
  
  <pre><code>d1 = np.random.uniform(low=0, high=2, size=(3,2))
  d2 = np.random.uniform(low=3, high=5, size=(3,2))
  X = np.vstack((d1,d2))
  
  X
  array([[ 1.4930674 ,  1.64890721],
         [ 0.40456265,  0.62262546],
         [ 0.86893397,  1.3590808 ],
         [ 4.04177045,  4.40938126],
         [ 3.01396153,  4.60005842],
         [ 3.2144552 ,  4.65539323]])
  </code></pre>
  
  <p>I want to compare two methods for generating the pairwise distances:</p>
  
  <p>assuming that X and Y are the same:</p>
  
  <pre><code>(X-Y)^2 = X.X + Y.Y - 2*X.Y^t
  </code></pre>
  
  <p>Here is the first method as it is used in scikit-learn for computing the pairwise distance, and later for kernel matrix.</p>
  
  <pre><code>import numpy as np
  def cal_pdist1(X):
         Y = X
         XX = np.einsum('ij,ij-&gt;i', X, X)[np.newaxis, :]
         YY = XX.T
         distances = -2*np.dot(X, Y.T)
         distances += XX
         distances += YY
         return(distances)
  
  cal_pdist1(X)
  array([[  0.        ,   2.2380968 ,   0.47354188,  14.11610424,
           11.02241244,  12.00213414],
         [  2.2380968 ,   0.        ,   0.75800718,  27.56880003,
           22.62893544,  24.15871196],
         [  0.47354188,   0.75800718,   0.        ,  19.37122424,
           15.1050792 ,  16.36714548],
         [ 14.11610424,  27.56880003,  19.37122424,   0.        ,
            1.09274896,   0.74497242],
         [ 11.02241244,  22.62893544,  15.1050792 ,   1.09274896,
            0.        ,   0.04325965],
         [ 12.00213414,  24.15871196,  16.36714548,   0.74497242,
            0.04325965,   0.        ]])
  </code></pre>
  
  <p>Now, if I use scipy pairwise distance function as below, I get </p>
  
  <pre><code>import scipy, scipy.spatial
  pd_sparse = scipy.spatial.distance.pdist(X, metric='seuclidean')
  scipy.spatial.distance.squareform(pd_sparse)
  array([[ 0.        ,  0.92916653,  0.45646989,  2.29444795,  1.89740167,
           2.00059442],
         [ 0.92916653,  0.        ,  0.50798432,  3.22211357,  2.78788236,
           2.90062103],
         [ 0.45646989,  0.50798432,  0.        ,  2.72720831,  2.28001564,
           2.39338343],
         [ 2.29444795,  3.22211357,  2.72720831,  0.        ,  0.71411943,
           0.58399694],
         [ 1.89740167,  2.78788236,  2.28001564,  0.71411943,  0.        ,
           0.14102567],
         [ 2.00059442,  2.90062103,  2.39338343,  0.58399694,  0.14102567,
           0.        ]])
  </code></pre>
  
  <p>The results are completely different! Shouldn't they be the same? </p>