I would like to apply a function to each of the 3x3 matrices in my (6890,6890,3,3) numpy array. Until now, I have tried using vectorization on a smaller example and with a simpler function which didn't work out.
def myfunc(x):
return np.linalg.norm(x)
m = np.arange(45).reshape(5,3,3)
t = m.shape[0]
r = np.zeros((t, t))
q = m[:,None,...] @ m.swapaxes(1,2) # m[i] @ m[j].T
f = np.vectorize(q, otypes=[np.float])
res = myfunc(f)
Is vectorization even the right approach to solve this problem efficiently or should I try something else? I've also looked into numpy.apply_along_axis but this only applies to 1D-subarrays.
myfuncthe actual function that you are working with?pyquaternion.Quaternion.log(pyquaternion.Quaternion(matrix=m)).normnp.vectorizehas a note saying it doesn't promise any speedup. Also it normally passes scalar elements to your function, where as you want to pass 2d elements. It has asignaturemode that can do that, but that's even slower.