Rahul wrote: > I want to compute dot product of two vectors stored as lists a and b.a > and b are of the same length. > > one simple way is > sum(a[i]*b[i] for i in range(len(a))) > > another simple way is > ans=0.0 > for i in range(len(a)): > ans=ans+a[i]*b[i] > > But is there any other way which is faster than any of the above. (By > the way profiling them i found that the second is faster by about 30%.)
You could try sigma = 0 for ai, bi in itertools.izip(a, b): sigma += ai * bi or sum(itertools.starmap(operator.mul, itertools.izip(a, b))) but if you are really concerned about number-crunching efficiency, use Numarray. Peter -- http://mail.python.org/mailman/listinfo/python-list