1

This may seem like a silly question, but I am a bloody newby in Python (and in programming). I am running a physics simulation that involves many (~10 000) 2x2 matrices that I store in an array. I call these matrices M and the array T in the code below. Then I simply want to compute the product of all of these matrices. This is what I came up with, but it looks ugly and it would be so much work for 10000+ 2x2 matrices. Is there a simpler way or an inbuilt function that I could use?

import numpy as np
#build matrix M (dont read it, just an example, doesnt matter here)    
def M(k1 , k2 , x):
    a = (k1 + k2) * np.exp(1j * (k2-k1) * x)
    b = (k1 - k2) * np.exp(-1j * (k1 + k2) * x)
    c = (k1 - k2) * np.exp(1j * (k2 + k1) * x)
    d = (k1 + k2) * np.exp(-1j * (k2 - k1) * x)
    M = np.array([[a , b] , [c , d]])
    M *= 1. / (2. * k1)
    return M


#array of test matrices T
T = np.array([M(1,2,3), M(3,3,3), M(54,3,9), M(33,11,42) ,M(12,9,5)])
#compute the matrix product of T[0] * T[1] *... * T[4]
#I originally had this line of code, which is wrong, as pointed out in the comments
#np.dot(T[0],np.dot(T[1], np.dot(T[2], np.dot(T[2],np.dot(T[3],T[4])))))
#it should be:
np.dot(T[0], np.dot(T[1], np.dot(T[2],np.dot(T[3],T[4]))))
5
  • Are the matrices always i * M for i from 1 upto some number? Commented Apr 4, 2013 at 15:36
  • no, they are really complicated. I actually plan to compute them on the fly in a for loop, if I can figure out how to do it. should I change that in my code snippet? maybe it's confusing. Commented Apr 4, 2013 at 15:36
  • 2
    Is there a reason for putting the matrices in an array? I think I would probably just put them in a list ... Commented Apr 4, 2013 at 15:39
  • Indeed, a list would probably even be faster with a for loop. Commented Apr 4, 2013 at 15:40
  • ok, now I'm done editing. the reason why I put them into an array is because I do numerical calculations. I don't know how it would affect the precision if I put it into a list. does it matter? Commented Apr 4, 2013 at 15:44

1 Answer 1

1

Not very NumPythonic, but you could do:

reduce(lambda x,y: np.dot(x,y), T, np.eye(2))

Or more concisely, as suggested

reduce(np.dot, T, np.eye(2))
Sign up to request clarification or add additional context in comments.

7 Comments

lambda x, y: np.dot(x, y) is just np.dot.
I was thinking that this seemed like a job for reduce
you dont need the identity matrix, reduce(np.dot, T) works fine
are you sure that this is valid? maybe I'm doing it wrong, but I'm getting different results. with my method above and with your reduce() line mine: [[ 0.24112953-0.1020551j -0.22988481+0.11903133j] [-0.22988481-0.11903133j 0.24112953+0.1020551j ]] . yours: [[ 0.29577303-0.1009189j -0.24969598+0.08683577j] [-0.24969598-0.08683577j 0.29577303+0.1009189j ]]
np.dot(T[0],np.dot(T[1], np.dot(T[2], np.dot(T[2],np.dot(T[3],T[4]))))) you have T[2] twice
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.