[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') 
boltzmann_factor=numpy.exp(-beta*field*s)
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.

cheers,
David



----- 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>
Message-ID:
    <fd2dcd540906030820k6cf0bb08wa7d5518481ed4d77@mail.gmail.com>
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):

                field=s[(i+self.N+1)%self.N][j]+s[i-1][j]+s[i][(j+self.N+1
)%self.N]+s[i][j-1]

                boltzmann_factor=numpy.exp(-beta*field*s[i][j])

                if(boltzmann_factor>r[i][j]):

                    s[i][j]=-s[i][j]

        return s

Thanks,
-Ranjit

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
SciPy-user@scipy.org
http://mail.scipy.org/mailman/listinfo/scipy-user


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



      


More information about the SciPy-user mailing list