Issue
Let's say I have the following code:
# define a 3x3 array
A = np.array([[-2, 1, 1], [1,-2,1], [1,1,-2]])
# define a 1D array of scalars to multiply A with
test = np.arange(10)
for i in range(len(test)):
matrix = scipy.linalg.expm(A*test[i])
I want to see if there is a way to do this multiplication without using a for loop. I am not trying to multiply the two arrays using matrix multiplication. I am treating the test array as a bank of scalar values that I want to multiply A with one by one. There has got to be some kind of sneaky numpy way of doing this. Any ideas?
Solution
scipy.linalg.expm
cannot be applied with a not squared matrix:
ValueError: expected a square matrix
so I think the easiest thing is to do:
list_result = list(map(lambda i: scipy.linalg.expm(A*i), range(10)))
An then if you want only one array:
np.concatenate(list_result) #for 2D
or
np.stack(list_result) #for 3D
Answered By - ansev
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.