Overview:
-
Vector Norm is a measure of the length of a vector or the magnitude of the vector.
-
Different types of vector norms measure the vector in different ways, but all of them give a number to convey the magnitude of the vector using a function.
-
So, the norm of a vector is a function that assigns non-negative real numbers to vectors which satisfy the following properties:
-
||x|| > 0 when x ^= 0 and ||X|| = 0 when x = 0
-
||k x|| = ||k|| ||x||
-
||x+y|| <= ||x|| ||y||
-
Types of Vector Norms:
Euclidean norm: Sum of the squares of the components of the vector.
||x||2 = (Σxi)1/2, where i = 1 to n
Infinity norm: The maximum absolute value of the components of the vector.
||x||∞ = max|xi|, where i = 1 to n
1-norm: The sum of the absolute value of the components of the vector.
||x||1 = (Σ|xi|), where i = 1 to n
Calculating the norm of a vector using NumPy:
-
The NumPy function vector_norm() from the linalg module computes the vector norm for one are more vectors.
-
The type of norm can be specified using the ord parameter of the vector_norm() function.
Example:
# Example Python program that computes the vector norms of the # Create a square matrix # Since no axis is specified all the elements # Vector norms for three column vectors are computed # Vector norms for three row vectors are computed |
Output:
Input vector(s): [[ 1 3 5] [ 7 9 11] [13 15 17]] Vector norm (Each element is a component of the vector): 31.12876483254676 Vector norms of the column vectors: [14.79864859 17.74823935 20.85665361] Vector norms of the row vectors: [ 5.91607978 15.84297952 26.13426869] |