[Numpy-discussion] memoization with ndarray arguments

Paul Northug pnorthug at gmail.com
Sat Mar 21 14:18:56 EDT 2009


I would like to 'memoize' the objective, derivative and hessian
functions, each taking a 1d double ndarray argument X, that are passed
as arguments to
scipy.optimize.fmin_ncg.

Each of these 3 functions has calculations in common that are
expensive to compute and are a function of X. It seems fmin_ncg
computes these quantities at the same X over the course of the
optimization.

How should I go about doing this?

numpy arrays are not hashable, maybe for a good reason. I tried anyway
by  keeping a dict of hash(tuple(X)), but started having collisions.
So I switched to md5.new(X).digest() as the hash function and it seems
to work ok. In a quick search, I saw cPickle.dumps and repr are also
used as key values.

I am assuming this is a common problem with functions with numpy array
arguments and was wondering what the best approach is (including not
using memoization).

Thanks,
Pål.



More information about the NumPy-Discussion mailing list