title: Gradient calculation with python
body: <p>I would like to know how does <code>numpy.gradient</code> work.
  I used gradient to try to calculate <em>group velocity</em> (group velocity of a wave packet is the derivative of frequencies respect to wavenumbers, not a group of velocities).  I fed a 3 column array to it, the first 2 colums are x and y coords, the third column is the frequency of that point (x,y). I need to calculate gradient and I did expect a 2d vector, being gradient definition </p>
  
  <pre><code>df/dx*i+df/dy*j+df/dz*k 
  </code></pre>
  
  <p>and my function only a function of x and y i did expect something like </p>
  
  <pre><code>df/dx*i+df/dy*j 
  </code></pre>
  
  <p>But i got 2 arrays with 3 colums each, i.e. 2 3d vectors; at first i thought that the sum of the two would give me the vector i were searchin for but the z component doesn't vanish. I hope i've been sufficiently clear in my explanation. I would like to know how <code>numpy.gradient</code> works and if it's the right choice for my problem. Otherwise i would like to know if there's any other python function i can use.</p>
  
  <p>What i mean is: I want to calculate gradient of an array of values:</p>
  
  <pre><code>data=[[x1,x2,x3]...[x1,x2,x3]]
  </code></pre>
  
  <p>where x1,x2 are point coordinates on an uniform grid (my points on the brillouin zone) and x3 is the value of frequency for that point. I give in input also steps for derivation for the 2 directions:</p>
  
  <pre><code>stepx=abs(max(unique(data[:,0])-min(unique(data[:,0]))/(len(unique(data[:,0]))-1)
  </code></pre>
  
  <p>the same for y direction.
  I didn't build my data on a grid, i already have a grid and this is why kind examples given here in answers do not help me. 
  A more fitting example should have a grid of points and values like the one i have:</p>
  
  <pre><code>data=[]
  for i in range(10):
    for j in range(10):
      data.append([i,j,i**2+j**2])
  
  data=array(data,dtype=float)
  
  gx,gy=gradient(data)
  </code></pre>
  
  <p>another thing i can add is that my grid is not a square one but has the shape of a polygon being the brillouin zone of a 2d crystal.</p>
  
  <p>I've understood that <code>numpy.gradient</code> works properly only on a square grid of values, not what i'm searchin for. Even if i make my data as a grid that would have lots of zeroes outside of the polygon of my original data, that would add really high vectors to my gradient affecting (negatively) the precision of calculation. This module seems to me more a toy than a tool, it has severe limitations imho.</p>
  
  <p>Problem solved using dictionaries.</p>