<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html; charset=ISO-8859-1"
http-equiv="Content-Type">
<title></title>
</head>
<body text="#000000" bgcolor="#ffffff">
Den 04.03.2011 07:19, skrev Daniel Hyams:
<blockquote
cite="mid:AANLkTi=Yrwm=nH1=zXyZDmXzrXSi+HtVPAZmeS_kWCn0@mail.gmail.com"
type="cite"><span class="Apple-style-span" style="border-collapse:
collapse; font-family: arial,sans-serif; font-size: 13px;">This
is probably so easy, I'm embarrassed to ask it...but I've been
casting around trying things to no avail for the last hour and a
half, so here goes...</span>
<div>
<font class="Apple-style-span" face="arial, sans-serif"><span
class="Apple-style-span" style="border-collapse: collapse;"><br>
</span></font>
<div><span style="border-collapse: collapse; font-family:
arial,sans-serif; font-size: 13px;">
<div>
I have a lot of dot products to take. The length-3
vectors that I want to dot are stacked in a 2D array like
this:</div>
<div><br>
</div>
<div>U = [u1 u2 u3....]</div>
<div><br>
</div>
<div>and</div>
<div> </div>
<div>V = [v1 v2 v3....]</div>
<div><br>
</div>
<div>So both of these arrays, are, say, 3x100 each. <br>
</div>
</span></div>
</div>
</blockquote>
<br>
That is the diagonal of matrix product U * V.T, that is<br>
<br>
np.diag(np.dot(U,V.T))<br>
<br>
You can also write this as <br>
<br>
np.sum(U*V, axis=1)<br>
<br>
The latter is faster with 100 x 3 arrays:<br>
<br>
>>> timeit.Timer(stmt='np.diag(np.dot(a,a.T))',
setup='import numpy as np; a=np.zeros((100,3))').timeit(number=1000)<br>
0.0568983632238087<br>
<br>
>>> timeit.Timer(stmt='np.sum(a*a,axis=1)', setup='import
numpy as np; a=np.zeros((100,3))').timeit(number=1000)<br>
0.03596760426239598<br>
<br>
We can do this with Python lists of NumPy arrays as well, avoiding
stacking the arrays in a 2D structure:<br>
<br>
[np.dot(u,v.T) for u,v in zip(U,V)]<br>
<br>
with U and V lists of 1D ndarrays instead of 2D ndarrays.<br>
<br>
>>> timeit.Timer(stmt='[np.dot(x.T,x) for x in a]',
setup='import numpy as np; a=100*[np.zeros(3)]').timeit(number=1000)<br>
0.21054868368582902<br>
<br>
While this is slower, we don't need to create the 2D stacks, and it
only took 200 µs on average. <br>
<br>
It could be useful is vectors are so big (or plentyful) that memory
is an issue, or just to write more Pythonic code. We don't need to
write FORTRAN in Python, computers are fast.<br>
<br>
Sturla<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
</body>
</html>