[SciPy-user] addition of vectors of different lengths

Zhiwen Chong zhiwen.chong at elf.mcgill.ca
Tue Aug 2 21:59:08 CDT 2005


Hello,

On 2-Aug-05, at 10:27 PM, Dan Christensen wrote:
> way to do this sort of addition?  My current solution is slow,
> probably because of the use of concatenate:

I modified your code a little bit and profiled it: (machine iBook G4,  
1GHz)

from scipy import array, rand, concatenate
for i in range(100):
     v1 = rand(230,1)
     v2 = rand(256,1)
     l1 = len(v1)
     l2 = len(v2)
     if l1 == l2:
         v = v1+v2
     elif l1 < l2:
         v = concatenate((v1+v2[:l1], v2[l1:]))
     else:
         v = concatenate((v1[:l2]+v2, v1[l2:]))

The first few lines of the profiler output sorted by cumtime is:

ncalls  tottime percall cumtime percall filename:lineno(function)
0       0       0                       profile:0(profiler)
1       0       0       1.24    1.24    profile:0(execfile('test.py'))
1       0       0       1.22    1.22    :0(execfile)
55/1    0.02    0       1.22    1.22    <string>:1(?)
1       0       0       1.22    1.22    test.py:1(?)
1       0       0       0.95    0.95    __init__.py:8(?)
2       0.03    0.015   0.77    0.385   __init__.py:2(?)
1       0.02    0.02    0.36    0.36    _nc_imports.py:3(?)
1       0       0       0.36    0.36    numerix.py:19(?)
1       0       0       0.3     0.3     limits.py:2(?)
2       0.04    0.02    0.3     0.15    machar.py:48(__init__)
1       0       0       0.3     0.3     machar.py:8(?)
27      0       0       0.28    0.01    ppimport.py:127(ppimport)
200     0       0       0.26    0.001   ppimport.py:114(__call__)
2028    0.07    0       0.24    0       numerix.py:76(any)
2       0.02    0.01    0.22    0.11    __init__.py:5(?)
27      0.01    0       0.22    0.008   ppimport.py:35(_get_so_ext)
57/55   0       0       0.21    0.004   :0(getattr)

The concatenate function seems to be all the way down here:

100   0.01   0   0.01   0   Numeric.py:231(concatenate)

Maybe you could write a routine that does specifically what you want  
in Fortran, and wrap it with F2PY?
http://cens.ioc.ee/projects/f2py2e/#usage


Zhiwen
-- 
"If the women don't find you handsome, they should at least find you  
handy." - Red Green




More information about the SciPy-user mailing list