Let’s see the program to compute the cross product of two given vectors using NumPy. For finding the cross product of two given vectors we are using numpy.cross() function of NumPy library.
Syntax: numpy.cross(a, b, axisa=-1, axisb=-1, axisc=-1, axis=None)[
Return: cross product of two (arrays of) vectors.
Let’s see the examples:
Example 1: Cross product of 1d-arrays.
Python3
# import library import numpy as np # declare vectors x = [ 1 , 2 ] y = [ 3 , 4 ] # find cross product of # two given vectors result = np.cross(x, y) # show the result print (result) |
Output:
-2
Example 2: Cross product of 2d-arrays.
Python3
# import library import numpy as np # declare vectors x = [[ 1 , 2 ], [ 3 , 4 ]] y = [[ 5 , 6 ], [ 7 , 8 ]] # find cross product of # two given vectors result = np.cross(x, y) # show the result print (result) |
Output:
[-4 -4]
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.