<div dir="ltr"><span style="font-family:arial,sans-serif;font-size:13.3333339691162px">On Wed, Oct 29, 2014 at 10:39 AM, Andrew Nelson <</span><a href="mailto:andyfaff@gmail.com" style="font-family:arial,sans-serif;font-size:13.3333339691162px">andyfaff@gmail.com</a><span style="font-family:arial,sans-serif;font-size:13.3333339691162px">> wrote:</span><br style="font-family:arial,sans-serif;font-size:13.3333339691162px"><br style="font-family:arial,sans-serif;font-size:13.3333339691162px"><span style="font-family:arial,sans-serif;font-size:13.3333339691162px">> Dear list,</span><br style="font-family:arial,sans-serif;font-size:13.3333339691162px"><span style="font-family:arial,sans-serif;font-size:13.3333339691162px">> I have a 4D array, A, that has the shape (NX, NY, 2, 2).  I wish to</span><br style="font-family:arial,sans-serif;font-size:13.3333339691162px"><span style="font-family:arial,sans-serif;font-size:13.3333339691162px">> perform matrix multiplication of the 'NY' 2x2 matrices, resulting in the</span><br style="font-family:arial,sans-serif;font-size:13.3333339691162px"><span style="font-family:arial,sans-serif;font-size:13.3333339691162px">> matrix B.  B would have shape (NX, 2, 2).  I believe that np.einsum would</span><br style="font-family:arial,sans-serif;font-size:13.3333339691162px"><span style="font-family:arial,sans-serif;font-size:13.3333339691162px">> be up to the task, but I'm not quite sure of the subscripts I would need to</span><br style="font-family:arial,sans-serif;font-size:13.3333339691162px"><span style="font-family:arial,sans-serif;font-size:13.3333339691162px">> achieve this.</span><br style="font-family:arial,sans-serif;font-size:13.3333339691162px"><br><div><font face="arial, sans-serif">Ok, I'll try to explain in more detail of what I'm trying to do (I'm not skilled in matrix algebra).  </font></div><div><font face="arial, sans-serif"><br></font></div><div><font face="arial, sans-serif">Say I have a series of matrices, M, which are all 2x2: M_0, M_1, ..., M_{NY - 1}. These all need to be multiplied by each other.</font></div><div><font face="arial, sans-serif">i.e. N = M_0 x M_1 x  ... x M_{NY - 1}.</font></div><div><font face="arial, sans-serif">Note that I want to multiply M_0 by M_1, the result of that by M_2, the result of that by M_3 and so on.</font></div><div><font face="arial, sans-serif">I can hold the (NY) matrices in a single array that has shape (NY, 2, 2).  The first row in that array would be M_0, the last would be M_{NY-1}.  The output of all that matrix multiplication would be a single 2x2 matrix.  So I would've thought an operation would do something like this:</font></div><div><font face="arial, sans-serif"><br></font></div><div><span style="font-family:arial,sans-serif"> #there are NY-1 matrix multiplications involved here.</span><font face="arial, sans-serif"><br></font></div><div><font face="arial, sans-serif">M[NY, 2, 2] ----->  N[2, 2]     </font></div><div><font face="arial, sans-serif"><br></font></div><div><font face="arial, sans-serif">Now let's make the next level of complication, I have NX of those M[NY, 2, 2] matrices.</font><span style="font-family:arial,sans-serif"> So I need to do the above matrix multiplication series NX times.</span><span style="font-family:arial,sans-serif">  I could hold all this in an array, P, with shape (NX, NY, 2, 2).  Each of the NX rows are independent.</span></div><div><span style="font-family:arial,sans-serif">Currently I am doing this in a nested loop, pseudocode follows:</span></div><div><br></div><div><python></div><div>output = np.zeros((NX, 2, 2))</div><div><br></div><div><font face="arial, sans-serif">for i in range(NX):</font></div><div><font face="arial, sans-serif">    temp = np.identity(2)</font></div><div><font face="arial, sans-serif">    for j in range(NY):</font></div><div><span style="font-family:arial,sans-serif">        temp = np.dot(temp, P[i, j])</span></div><div><span style="font-family:arial,sans-serif"><br></span></div><div><font face="arial, sans-serif">    output[i] = temp</font></div><div><font face="arial, sans-serif"></python></font></div><div><font face="arial, sans-serif"><br></font></div><div><font face="arial, sans-serif">My original question was posted as I would like to remove that doubly nested loop, with something more elegant, as well as a whole load faster.  Is there an np.einsum that can furnish that?</font></div><div><font face="arial, sans-serif">(Please forgive me if this still isn't clear or precise enough).</font></div><div><br></div>-- <br>_____________________________________<br>Dr. Andrew Nelson<br><br><br>_____________________________________
</div>