optimising single value functions for array calculations
Hello, I am developing a module which bases its calculations on another specialised module. My module uses numpy arrays a lot. The problem is that the other module I am building upon, does not work with (whole) arrays but with single values. Therefore, I am currently forces to loop over the array: ### a = numpy.arange(100) b = numpy.arange(100,200) for i in range(0,a.size): a[i] = myfunc(a[i])* b[i] ### The results come out well. But the problem is that this way of calculation is very ineffiecent and takes time. May anyone give me a hint on how I can improve my code without having to modify the package I am building upon. I do not want to change it a lot because I would always have to run behind the chnages in the other package. To summarise: How to I make a calculation function array-aware? Thanks in advance, Timmie
Hello Timmie, numpy.vectorize(myfunc) should do what you want. Cheers, Emmanuelle
Hello, I am developing a module which bases its calculations on another specialised module. My module uses numpy arrays a lot. The problem is that the other module I am building upon, does not work with (whole) arrays but with single values. Therefore, I am currently forces to loop over the array:
### a = numpy.arange(100) b = numpy.arange(100,200) for i in range(0,a.size): a[i] = myfunc(a[i])* b[i]
###
The results come out well. But the problem is that this way of calculation is very ineffiecent and takes time.
May anyone give me a hint on how I can improve my code without having to modify the package I am building upon. I do not want to change it a lot because I would always have to run behind the chnages in the other package.
To summarise: How to I make a calculation function array-aware?
Thanks in advance, Timmie
_______________________________________________ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
I does not solve the slowness problem. I think I read on the list about an experimental code for fast vectorization. Nadav. -----הודעה מקורית----- מאת: numpy-discussion-bounces@scipy.org בשם Emmanuelle Gouillart נשלח: ב 01-דצמבר-08 12:28 אל: Discussion of Numerical Python נושא: Re: [Numpy-discussion] optimising single value functions for array calculations Hello Timmie, numpy.vectorize(myfunc) should do what you want. Cheers, Emmanuelle
Hello, I am developing a module which bases its calculations on another specialised module. My module uses numpy arrays a lot. The problem is that the other module I am building upon, does not work with (whole) arrays but with single values. Therefore, I am currently forces to loop over the array:
### a = numpy.arange(100) b = numpy.arange(100,200) for i in range(0,a.size): a[i] = myfunc(a[i])* b[i]
###
The results come out well. But the problem is that this way of calculation is very ineffiecent and takes time.
May anyone give me a hint on how I can improve my code without having to modify the package I am building upon. I do not want to change it a lot because I would always have to run behind the chnages in the other package.
To summarise: How to I make a calculation function array-aware?
Thanks in advance, Timmie
_______________________________________________ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
_______________________________________________ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
2008/12/1 Nadav Horesh <nadavh@visionsense.com>:
I does not solve the slowness problem. I think I read on the list about an experimental code for fast vectorization.
The choices are basically weave, fast_vectorize (http://projects.scipy.org/scipy/scipy/ticket/727), ctypes, cython or f2py. Any I left out? Ilan's fast_vectorize should have been included in SciPy a while ago already. Volunteers for patch review? Cheers Stéfan
Hi,
thanks for all your answers. I will certainly test it.
numpy.vectorize(myfunc) should do what you want.
Just to add a better example based on a recent discussion here on this list [1]: myfunc(x): res = math.sin(x) return res a = numpy.arange(1,20) => myfunc(a) will not work. => myfunc need to have a possibility to pass single values to math.sin either through interation (see my inital email) or through other options. (I know that numpy has a array aware sinus but wanted to use it as an example here.) My orriginal problem evolves from here timeseries computing [2]. Well, I will test and report back further. Thanks again and until soon, Timmie [1]: http://thread.gmane.org/gmane.comp.python.numeric.general/26417/focus=26418 [2]: http://thread.gmane.org/gmane.comp.python.scientific.user/18253
2008/12/1 Timmie <timmichelsen@gmx-topmail.de>:
Hello, I am developing a module which bases its calculations on another specialised module. My module uses numpy arrays a lot. The problem is that the other module I am building upon, does not work with (whole) arrays but with single values. Therefore, I am currently forces to loop over the array:
### a = numpy.arange(100) b = numpy.arange(100,200) for i in range(0,a.size): a[i] = myfunc(a[i])* b[i]
###
Hi, Safe from using numpy functions inside myfunc(), numpy has no way of optimizing your computation. vectorize() will help you to have a clean interface, but it will not enhance speed. Matthieu -- Information System Engineer, Ph.D. Website: http://matthieu-brucher.developpez.com/ Blogs: http://matt.eifelle.com and http://blog.developpez.com/?blog=92 LinkedIn: http://www.linkedin.com/in/matthieubrucher
participants (5)
-
Emmanuelle Gouillart
-
Matthieu Brucher
-
Nadav Horesh
-
Stéfan van der Walt
-
Timmie