title: How to cache the function that is returned by scipy interpolation
body: <p>Trying to speed up a potential flow aerodynamic solver. Instead of calculating velocity at an arbitrary point using a relatively expensive formula I tried to precalculate a velocity field so that I could interpolate the values and (hopefully) speed up the code. Result was a slow-down due (I think) to the scipy.interpolate.RegularGridInterpolator method running on every call. How can I cache the <em>function</em> that is the result of this call? Everything I tried gets me hashing errors.</p>
  
  <p>I have a method that implements the interpolator and a second 'factory' method to reduce the argument list so that it can be used in an ODE solver.</p>
  
  <p>x_panels and y_panels are 1D arrays/tuples, vels is a 2D array/tuple, x and y are floats.</p>
  
  <pre><code>def _vol_vel_factory(x_panels, y_panels, vels):
  # Function factory method
  def _vol_vel(x, y, t=0):
      return _volume_velocity(x, y, x_panels, y_panels, vels)
  return _vol_vel
  
  def _volume_velocity(x, y, x_panels, y_panels, vels):
  velfunc = sp_int.RegularGridInterpolator(
          (x_panels, y_panels), vels
          )
  return velfunc(np.array([x, y])).reshape(2)
  </code></pre>
  
  <p>By passing tuples instead of arrays as inputs I was able to get a bit further but converting the method output to a tuple did not make a difference; I still got the hashing error.</p>
  
  <p>In any case, caching the result of the _volume_velocity method is not really what I want to do, I really want to somehow cache the result of _vol_vel_factory, whose result is a function. I am not sure if this is even a valid concept.</p>