[Numpy-discussion] vectorization of vectorization

Scott Ransom sransom@nrao....
Thu Aug 19 16:31:20 CDT 2010


If you use already vectorized functions (like special.iv) you often don't 
need to use vectorization()

For example:

-----------------------------
import numpy as num
import scipy.special as special

def funct(order, t, power):
    return special.iv(order, t)**power

order = num.arange(4.0)
ts = num.linspace(0.2, 0.4, 3)

funct(order, ts[:,num.newaxis], 2.0)
-----------------------------

That gives:

In [39]: funct(order, ts[:,num.newaxis], 2.0)
Out[39]: 
array([[  1.02015056e+00,   1.01004176e-02,   2.51671536e-05,
          2.79169796e-08],
       [  1.04576573e+00,   2.30110211e-02,   1.28473444e-04,
          3.19983896e-07],
       [  1.08243587e+00,   4.16269171e-02,   4.10791968e-04,
          1.81365508e-06]])


With no loop.  And both order and t are vectors....

Scott



On Thursday, August 19, 2010 05:22:32 pm sm lkd wrote:
> Hello,
> 
> Here's my problem: for each value t of an array (from 0 to 1e6) a
> smaller array is computed (size between 2-6). To compute the smaller
> array, I have a function (which can be easily vectorized if necessary)
> which takes t and an array of powers of t. The return is an array of
> modified Bessel function values, i.e.:
> 
> def funct(order, t, power):
>    return special.iv(order, t)**power
> 
> Note that order and power are arrays after this vectorization:
> vec_func = sp.vectorization(func)
> 
> Right this is how it's used:
> for i in range(1000000):
>    y[i] = vec_func(orders, t, powers).prod()
> 
> Incredibly slow.
> 
> Of course, it is desirable to vectorize it it terms of t. I have tried
> different methods but still cannot make it work. Any suggestions or
> ointers?
> 
> Thank you.

-- 
Scott M. Ransom            Address:  NRAO
Phone:  (434) 296-0320               520 Edgemont Rd.
email:  sransom@nrao.edu             Charlottesville, VA 22903 USA
GPG Fingerprint: 06A9 9553 78BE 16DB 407B  FFCA 9BFA B6FF FFD3 2989


More information about the NumPy-Discussion mailing list