Reputation: 101
A = [a1, a2]
, B = [b1, b2]
, C = [c1, c2]
, D = [d1, d2]
.
The outer product of A, B, C, D
is just:
[
[
[[a1*b1*c1*d1, a1*b1*c1*d2], [a1*b1*c1*d1, a1*b1*c2*d2]],
[[a1*b2*c2*d1, a1*b2*c1*d2], [a1*b2*c2*d1, a1*b2*c2*d2]]
],
[
[[a2*b1*c1*d1, a2*b1*c1*d2], [a2*b1*c2*d1, a2*b1*c2*d2]],
[[a2*b2*c1*d1, a2*b2*c1*d2], [a2*b2*c2*d1, a2*b2*c2*d2]]
]
]
In numpy, the outer product is thus an array of shape (2, 2, 2, 2)
. I know how to do this. The problem is, I have 100 this kind of A, B, C, D. They are taken from A_k, B_k, C_k, D_k by fixing k. So they are actually array of shape (2, 100) or (100, 2). Actually may be (20, 100) or (100, 20). I use 2 to simplify the writing. What I want to do is: do this kind of outer product for 100 times and then sum them up. That is: to sum 100 arrays of shape (2, 2, 2, 2). How can I do this efficiently with numpy? If better, is there any GPU acceleration way of doing this?
If A, B, C, D are just 1d array, I know how to do this. The following one-line code works fine:
a[:, None, None, None] * b[None, :, None, None] * c[None, None, :, None] * d[None, None, None, :]
Upvotes: 0
Views: 169
Reputation: 3302
numpy.einsum plus numpy.sum with careful selection of axes should give you what you need.
Upvotes: 1