In mathematics, a vector is a quantity that cannot be represented only by a single unit. It has to be divided into 2 components, namely magnitude and direction. Geometrically, a vector can be represented by a line segment with an arrow attached at its end. the arrow points at the direction of the vector.
Vectors can be implemented in python in the form of arrays. The arrays can be assigned like normal arrays or as numpy arrays. Numpy arrays can be n-dimensional.
Some properties of vectors
- When a vector is transmitted parallel to itself, it remains unchanged.
- Parallel vectors have the same direction.
- Vectors are said to be equal when they have the same magnitude and the direction.
- Vectors are said to be negative when they are equal in magnitude but opposite in direction.
In vector algebra we can calculate the angle between two vectors using a simple formula. The dot product of the two vectors can be used to determine the cosine of the angle between the two vectors which will ultimately give us our angle.
Let the two vectors be ‘u‘ and ‘v‘ and the angle between them be ‘A’ . The formula is given below:
The numerator represents the dot product of the two vectors whereas the denominator is the multiplication of the magnitudes of the two vectors.
Also read: Mocking in Python Using Unittest.mock
Using python to determine the angle between two vectors
We can use Python to compute the angle between two vectors using user-defined functions or by using functions from the numerical python( Numpy) library. The numpy library contains functions such as linalg.norm() and arccos() that makes calculations easier. to know more about numpy, click here.
Implementing python functions for determining angles
We will be using the math library first and implement the function. The code for the program is as follows:
#importing required math module import math #calculating the magnitude of the vectors def mag(u, N): # Stores the final magnitude magnitude = 0 # Traverse the array for i in range(N): magnitude += u[i] * u[i] # Return the square root of magnitude return math.sqrt(magnitude) # Function to find the dot product of the vectors def dotProd(u, v, N): # Stores the dot product prod = 0 # Traverse the array for i in range(N): prod = prod + u[i] * v[i] # Return the product return prod def angleVector(u, v, N): # Stores the dot product of vectors dotProductOfVectors = dotProd(u, v, N) # magnitude of vector u magOfu = mag(u, N) #magnitude of vector v magOfv = mag(v, N) # angle between given vectors angle = (dotProductOfVectors / (magOfu * magOfv)) #display the angle print('%.5f'%angle) #pre defined vectors u = [-0.5, -2, 1] v = [-1, -1, 0.3] # Size of the vectors N = len(u) #display the angle print("The angle between the two vectors:") #function call to calculate the angle between the vectors angleVector(u, v, N)
The output of the above code will be the following:
The angle between the two vectors: 0.84529
Using numpy to calculate the angle between two n-dimensional vectors
Now let’s look at how we can easily calculate the angle using numpy.
#importing the required modules from numpy import arccos, array from numpy.linalg import norm import math #function for calculating the angle using numpy def angles(u, v): #using the arccos function from numpy return arccos(u.dot(v)/(norm(u)*norm(v))) #defining the vectors u = array([3, -4, 5]) v = array([2, 7, -3]) #function call to compute the angle c= angles(u,v) #the function returns the angle in radians #converting the angle to degrees from radians angle= math.degrees(c) #displaying the result print("the vectors are=",u,"and",v) print("the angle between the two vectors is=",angle)
The output of the above code will look like something as follows:
the vectors are= [ 3 -4 5] and [ 2 7 -3] the angle between the two vectors is= 131.647015792716
This tutorial explores the various ways in which one can calculate the angle between two n dimensional vectors in python. The two programs shown in this article, demonstrates the usage of the math and the numpy libraries for the same. Using the dot product of the two given vectors and by calculating their individual magnitudes, the cosine of the angle can be determined and is used to define the relationship between them.