1

To follow up on this question I am new to python and I am trying to calculate the exponential of a product Matrix - Scalar using vectorization (if possible)

What I did:

n=10
t_ = np.arange(1,n+1)*5*np.pi/n
a_11,a_12, a_21, a_22=0,1,-1,-1
x_0,v_0=1,1
A = np.array([[a_11,a_12], [a_21, a_22]])  
A_ = np.array([A  for k in  range (1,n+1,1)])

X_0 = np.array([[x_0],[v_0]]) # build X_0
print A

x_=scipy.linalg.expm(t_[:,None,None]*A[None,:,:])*X_0

I get the following error within linalg.expm:

ValueError: expected a square matrix

Any help is much appreciated.

Conjecture
  • 353
  • 4
  • 18
  • Do you know what `t_[:,None,None]` means ? You're passing a `10x2x2` multidimensional array to `linalg.expm`. Of course it won't work. Clarify what you want to compute. – llllllllll Feb 06 '18 at 21:26
  • @liliscent I am not sure what it does mean, I guess it's to do elementwise multiplication and it works when I use vectorization to compute a np.array of At elements for each element t from t_. However when I try to get an np.array of exp(At) elements for each t in t_ I get an error. – Conjecture Feb 06 '18 at 21:44
  • 1
    A simple iteration is the way to go, as illustrated in this question https://stackoverflow.com/questions/35486239/matrix-exponentiation-in-python. `expm` the `Pade` approximation, and only works with one square matrix (it uses a matrix `inv`). – hpaulj Feb 06 '18 at 22:05
  • @hpaulj: Thanks for the useful link. I have a problem when I use the iteration: y_ = [scipy.linalg.expm(t*A)*X_0 for t in t_] I get an array of n elements of shape (2,2) whereas I expect an array of (2,1) shaped elements after multiplication with X_0 element. Could you please help me find my error ? Looks like np.matrix is more intelligent for doing matrix-vector multiplication? – Conjecture Feb 07 '18 at 10:36

0 Answers0