Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > dot products

Reply
Thread Tools

dot products

 
 
Raymond Hettinger
Guest
Posts: n/a
 
      12-21-2004
[Rahul].
> > 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.

>
> Yes:
> from itertools import imap
> from operator import mul
> ans = sum(imap(mul, a, b))


Doh! We all missed it.

If your vector length is known in advance (and it often is in some apps), the
simplest and fastest approach is:

a[0]*b[0] + a[1]*b[1] + a[2]*b[2]


C:\pydev>python timedot.py 3
0.32 sec: a[0]*b[0] + a[1]*b[1] + a[2]*b[2]
1.38 sec: sum(a[i]*b[i] for i in xrange(len(a)))
1.32 sec: sum(x*y for x,y in izip(a,b))
1.62 sec: sum(x*y for x,y in zip(a,b))
0.75 sec: sum(imap(mul, a, b))
1.04 sec: sum(map(mul, a, b))



Raymond Hettinger


 
Reply With Quote
 
 
 
 
Alan G Isaac
Guest
Posts: n/a
 
      12-21-2004
[Rahul].
> I want to compute dot product of two vectors stored as lists a and b.a
> and b are of the same length


from scipy import dot
ans=dot(a,b)

This times faster than the alternatives I have seen mentioned so far,
given scipy.

Cheers,
Alan Isaac


 
Reply With Quote
 
 
 
 
Alan G Isaac
Guest
Posts: n/a
 
      12-21-2004
"Alan G Isaac" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> This times faster than the alternatives I have seen mentioned so far,
> given scipy.


Actually, since I am new to 'timeit', I probably should check that
I am not overlooking something. Especially since I see an order
of magnitude difference in performance. Does the code below
give the right comparisons?

Thanks,
Alan Isaac

#-------------------------------------------------------------
import timeit
env1='''
from operator import mul
from itertools import imap
def innerprod(x,y):
return sum(imap(mul,x,y))
from scipy import rand
x=rand(50); y=rand(50)
'''
env2='''
from operator import mul
from itertools import imap
from scipy import rand
x=rand(50); y=rand(50)
'''
env3='''
from scipy import rand,dot
x=rand(50); y=rand(50)
'''
t1=timeit.Timer("innerprod(x,y)",env1)
t2=timeit.Timer("sum(imap(mul,x,y))",env2)
t3=timeit.Timer("dot(x,y)",env3)
trials=1000
print t1.repeat(2,trials) #about 0.1 seconds
print t2.repeat(2,trials) #about 0.1 seconds
print t3.repeat(2,trials) #about 0.01 seconds



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: what is the difference between the vb dot net and asp dot net Nathan Sokalski ASP .Net 11 08-14-2009 08:22 PM
Dot Net Project Execution without Dot Net and Framework.... krishnan ASP .Net Building Controls 0 01-07-2006 08:51 AM
Dynamically resize visible text and append dot dot dot (like gmail subject lines.. ) how to question? jawolter@gmail.com Javascript 6 12-12-2005 04:09 PM
volatile struct in dot h vs dot c Christopher M. Lusardi C Programming 3 05-15-2004 12:17 AM
To dot or not to dot? SamuŽl van Laere HTML 8 10-16-2003 02:55 PM



Advertisments