[Numpy-discussion] cross product of two 3xn arrays

Gary Ruben gruben at bigpond.net.au
Wed Feb 15 18:04:02 CST 2006


This *almost* does what you want, I think. I can't see a neat way to 
give column vectors in the solution:

In [21]: a=array([[[1],[2],[3]],[[4],[5],[6]],[[7],[8],[9]]])

In [22]: b=array([[[1],[2],[4]],[[4],[5],[7]],[[7],[8],[10]]])

In [24]: cross(a.transpose(),b.transpose())
Out[24]:
array([[[ 2, -1,  0],
         [ 5, -4,  0],
         [ 8, -7,  0]]])

Gary R.

Ian Harrison wrote:
> Hello,
> 
> I have two groups of 3x1 arrays that are arranged into two larger 3xn
> arrays. Each of the 3x1 sub-arrays represents a vector in 3D space. In
> Matlab, I'd use the function cross() to calculate the cross product of
> the corresponding 'vectors' from each array. In other words:
> 
> if ai and bj are 3x1 column vectors:
> 
> A = [ a1  a2  a3 ]
> 
> B = [ b1  b2  b3 ]
> 
> C = A x B = [ (a1 x b1)  (a2 x b2)  (a3 x b3) ]
> 
> Could someone suggest a clean way to do this? I suppose I could write
> a for loop to cycle through each pair of vectors and send them to
> numpy's cross(), but since I'm new to python/scipy/numpy, I'm guessing
> that there's probably a better method that I'm overlooking.
> 
> Thanks,
> Ian





More information about the Numpy-discussion mailing list