How to get the magnitude of a vector in NumPy? - GeeksforGeeks

29 Aug, 2020
The fundamental feature of linear algebra are vectors, these are the objects having both direction and magnitude. In Python, NumPy arrays can be used to depict a vector.
There are mainly two ways of getting the magnitude of vector:
By defining an explicit function which computes the magnitude of a given vector based on the below mathematical formula:if V is vector such that, V = (a, b, c)
then ||V|| = ?(a*a + b*b + c*c)
Here are some programs which computes the magnitude of a vector following the above approach:# program to compute magnitude of a vector  # importing required librariesimport numpyimport math  # function defination to compute magnitude o f the vectordef magnitude(vector):     return math.sqrt(sum(pow(element, 2) for element in vector))  # displaying the original vectorv = numpy.array([0, 1, 2, 3, 4])print('Vector:', v)  # computing and displaying the magnitude of the vectorprint('Magnitude of the Vector:', magnitude(v))Output:Vector: [0 1 2 3 4]
Magnitude of the Vector: 5.477225575051661
Below is another example with the same approach:# program to compute magnitude of a vector  # importing required librariesimport numpyimport math  # function defination to compute magnitude o f the vectordef magnitude(vector):     return math.sqrt(sum(pow(element, 2) for element in vector))  # computing and displaying the magnitude of the vectorprint('Magnitude of the Vector:', magnitude(numpy.array([1, 2, 3])))Output:Magnitude of the Vector: 3.7416573867739413

if V is vector such that, V = (a, b, c)
then ||V|| = ?(a*a + b*b + c*c)

Here are some programs which computes the magnitude of a vector following the above approach:
# program to compute magnitude of a vector  # importing required librariesimport numpyimport math  # function defination to compute magnitude o f the vectordef magnitude(vector):     return math.sqrt(sum(pow(element, 2) for element in vector))  # displaying the original vectorv = numpy.array([0, 1, 2, 3, 4])print('Vector:', v)  # computing and displaying the magnitude of the vectorprint('Magnitude of the Vector:', magnitude(v))
Output:
Vector: [0 1 2 3 4]
Magnitude of the Vector: 5.477225575051661

Below is another example with the same approach:
# program to compute magnitude of a vector  # importing required librariesimport numpyimport math  # function defination to compute magnitude o f the vectordef magnitude(vector):     return math.sqrt(sum(pow(element, 2) for element in vector))  # computing and displaying the magnitude of the vectorprint('Magnitude of the Vector:', magnitude(numpy.array([1, 2, 3])))
Output:
Magnitude of the Vector: 3.7416573867739413

By using the norm() method in linalg module of NumPy library. The Linear Algebra module of NumPy offers various methods to apply linear algebra on any NumPy array. Below are some programs which use numpy.linalg.norm() to compute the magnitude of a vector:# program to compute magnitude of a vector  # importing required librariesimport numpy  # displaying the original vectorv = numpy.array([1, 2, 3])print('Vector:', v)  # computing and displaying the magnitude of# the vector using norm() methodprint('Magnitude of the Vector:', numpy.linalg.norm(v))Output:Vector: [1 2 3]
Magnitude of the Vector: 3.7416573867739413
An additional argument ord can be used to compute the nth order of the norm() of a vector.# program to compute the nth order of the # magnitude of a vector  # importing required librariesimport numpy  # displaying the original vectorv = numpy.array([0, 1, 2, 3, 4])print('Vector:', v)  # computing and displaying the magnitude of the vectorprint('Magnitude of the Vector:', numpy.linalg.norm(v))  # Computing the nth order of the magnitude of vectorprint('ord is 0: ', numpy.linalg.norm(v, ord = 0))print('ord is 1: ', numpy.linalg.norm(v, ord = 1))print('ord is 2: ', numpy.linalg.norm(v, ord = 2))print('ord is 3: ', numpy.linalg.norm(v, ord = 3))print('ord is 4: ', numpy.linalg.norm(v, ord = 4))Output:Vector: [0 1 2 3 4]
Magnitude of the Vector: 5.477225575051661
ord is 0:  4.0
ord is 1:  10.0
ord is 2:  5.477225575051661
ord is 3:  4.641588833612778
ord is 4:  4.337613136533361

# program to compute magnitude of a vector  # importing required librariesimport numpy  # displaying the original vectorv = numpy.array([1, 2, 3])print('Vector:', v)  # computing and displaying the magnitude of# the vector using norm() methodprint('Magnitude of the Vector:', numpy.linalg.norm(v))
Output:
Vector: [1 2 3]
Magnitude of the Vector: 3.7416573867739413

An additional argument ord can be used to compute the nth order of the norm() of a vector.
# program to compute the nth order of the # magnitude of a vector  # importing required librariesimport numpy  # displaying the original vectorv = numpy.array([0, 1, 2, 3, 4])print('Vector:', v)  # computing and displaying the magnitude of the vectorprint('Magnitude of the Vector:', numpy.linalg.norm(v))  # Computing the nth order of the magnitude of vectorprint('ord is 0: ', numpy.linalg.norm(v, ord = 0))print('ord is 1: ', numpy.linalg.norm(v, ord = 1))print('ord is 2: ', numpy.linalg.norm(v, ord = 2))print('ord is 3: ', numpy.linalg.norm(v, ord = 3))print('ord is 4: ', numpy.linalg.norm(v, ord = 4))
Output:
Vector: [0 1 2 3 4]
Magnitude of the Vector: 5.477225575051661
ord is 0:  4.0
ord is 1:  10.0
ord is 2:  5.477225575051661
ord is 3:  4.641588833612778
ord is 4:  4.337613136533361

Python numpy-Linear Algebra
Python-numpy
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Python Dictionary
Read a file line by line in Python
Enumerate() in Python
How to Install PIP on Windows ?
Iterate over a list in Python
Different ways to create Pandas Dataframe
Python String | replace()
Python program to convert a list to string
Reading and Writing to text files in Python
sum() function in Python