title: Calculate distances in multi-dimensional matrix
body: <p>I wrote this code to calculate the distance. But getting error of <code>ValueError: math domain error</code>.</p>
  
  <pre><code>import math
  
  a=[[5, 10],
     [16, 23]]
  
  b=[[6, 1], [21, 2]]
  
  dL=[]
  dist=[]
  k=0
  h=1
  for i in range(len(a)):
      dL=[]
      for j in range(len(a)):
          d=math.sqrt((a[i][k]-b[j][k])^2 + (a[i][h]-b[j][h])^2)
          dL.append(d)
      dist.append(dL)
  </code></pre>
  
  <p>In the iterations, <code>d</code> should be:</p>
  
  <pre class="lang-none prettyprint-override"><code>i=0, j=0: d=sqrt((5-6)^2+(10-1)^2)
  i=0, j=1: d=sqrt((5-21)^2+(10-2)^2)
  i=1, j=0: d=sqrt((16-6)^2+(23-1)^2)
  i=1, j=1: d=sqrt((16-21)^2+(23-2)^2)
  </code></pre>