[SciPy-user] nested for loops

Nils Wagner nwagner at iam.uni-stuttgart.de
Wed Jul 5 07:46:30 EDT 2006


Christian Kristukat wrote:
> Nils Wagner wrote:
>   
>> Hi all,
>>
>> How can I improve the computation of U in the attached script ?
>> An example would be appreciated.
>>
>>     
>
> Make use of the numpy array operations and the broadcasting feature. See the
> attached file. The computation is about 60 times faster here.
>
> Christian
>
>
>   
> ------------------------------------------------------------------------
>
> from scipy import *
> import time
>
> from pylab import contourf, show, subplot, figure, title, colorbar
> xcoor=linspace(-1,1,30)
> ycoor=linspace(-1,1,30)
> print shape(xcoor)
> print shape(ycoor)
> X1,Y1 = meshgrid(xcoor,ycoor)
> print shape(xcoor)
> print shape(ycoor)
> xcoor=linspace(-1,1,30)
> ycoor=linspace(-1,1,30)
> U  = zeros((len(xcoor),len(ycoor)),float)
> file = open('mode.dat')
> v = io.read_array(file)
> N = len(v)
>
> start = time.time()
> z_ik = xcoor[:,newaxis]+1j*ycoor[newaxis,:]
> U = zeros(z_ik.shape)
>
> for n in arange(1,N+1):
>     U = U + sin(2.*n*angle(z_ik)/3.)*special.jv(2.*n/3.,sqrt(19.739209)*abs(z_ik))*v[n-1]
> print time.time()-start
> #
> # How can I improve the computation of the mode shape U = ?
> #
> X1,Y1 = meshgrid(xcoor,ycoor)
> figure()
> CS=contourf(X1,Y1,U,20)
> cbar=colorbar(CS)
> show()
>
>   
> ------------------------------------------------------------------------
>
> _______________________________________________
> SciPy-user mailing list
> SciPy-user at scipy.org
> http://projects.scipy.org/mailman/listinfo/scipy-user
>   

Great !

Thank you very much for your prompt reply.

Nils




More information about the SciPy-User mailing list