473,698 Members | 2,588 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

dot products

HI.
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%.)
rahul

Jul 18 '05 #1
12 8123
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)))


btw, imho the most "Pythonic" would be:

sum(i*j for (i,j) in zip(a,b))
Jul 18 '05 #2
Rahul wrote:
HI.
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.
Try:

sum(x * y for x, y in zip(a, b))

Between zip() (lockstep iteration over several sequences) and enumerate()
(iteration over a sequence, but also providing an index counter), it is rare
that you will want to use indexing notation in a generator expression.
(By the way profiling them i found that the second is faster by about 30%.)


For short sequences, generator expressions may end up slightly slower than list
comprehensions or for loops, as the latter two do not involve the overhead of
setting up the generator and retrieving values from it. As the sequences
increase in length, generator expressions generally win in the end due to their
reduced memory impact.

Cheers,
Nick.

--
Nick Coghlan | nc******@email. com | Brisbane, Australia
---------------------------------------------------------------
http://boredomandlaziness.skystorm.net
Jul 18 '05 #3
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.s tarmap(operator .mul, itertools.izip( a, b)))

but if you are really concerned about number-crunching efficiency, use
Numarray.

Peter

Jul 18 '05 #4
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.


.. >>> import numarray as na
.. >>> a, b = na.arange(5), na.arange(5, 10)
.. >>> na.dot(a, b)
.. 80

Steve
Jul 18 '05 #5
[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))

In general:
* reduction functions like sum() do not need their arguments to
take time building a full list; instead, an iterator will do fine
* applying itertools instead of genexps can save the eval-loop overhead
* however, genexps are usually more readable than itertools solutions
* xrange() typically beats range()
* but indexing is rarely the way to go
* izip() typically beats zip()
* imap() can preclude the need for either izip() or zip()
* the timeit module settles these questions quickly

Here are the some timings for vectors of length 10 and 3 respectively

C:\pydev>python timedot.py 3
1.25333310984 sum(a[i]*b[i] for i in xrange(len(a)))
1.16825625639 sum(x*y for x,y in izip(a,b))
1.45373455807 sum(x*y for x,y in zip(a,b))
0.635497577901 sum(imap(mul, a, b))
0.85894416601 sum(map(mul, a, b))

C:\pydev>python timedot.py 10
2.19490353509 sum(a[i]*b[i] for i in xrange(len(a)))
2.01773998894 sum(x*y for x,y in izip(a,b))
2.44932533231 sum(x*y for x,y in zip(a,b))
1.24698871922 sum(imap(mul, a, b))
1.49768685362 sum(map(mul, a, b))

Raymond Hettinger
Jul 18 '05 #6
Raymond Hettinger wrote:
* applying itertools instead of genexps can save the eval-loop overhead
* however, genexps are usually more readable than itertools solutions


I'm still waiting for you to implement itertools as a parse-tree analyzer/code generator,
rather than an "bare" extension module.

</F>

Jul 18 '05 #7

Raymond Hettinger wrote:
[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))

In general:
* reduction functions like sum() do not need their arguments to
take time building a full list; instead, an iterator will do fine
* applying itertools instead of genexps can save the eval-loop

overhead * however, genexps are usually more readable than itertools solutions
* xrange() typically beats range()
* but indexing is rarely the way to go
* izip() typically beats zip()
* imap() can preclude the need for either izip() or zip()
* the timeit module settles these questions quickly

Here are the some timings for vectors of length 10 and 3 respectively

C:\pydev>python timedot.py 3
1.25333310984 sum(a[i]*b[i] for i in xrange(len(a)))
1.16825625639 sum(x*y for x,y in izip(a,b))
1.45373455807 sum(x*y for x,y in zip(a,b))
0.635497577901 sum(imap(mul, a, b))
0.85894416601 sum(map(mul, a, b))

C:\pydev>python timedot.py 10
2.19490353509 sum(a[i]*b[i] for i in xrange(len(a)))
2.01773998894 sum(x*y for x,y in izip(a,b))
2.44932533231 sum(x*y for x,y in zip(a,b))
1.24698871922 sum(imap(mul, a, b))
1.49768685362 sum(map(mul, a, b))

Raymond Hettinger

Thanks all of you guys for enlightening me. Python is truly elegant.

Jul 18 '05 #8
On 19 Dec 2004 03:04:15 -0800, "Rahul" <co********@gma il.com> wrote:
HI.
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%.)
rahul

Don't know about the timing, but another way:
import operator
a, b = range(5), range(5,10)
sum(map(operato r.mul, a, b)) 80

Checking...
class OpShow(object): ... def __init__(self): self.tot = 0
... def __call__(self, x, y):
... prod = x*y
... self.tot += prod
... print '%3s * %-3s => %s (tot %s)' %(x, y, prod, self.tot)
... return prod
... sum(map(OpShow( ), a, b))

0 * 5 => 0 (tot 0)
1 * 6 => 6 (tot 6)
2 * 7 => 14 (tot 20)
3 * 8 => 24 (tot 44)
4 * 9 => 36 (tot 80)
80

Regards,
Bengt Richter
Jul 18 '05 #9
On Sun, Dec 19, 2004 at 03:04:15AM -0800, Rahul wrote:
HI.
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%.)
rahul


some numbers to confirm my previous reply:

1
zip: 0.00115 (1.00)
range: 0.00108 (0.94)
array: 0.00075 (0.65)
10
zip: 0.00306 (1.00)
range: 0.00288 (0.94)
array: 0.00074 (0.24)
100
zip: 0.02195 (1.00)
range: 0.02035 (0.93)
array: 0.00079 (0.04)
1000
zip: 0.21016 (1.00)
range: 0.19930 (0.95)
array: 0.00130 (0.01)
10000
zip: 4.98902 (1.00)
range: 2.70843 (0.54)
array: 0.01405 (0.00)

(the integers are the number of elements in a random array of floats;
'zip' refers to

sum([x*y for (x,y) in zip(a,b)])

'range', to

sum([a[i]*b[i] for i in range(len(a))])

and 'array' makes a and b Numeric's 'array' objects, with atlas
installed (and hence dotblas loading and assembler version of dot
tuned for the system's processor (in this case a pentium3)). The code
in this case is simply

Numeric.dot(a, b)

The advantage of atlas on systems with sse2 should be even greater.

--
John Lenton (jo**@grulic.or g.ar) -- Random fortune:
El deseo nos fuerza a amar lo que nos hará sufrir.
-- Marcel Proust. (1871-1922) Escritor francés.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.5 (GNU/Linux)

iD8DBQFBxv1BgPq u395ykGsRAvuKAJ kBULU763LN368mV FJf+trqku8/KQCbB75C
noYAuTFkRh4SJ3V mJCXpwZY=
=F9aS
-----END PGP SIGNATURE-----

Jul 18 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1481
by: frizzle | last post by:
Hi there I have a products site with a mysql backend. I have two tables: 1 with series, 1 with products belonging to certain series. Series looks like: - id - kind
0
3253
by: Ralph Guzman | last post by:
TASK: I have to generate a report with all categories, subcategories and products in database. PROBLEM: I want to write one query that will return: 1. category 2. subcategory: determined by parent_id 3. products in each category or subcategory.
2
3127
by: Hohn Upshew | last post by:
I need some help to build a report enumerating the products in descending order depending on the sum of liters. In this way i can view the top products sold for a given period.But i fail to do it. In my query i have build a total as follows SELECT products.Productid, products.grade, products.size, Sum(.liters) AS SumOfliters, orders.invoicedate FROM products INNER JOIN ((affiliates INNER JOIN Customers ON
1
2779
by: Mark | last post by:
My Category and Product tables look like: TblCategory CategoryID Category TblProduct ProductID CategoryID Product
6
3100
by: Paul T. Rong | last post by:
Dear all, Here is my problem: There is a table "products" in my access database, since some of the products are out of date and stopped manufacture, I would like to delete those PRODUCTS from the table, but I was not allowed to do that, because "there are records related with those PRODUCTS in other tables (e.g. in table "ORDER_DETAIL").
2
1913
by: TD | last post by:
I have this expression in the criteria section of a query: IIf(Forms!frmReports!cboProductID="<ALL PRODUCTS>",. Like "*",Forms!frmReports!cboProductID) (the syntax of the above expression may not be exact because I'm trying to write it from memory us now) I have a form with a combo box that I fill from a Union query that adds the phrase "<ALL PRODUCTS>" so that when you click on this combo box
24
1814
by: Rob R. Ainscough | last post by:
I was reading yet another book on .NET - VB 2005 Professional (wrox) and read the statement; "Microsoft has staked their future on .NET and publicly stated that henceforth almost all their research and development will be done on this platform. It is expected that, eventually, almost all Microsoft products will be ported to the .NET platform." So I thought about this statement for a minute -- then I realized that I have ALL...
9
1474
by: Earl | last post by:
I have somewhat of an interesting scenario: The form allows the user to select a service, which populates a a grid of product information related to that service ("service grid"). The user can then select from the products in the first grid and those choices populate the second "choices grid". The product grid is bound to a strongly-typed dataset table which can be filled in a couple of different ways. More on this shortly.
0
906
by: shapper | last post by:
Hello, I am displaying a list of products. I created a model using Linq To SQL. The List method is as follows: public void List(int? page) { dt.Products = (from p in db.Products orderby p.UnitPrice
0
8680
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8609
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9169
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
6528
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4371
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2335
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.