[SciPy-dev] Using randomkit in extensions

Tom Loredo loredo at astro.cornell.edu
Thu May 18 17:19:30 EDT 2006


Hi folks,

There's been talk here in the last few months about the need
to link to some numpy/scipy libraries in user's extensions.
As far as I can follow the discussion, no solution to this
problem has been found yet.  Clue me in if I'm wrong!

The recommended workaround has been to just duplicate code.
It's inefficient but it works!  That's what I've been doing.
But it's a problematic solution for extensions that use
random numbers.  Code should produce reproducible results.
Robert added the ability to save/restore state to numpy.random
specifically to support reproducibility.  Simply copying
randomkit.[ch] or mtrand.c breaks this.

So far my extensions have needed a deterministic number
of uniform or normal samples, so I've written them to 
accept a numpy array of samples as input (the Python 
wrapper around the extension code hides this).  But now
I'm writing an extension that internally uses a rejection
technique, with the expected # of rejections varying widely
from case to case.  So it's not possible to send it an
array with the needed samples.

A possible workaround would be to grab mtrand's state
in Python and communicate it to my extension's copy of
*rand*.  The Python wrapper around the extension call
could get the state back after the call and update
numpy's mtrand state appropriately.

Does this sound sensible?  Is there a better way?  If
this would work, how should I proceed (I've never used
Pyrex---will it be necessary)?  I can write the
extension in Fortran, C or C++.

Thanks,
Tom

-------------------------------------------------
This mail sent through IMP: http://horde.org/imp/




More information about the SciPy-Dev mailing list