Python / Numpy - cross product of matching strings in two arrays

What is the best way to cross product each matching row between two arrays? For instance:

a = 20x3 array
b = 20x3 array
c = 20x3 array = some_cross_function(a, b) where:
c[0] = np.cross(a[0], b[0])
c[1] = np.cross(a[1], b[1])
c[2] = np.cross(a[2], b[2])
...etc...

I know this can be done with a simple python loop or with numpy apply_along_axis, but I am wondering if there is a good way to do this completely in C numpy base code. I am currently using a simple loop, but this is by far the slowest part of my code (my actual arrays are tens of thousands of lines).

+5
source share
1 answer

I will probably have to delete this answer after a few minutes when I understand my mistake, but is the obvious thing not working?

>>> a = np.random.random((20,3))
>>> b = np.random.random((20,3))
>>> c = np.cross(a,b)
>>> c[0], np.cross(a[0], b[0])
(array([-0.02469147,  0.52341148, -0.65514102]), array([-0.02469147,  0.52341148, -0.65514102]))
>>> c[1], np.cross(a[1], b[1])
(array([-0.0733347 , -0.32691093,  0.40987079]), array([-0.0733347 , -0.32691093,  0.40987079]))
>>> all((c[i] == np.cross(a[i], b[i])).all() for i in range(len(c)))
True
+5
source

All Articles