Let X be a M x N matrix. Denote xi the i-th column of X. I want to create a 3 dimensional N x M x M array consisting of M x M matrices xi.dot(xi.T)
.
How can I do it most elegantly with numpy? Is it possible to do this using only matrix operations, without loops?
One approach with broadcasting
-
X.T[:,:,None]*X.T[:,None]
Another with broadcasting
and swapping axes afterwards -
(X[:,None,:]*X).swapaxes(0,2)
Another with broadcasting
and a multi-dimensional transpose afterwards -
(X[:,None,:]*X).T
Another approach with np.einsum
, which might be more intuitive thinking in terms of the iterators involved if you are translating from a loopy code -
np.einsum('ij,kj->jik',X,X)
Basic idea in all of these approaches is that we spread out the last axis for elementwise multiplication against each other keeping the first axis aligned. We achieve this process of putting against each other by extending X
to two 3D
array versions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With