[Pyrex] using numpy arrays and random number generators in Pyrex

Mark P. Miller mpmusu at cc.usu.edu
Wed Jan 31 22:27:13 UTC 2007


Thanks again Robert:

Robert Kern wrote:
> If you have to do many iterations and generate one random number each iterate
> with different parameters each time (and unknown until you get to that iterate),
> then pyrexGSL might be useful. MCMC is one such application. Of course, you can
> also extract the C and Pyrex declarations from numpy/random/mtrand/ for the same
> purpose. Depending on your needs, that might be a better option: that code is
> BSD-licensed and smaller and more self-contained than the GSL. Of course, I'm
> biased; I wrote the non-uniform generators for it.
> 

Working somehow with mtrand makes some sense to me...but the specific 
implementation is not yet clear.  In general, my pyrex functions look 
something like this:

import numpy as np

def function1(int var1, int var2, float var3, float mean):
    cdef float a
    cdef float result
    a = np.random.exponential(mean)
    #do a bunch of additional calculations with a and other passed
    #function parameters
    return result

There are many functions like this that get called at different places 
throughout the code.  In general, it is not known in advance when a 
particular function will get called, nor is it always known exactly how 
many times each function will get called.  For this reason, I have shied 
away from generating lots of random numbers in advance (that and the 
fact that I don't completely understand how to efficiently iterate 
through and use elements from a returned array of random numbers).

Any additional thoughts/suggestions?

-Mark



More information about the Pyrex mailing list