We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Why did NumPy's cross product not give me a vector?
Good question imaginary person asking this question. The cross product of 2 vectors produces another vector (which are what NumPy calls "1d arrays"). So you might reasonably wonder, "Why did the example only produce a scalar (ie, just one number)?"
The reason the example gives the result as just "-2" is that when two vectors lie on the x-y plane, then their cross product vector is orthogonal (basically perpendicular) to the x-y plane.
What does that mean in plane English?
That the x and y components/values become 0. It's easier to see with an animation, so here's a link to the gif on the wiki page for cross products (can't seem to add it, so go to the wiki page for cross products).
If you give NumPy only a 2d vector (ie, a 1d NumPy array that has only 2 rows), it still treats it as a 3d vector (ie, a 1d NumPy array that has 3 rows where the 3rd value is assumed to be 0). The calculation of the cross product itself also shows this, so I've included it for those who are interested.
For some beautiful animations by 3blue1brown, check out a great explanation called "Cross products | Chapter 10, Essence of linear algebra"
The step-by-step calculation of the cross product
Note that you can think of a 3d vector (ie, a 1d NumPy array with 3 rows) as having values for the coordinates x, y, and z. Symbolically,
[ x, y, z].
If any of the following steps seem mysterious, then the section titled "Matrix notation" in the cross product wiki article might help:
Dot and Cross
You are viewing a single comment's thread. Return to all comments →
Why did NumPy's cross product not give me a vector?
Good question imaginary person asking this question. The cross product of 2 vectors produces another vector (which are what NumPy calls "1d arrays"). So you might reasonably wonder, "Why did the example only produce a scalar (ie, just one number)?"
The reason the example gives the result as just "-2" is that when two vectors lie on the x-y plane, then their cross product vector is orthogonal (basically perpendicular) to the x-y plane.
What does that mean in plane English?
That the x and y components/values become 0. It's easier to see with an animation, so here's a link to the gif on the wiki page for cross products (can't seem to add it, so go to the wiki page for cross products).
If you give NumPy only a 2d vector (ie, a 1d NumPy array that has only 2 rows), it still treats it as a 3d vector (ie, a 1d NumPy array that has 3 rows where the 3rd value is assumed to be 0). The calculation of the cross product itself also shows this, so I've included it for those who are interested.
For some beautiful animations by 3blue1brown, check out a great explanation called "Cross products | Chapter 10, Essence of linear algebra"
The step-by-step calculation of the cross product
Note that you can think of a 3d vector (ie, a 1d NumPy array with 3 rows) as having values for the coordinates x, y, and z. Symbolically,
[ x, y, z].
If any of the following steps seem mysterious, then the section titled "Matrix notation" in the cross product wiki article might help:
[ 1, 2, 0] x [ 3, 4, 0 ]
= determinant( [ [ 2, 0 ], [ 4, 0 ] ] ) i
- determinant( [ [ 1, 0 ], [ 3, 0 ] ] ) j
+ determinant( [ [ 1, 2 ], [ 3, 4 ] ] ) k
So this becomes
= (2 · 0 - 0 · 4) i
- (1 · 0 - 0 · 3) j
+ (1 · 3 - 2 · 4) k
Which simplifies to
= 0 i
- 0 j
+ (-2) k