[SciPy-User] 3D array problem in Python (Happyman)

federico vaggi vaggi.federico@gmail....
Sun Dec 30 13:09:37 CST 2012


There are faster ways to loop over the array using array slicing:

http://www.scipy.org/Tentative_NumPy_Tutorial#head-864862d3f2bb4c32f04260fac61eb4ef34788c4c

If, however, you have to apply a slow function to all non-zero elements,
that will probably be your bottleneck, and fast access using array slicing
won't help you.

By the way, as a quick hint, an easy way to access all non-zero elements of
an array is using logical indexing, which you will be familiar with if
you've used MATLAB before.  You appear however to be using python lists and
not numpy arrays judging by your syntax.

> ----------------------------------------------------------------------
>
> Message: 1
> Date: Sun, 30 Dec 2012 07:32:13 +0400
> From: Happyman <bahtiyor_zohidov@mail.ru>
> Subject: [SciPy-User] 3D array problem in Python
> To: scipy-user <scipy-user@scipy.org>
> Message-ID: <1356838333.805656881@f211.mail.ru>
> Content-Type: text/plain; charset="utf-8"
>
>  Hello
>
> I have 3 dimensional array ?which I want ?to calculate in a huge process.
> Everything is working well if I use ordinary way which is unsuitable in
> Python like the following:
>
> nums=32
> rows=120
> cols=150
>
> for k in range(0,nums):
> ? ? ? ? ? for i in range(0,rows):
> ? ? ? ? ? ? ? ? ? ? ?for j in range(0,cols):
> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if float ( R[ k ] [ i ] [ j ] ) ==
> 0.0:
> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?val11 [ i ] =0.0
> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? else:
> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?val11[ i ] [ j ], val22[
> i ][ j ] = integrate.quad( lambda x : ?F1(x)*F2(x) , 0 , pi)
>
> But, this calculation takes so long time, let's say about ?1 hour
> (theoretically)... Is there any better way to easily and fast calculate the
> process such as [ F( i ) for i in xlist ] or something like that rather
> than using for loop?
>
>
> ?
> -------------- next part --------------
> An HTML attachment was scrubbed...
> URL:
> http://mail.scipy.org/pipermail/scipy-user/attachments/20121230/72f77b63/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 112, Issue 44
> *******************************************
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/scipy-user/attachments/20121230/c6e88d7b/attachment.html 


More information about the SciPy-User mailing list