Table of Contents
ToggleA matrix norm is a function that assigns a non-negative number to a matrix. It provides a measure of the size or magnitude of a matrix.
In general, matrix norms are used to quantify how large or small a matrix is, and they play an important role in problems involving matrix equations, such as in solving systems of linear equations or performing matrix factorizations.
There are several types of matrix norms, but the most commonly used ones are −
The Frobenius norm is one of the simplest and most commonly used matrix norms. It is defined as the square root of the sum of the absolute squares of the matrix elements. Mathematically, it is given by −
‖A‖F = √(Σi=1 Σj=1 |aij|²)
Where A is the matrix, and aij are the elements of the matrix. The Frobenius norm is equivalent to the L2 norm of the matrix treated as a vector.
The 1-norm (also called the maximum column sum norm) of a matrix is defined as the maximum absolute column sum. Mathematically, it is given by −
‖A‖1 = maxj Σi=1 |aij|
In simple terms, the 1-norm is the maximum sum of the absolute values of the elements in any column of the matrix.
The infinity norm (also called the maximum row sum norm) of a matrix is defined as the maximum absolute row sum. Mathematically, it is given by −
‖A‖∞ = maxi Σj=1 |aij|
The infinity norm gives the maximum sum of absolute values of elements in any row of the matrix.
The 2-norm (also called the spectral norm) of a matrix is defined as the largest singular value of the matrix. It measures the largest stretch factor of the matrix when applied to a vector. The 2-norm is given by −
‖A‖2 = σmax(A)
Where, σmax(A) is the largest singular value of matrix A. In this case, the 2-norm is related to the matrix’s singular values, which can be computed using singular value decomposition (SVD).
NumPy provides functions for calculating various matrix norms. The numpy.linalg.norm() function can be used to compute most of the common matrix norms. Let us explore how to use this function for different types of matrix norms.
To compute the Frobenius norm using NumPy, we use the numpy.linalg.norm() function with the parameter ord='fro'.
In the following example, the Frobenius norm of the matrix A is calculated by taking the square root of the sum of the squares of all elements in the matrix −
# Open Compiler
import numpy as np
# Define a matrix A
A = np.array([[1, 2], [3, 4]])
# Compute the Frobenius norm of the matrix
frobenius_norm = np.linalg.norm(A, 'fro')
print("Frobenius norm of A:", frobenius_norm)
Output:
Following is the output obtained −
Frobenius norm of A: 5.477225575051661
To compute the 1-norm, we use the numpy.linalg.norm() function with the parameter ord=1. The 1-norm of the matrix is the maximum sum of the absolute values of the elements in any column of the matrix.
In this case, the column sums are 4 and 6, so the 1-norm is 6 −
# Open Compiler
import numpy as np
# Define a matrix A
A = np.array([[1, 2], [3, 4]])
# Compute the 1-norm of the matrix
one_norm = np.linalg.norm(A, 1)
print("1-norm of A:", one_norm)
Output:
Following is the output obtained −
1-norm of A: 6.0
To compute the infinity norm, we use the numpy.linalg.norm() function with the parameter ord=np.inf. The infinity norm of the matrix is the maximum sum of the absolute values of the elements in any row of the matrix.
In this case, the row sums are 3 and 7, so the infinity norm is 7 −
# Open Compiler
import numpy as np
# Define a matrix A
A = np.array([[1, 2], [3, 4]])
# Compute the infinity norm of the matrix
infinity_norm = np.linalg.norm(A, np.inf)
print("Infinity norm of A:", infinity_norm)
Output:
Following is the output obtained −
Infinity norm of A: 7.0
To compute the 2-norm (spectral norm), we use the numpy.linalg.norm() function with the parameter ord=2. The 2-norm (spectral norm) of the matrix is the largest singular value of the matrix, which measures the largest stretch factor of the matrix when applied to a vector.
Following is an example to compute the 2-norm in NumPy −
# Open Compiler
import numpy as np
# Define a matrix A
A = np.array([[1, 2], [3, 4]])
# Compute the 2-norm of the matrix
two_norm = np.linalg.norm(A, 2)
print("2-norm (spectral norm) of A:", two_norm)
Output:
Following is the output obtained −
2-norm (spectral norm) of A: 5.464985704219043
Matrix norms have many practical applications in numerical analysis, machine learning, optimization, and more −
Key Takeaway: Master matrix norms with NumPy at Vista Academy!
