Re: [Numpy-discussion] Is there a better way to write a stacked matrix
26 Oct
2017
26 Oct
'17
2:01 p.m.
On Thu, Oct 26, 2017 at 12:11 PM, Daniele Nicolodi <daniele@grinta.net> wrote:
is there a better way to write the dot product between a stack of matrices? In my case I need to compute
y = A.T @ inv(B) @ A
with A a 3x1 matrix and B a 3x3 matrix, N times, with N in the few hundred thousands range. I thus "vectorize" the thing using stack of matrices, so that A is a Nx3x1 matrix and B is Nx3x3 and I can write:
y = np.matmul(np.transpose(A, (0, 2, 1)), np.matmul(inv(B), A))
If you only ever multiply your matrix inverse by a single vector then you may also wish to consider np.linalg.solve(B,A) which usually has a better prefactor (although for 3x3 it's pretty marginal, your hardware may vary). Peter
2384
Age (days ago)
2384
Last active (days ago)
0 comments
1 participants
participants (1)
-
Peter Creasey