title: How can I create an n-dimensional grid in numpy to evaluate a function for arbitrary n?
body: <p>I'm trying to create a naive numerical integration function to illustrate the benefits of Monte Carlo integration in high dimensions. I want something like this:</p>
  
  <pre><code>def quad_int(f, mins, maxs, numPoints=100):
      '''
      Use the naive (Riemann sum) method to numerically integrate f on a box 
      defined by the mins and maxs.
  
      INPUTS:
      f         - A function handle. Should accept a 1-D NumPy array 
          as input.
      mins      - A 1-D NumPy array of the minimum bounds on integration.
      maxs      - A 1-D NumPy array of the maximum bounds on integration.
      numPoints - An integer specifying the number of points to sample in 
          the Riemann-sum method. Defaults to 100.
      '''
      n = len(mins)
  
      # Create a grid of evenly spaced points to evaluate f on
      # Evaluate f at each point in the grid; sum all these values up
      dV = np.prod((maxs-mins/numPoints))
      # Multiply the sum by dV to get the approximate integral
  </code></pre>
  
  <p>I know my <code>dV</code> is going to cause problems with numerical stability, but right now what I'm having trouble with is creating the domain. If the number of dimensions was fixed, it would be easy enough to just use <code>np.meshgrid</code> like this:</p>
  
  <pre><code># We don't want the last value since we are using left-hand Riemann sums
  x = np.linspace(mins[0],maxs[0],numPoints)[:-1]
  y = np.linspace(mins[1],maxs[1],numPoints)[:-1]
  z = np.linspace(mins[2],maxs[2],numPoints)[:-1]
  
  X, Y, Z = np.meshgrid(x,y,z)
  tot = 0
  for index, x in np.ndenumerate(X):
      tot += f(x, Y[index], Z[index])
  </code></pre>
  
  <p>Is there an analogue to <code>np.meshgrid</code> that can do this for arbitrary dimensions, maybe accept a tuple of arrays? Or is there some other way to do Riemann sums in higher dimensions? I've thought about doing it recursively but can't figure out how that would work.</p>