-1

I would like to efficiently calculate all pairwise cross products of the rows of two matrices, A and B, which are nx3 and mx3 in size. And would ideally like to achieve this in einsum notation.

i.e. the output Matrix C, would be (n X m x 3),

where

C[0][0] = cross(n[0],m[0])

C[0][1] = cross(n[0],m[1])

...

C[1][0] = cross(n[1],m[0])

...

Due to the approach I am taking, using for loops aren't an option.

Any help would be much appreciated.

oracle3001
  • 1,090
  • 19
  • 31
  • 1
    `for loops aren't an option` -- you are going to have to explain that, because on the face of it, that's nonsense. – Tim Roberts Apr 22 '21 at 00:27
  • What does `cross` say about its inputs? I know it can work with a pair of `(n,3)` arrays – hpaulj Apr 22 '21 at 00:29
  • Its possible to do a single cross product using einsum in this manner, https://stackoverflow.com/questions/39662540/cross-products-with-einsums, I presume there is a way of adjusting this to do all pairwise. I have managed to do the all pairwise dot product, e.g. n1_dot_n2 = jnp.einsum('ji,ki-> jk', n1, n2), thus I presume there is some adjustment to the einsum cross product to achieve the same thing? – oracle3001 Apr 22 '21 at 00:31
  • 1
    Looks like `cross` broadcasts the leading dimensions. `np.cross(A[:, None,:], B[None, :,:])` The source code is easily found from the docs. – hpaulj Apr 22 '21 at 00:38
  • Perfect. Thanks. – oracle3001 Apr 22 '21 at 00:53

1 Answers1

1

Looks like cross broadcasts the leading dimensions.

np.cross(A[:, None,:], B[None, :,:])
hpaulj
  • 221,503
  • 14
  • 230
  • 353