[SciPy-user] Loopless square 2d arrays with radially symmetric functions

Gary Ruben gruben@bigpond.net...
Thu Mar 26 16:42:45 CDT 2009


or this avoids X and Y:

R2 = numpy.hypot(*numpy.ogrid[-128:128,-128:128])**2
p = numpy.exp(-0.01*R2)

or, naturally:

p = numpy.exp(-0.01*numpy.hypot(*numpy.ogrid[-128:128,-128:128])**2)

Gary R.

Ryan May wrote:
> 
> On Wed, Mar 25, 2009 at 3:56 PM, David Baddeley 
> <david_baddeley@yahoo.com.au <mailto:david_baddeley@yahoo.com.au>> wrote:
> 
> 
>     There's always:
> 
>     X, Y = numpy.mgrid[-128:128, -128:128]
>     R2 = X**2 + Y**2
>     p = numpy.exp(-0.01*R2)
> 
>     which uses a bit more memory, but avoids loops.
> 
>     David
> 
> 
> If you use ogrid instead of mgrid, you'll avoid the increase in memory, 
> since the arrays are created in a form ready for broadcasting.
> 
> Ryan



More information about the SciPy-user mailing list