[SciPy-user] Loopless square 2d arrays with radially symmetric functions
David Baddeley
david_baddeley@yahoo.com...
Wed Mar 25 15:56:53 CDT 2009
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
Message: 3
Date: Wed, 25 Mar 2009 11:22:46 -0400
From: josef.pktd@gmail.com
Subject: Re: [SciPy-user] Loopless square 2d arrays with radially
symmetric functions
To: SciPy Users List <scipy-user@scipy.org>
Message-ID:
<1cd32cbb0903250822s78f66a08hc423b17aff761049@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1
On Wed, Mar 25, 2009 at 10:58 AM, David Vine <djvine@gmail.com> wrote:
> Hello,
>
> I use a lot of radially symmetric arrays in my code and I would like to know
> if there is a more efficient method for creating these arrays than the
> nested for-loops i am currently using.
>
> For example, to create a Gaussian I would currently do this (Python 2.5,
> Ubuntu Intrepid):
> import scipy
> p = scipy.zeros((256,256))
> for i in xrange(256):
> ?? for j in xrange(256):
> ?????? p[i,j] = exp(-0.01*( (i-128.)**2. - (j-128.)**2.)? )
>
> and my question is whether there is a more efficient 'loopless' method (i.e
> involving no for-loops) ?
>
> Thanks in advance
> David
>
I think this does the same with broadcasting
import numpy as np
i = np.arange(256)
p = np.exp(-0.01*( (i[:,np.newaxis]-128.)**2. - (i[np.newaxis,:]-128.)**2.))
Josef
------------------------------
Get fit, feel better! All the help you need at Yahoo!Xtra Health - http://nz.lifestyle.yahoo.com/health
More information about the SciPy-user
mailing list