title: Partial derivative in Python
body: <p>I am slowly moving from C to Python. This time I need to calculate partial derivatives numerically from a grid given. I know how to do it in C, so at the moment I just use inline adapter, i.e.</p>
  
  <pre><code>def dz(x,X,Y,Z,dx):
      y = numpy.zeros((X,Y,Z), dtype='double');
      code = """
              int i, j, k;
              for (i=0; i&lt;X-1; i++){
                  for(k=0; k&lt;Y; k++){
                      for (j=0; j&lt;Z; j++){
                          y[i,k,j] = (x[i+1, k, j] - x[i, k, j])/dx;
                          }
                      }
                  }
              for (j=0; j&lt;Z; j++){
                  for(k=0; k&lt;Y; k++){
                      y[X-1,k,j] = - x[X-1, k, j]/dx;
                      }
                  }
          """
      weave.inline(code, ['x', 'y', 'dx', 'X', 'Y', 'Z'], \
                  type_converters=converters.blitz, compiler = 'gcc');
      return y;
  </code></pre>
  
  <p>where <code>x</code> and <code>y</code> are 3D numpy arrays, as you can see, and the second loop stands for boundary conditions. Of course, I can implement the same logic in pure Python, but the code would be inefficient. I wonder, though, if it is possible to calculate a partial derivative using pure numpy? I would appreciate any help anyone can provide.</p>