[SciPy-user] can this be vectorized?

Ranjit Chacko rjchacko@gmail....
Wed Jun 3 10:20:22 CDT 2009


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.html 


More information about the SciPy-user mailing list