<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<p dir="ltr">There was an idea on this list to provide a function the run multiple dot on several vectors/matrices. It seems to be a particular implementation of this proposed function.</p>
<p dir="ltr">  Nadav.</p>
<div class="gmail_quote">On 22 May 2015 11:58, David Cournapeau <cournape@gmail.com> wrote:<br type="attribution">
<blockquote class="quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div>
<div dir="ltr"><br>
<div><br>
<div class="elided-text">On Fri, May 22, 2015 at 5:39 PM, Mathieu Blondel <span dir="ltr">
<<a href="mailto:mathieu@mblondel.org">mathieu@mblondel.org</a>></span> wrote:<br>
<blockquote style="margin:0 0 0 0.8ex;border-left:1px #ccc solid;padding-left:1ex">
<div dir="ltr">
<div>
<div>
<div>
<div>
<div>
<div>
<div>
<div>Hi,<br>
<br>
</div>
I often need to compute the equivalent of<br>
<br>
</div>
np.diag(np.dot(A, B)).<br>
<br>
</div>
Computing np.dot(A, B) is highly inefficient if you only need the diagonal entries. Two more efficient ways of computing the same thing are<br>
<br>
np.sum(A * B.T, axis=1)<br>
<br>
and<br>
<br>
np.einsum("ij,ji->i", A, B).<br>
<br>
</div>
The first can allocate quite a lot of temporary memory.<br>
</div>
The second can be quite cryptic for someone not familiar with einsum.<br>
<div>I assume that einsum does not compute np.dot(A, B), but I haven't verified.<br>
</div>
</div>
<div><br>
</div>
Since this is is quite a recurrent pattern, I was wondering if it would be worth adding a dedicated function to NumPy and SciPy's sparse module. A possible name would be "diagdot". The best performance would be obtained when A is C-style and B fortran-style.<br>
</div>
</div>
</div>
</blockquote>
<div><br>
</div>
<div>Does your implementation use BLAS, or is just a a wrapper around einsum ?</div>
<div><br>
</div>
<div>David</div>
</div>
<br>
</div>
</div>
</div>
</blockquote>
</div>
</body>
</html>