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

Gary Ruben gruben at bigpond.net.au
Thu Feb 16 01:00:04 CST 2006


Hi Travis,
Have you tested this? It appears to give the wrong answer on my system.
I expect to get from this

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]]])

the solution

Out[24]:
array([[[ 2],
         [-1],
         [ 0]],

        [[ 5],
         [-4],
         [ 0]],

        [[ 8],
         [-7],
         [ 0]]])

i.e. the same as my example but with column vectors instead of rows, but 
doing cross(a,b,axisa=0,axisb=0,axisc=0) gives

Out[15]:
array([[[ 0],
         [ 0],
         [-3]],

        [[ 0],
         [ 0],
         [ 6]],

        [[ 0],
         [ 0],
         [-3]]])

Gary R.

Travis Oliphant wrote:
> 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:
>>  
>>
> 
> 
> Help on function cross in module numpy.core.numeric:
> 
> cross(a, b, axisa=-1, axisb=-1, axisc=-1)
>    Return the cross product of two (arrays of) vectors.
> 
>    The cross product is performed over the last axis of a and b by default,
>    and can handle axes with dimensions 2 and 3. For a dimension of 2,
>    the z-component of the equivalent three-dimensional cross product is
>    returned.
> 
> It's the axisa, axisb, and axisc  that you are interested in.
> 
> The default is to assume you have Nx3 arrays and return an Nx3 array.  
> But you can change the axis used to find vectors.
> 
> cross(A,B,axisa=0,axisb=0,axisc=0)
> 
> will do what you want.  I suppose, a single axis= argument might be 
> useful as well for the common situation of having all the other axis 
> arguments be the same.
> 
> -Travis





More information about the Numpy-discussion mailing list