Tutorial by Examples

Numpy provides a cross function for computing vector cross products. The cross product of vectors [1, 0, 0] and [0, 1, 0] is [0, 0, 1]. Numpy tells us: >>> a = np.array([1, 0, 0]) >>> b = np.array([0, 1, 0]) >>> np.cross(a, b) array([0, 0, 1]) as expected. While cr...
Either input can be an array of 3- (or 2-) element vectors. >>> a=np.array([[1,0,0],[0,1,0],[0,0,1]]) >>> b=np.array([1,0,0]) >>> np.cross(a,b) array([[ 0, 0, 0], [ 0, 0, -1], [ 0, 1, 0]]) The result in this case is array([np.cross(a[0],b), np.c...
In our last two examples, numpy assumed that a[0,:] was the first vector, a[1,:] the second, and a[2,:] the third. Numpy.cross has an optional argument axisa that allows us to specify which axis defines the vectors. So, >>> a=np.array([[1,1,1],[0,1,0],[1,0,-1]]) >>> b=np.array([0...

Page 1 of 1