[SciPy-user] can this be vectorized?

David Baddeley david_baddeley@yahoo.com...
Wed Jun 3 16:48:53 CDT 2009

Hi Ranjit,

I think this is going to be hard to vectorise your code as it is, as the array s (which is used to calculate field) changes within the loop. 

r = numpy.random.random((self.N,self.N))
field= scipy.ndimage.convolve(s, numpy.array([[0,1,0],[1,0,1],[0,1,0]]), 'same') 
s = s*2*(0.5 - (boltzmann_factor>r))

would give you similar results with a few subtle differences (the 'field' is calculated with the initial s rather than the constantly updated s) which may or may not be important depending on how large you expect your boltzmann factor to be. Also note that the convolution the way I've written it doesn't wrap at the edges - you could achieve this by circularly padding s before the convolution and then cropping back down to the original size.


----- Original Message ----

Date: Wed, 3 Jun 2009 11:20:22 -0400
From: Ranjit Chacko <rjchacko@gmail.com>
Subject: Re: [SciPy-user] can this be vectorized?
To: Robert Kern <robert.kern@gmail.com>
Cc: SciPy Users List <scipy-user@scipy.org>
Content-Type: text/plain; charset="iso-8859-1"

Thanks, that worked but I guess I was actually asking the wrong question.
Here's some code with loops that I want to speed up. I have an example where
someone used weave to speed it up, but I'd like to see if there's a way to
speed this up using just numpy.

    def oneMCS(self,s,beta):

        r = numpy.random.random((self.N,self.N))

        for i in range(self.N):

            for j in range(self.N):





        return s


On Tue, Jun 2, 2009 at 4:11 PM, Robert Kern <robert.kern@gmail.com> wrote:

> On Tue, Jun 2, 2009 at 15:05, Ranjit Chacko <rjchacko@gmail.com> wrote:
> > I have a square array A and I want to produce a second square array B of
> the
> > same dimension where each element of B is the sum of a square
> neighborhood
> > of each element of A. Is there a way to do this without loops in numpy,
> or
> > do I have to use for loops?
> scipy.ndimage.convolve() with a square array of 1s the size of the
> desired neighborhood as the weights parameter.
> --
> Robert Kern
> "I have come to believe that the whole world is an enigma, a harmless
> enigma that is made terrible by our own mad attempt to interpret it as
> though it had an underlying truth."
>  -- Umberto Eco
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/scipy-user/attachments/20090603/66fc9cdf/attachment-0001.html 


SciPy-user mailing list

End of SciPy-user Digest, Vol 70, Issue 5


More information about the SciPy-user mailing list