Multiply array with diagonal matrix stored as vector
<p>I have a 1D array A = [a, b, c...] (length N_A) and a 3D array T of shape (N_A, N_B, N_A). A is meant to represent a diagonal N_A by N_A matrix.</p>

<p>I'd like to perform contractions of A with T without having to promote A to dense storage. In particular, I'd like to do </p>

<pre><code>np.einsum('ij, ikl', A, T)
</code></pre>

<p>and </p>

<pre><code>np.einsum('ikl, lm', T, A)
</code></pre>

<p>is it possible to do such things while keeping A sparse?</p>

<p>Note this question is similar to </p>

<p><a href="http://stackoverflow.com/questions/36152392/dot-product-with-diagonal-matrix-without-creating-it-full-matrix">dot product with diagonal matrix, without creating it full matrix</a></p>

<p>but not identical, since it's not clear to me how one generalizes to more complicated index patterns.</p>

<p><code>np.einsum('ij, ikl', np.diag(a), t)</code> is equivalent to <code>(a * t.T).T</code>.</p>

<p><code>np.einsum('ikl, lm', t, np.diag(a))</code> is equivalent to <code>a * t</code>.</p>

<p>(found by trial-and-error)</p>

python numpy