Finding the norm of a matrix using Python numpy library

The norm of a matrix is calculated by taking all the elements of the matrix into consideration and returning a positive real number. Several norms of matrix exist which include Frobenius norm, 1-norm, 2-norm, -1-norm, -2-norm, infinity norms and nuclear norm.

Linear Algebra Updated 6 Apr 2025 1 min read

Overview

  • The norm of a square matrix describes the magnitude of the matrix.
  • Matrix norm is a function that combines all the values of a matrix to form a single positive real number that satisfies the following conditions.
    • || A || >= 0 for any square matrix A
    • || A || >= 0 if and only if the matrix A = 0
    • || kA || = | k | || A || where k is a scalar
    • || A + B || <= || A || + || B ||
    • || AB || <= || A || || B ||
  • There are several matrix norms and each of them are calculated differently.
  • The parameter ord of the numpy.linalg.norm() function denotes which matrix norm needs to be calculated.

Types of matrix norms

  • Frobenius norm or Euclidean norm: Returns the square root of the sum of the squares of the

    all matrix elements. This is the default norm returned by the numpy.norm() function when ord = None.

  • The 1-norm: The 1-norm is calculated by taking the maximum of the sums of the matrix columns. Column sums are calculated using the absolute value of the column elements.
  • The -1-norm: The -1-norm is calculated by taking the minimum of the sums of the matrix columns. Column sums are calculated using the absolute value of the column elements.
  • The 2-norm: Conventionally refers to the Euclidean norm. However when ord = 2, numpy.linalg.norm() returns the maximum of the singular values of the diagonal matrix obtained through singular value decomposition.
  • The -2-norm: Returns the minimum of the singular values of the diagonal matrix obtained through singular value decomposition.
  • Nuclear norm: The Nuclear norm is provided by summing up the singular values obtained through singular value decomposition.

Example

python
# Example Python program that finds the various matrix norms of a given

# matrix using the function norm() of numpy.linalg module

            import numpy

            import math

# Create a 2x2 square matrix

            a1 = numpy.ndarray(shape = (2, 2),

                               dtype = numpy.int32)

a1[0][0] = -2

            a1[0][1] = -4

            a1[1][0] = 3

            a1[1][1] = 5

print("Square matrix:")

            print(a1)

# Frobenius norm / Euclidean norm

            normVal = numpy.linalg.norm(a1)

            print("Euclidean norm of the matrix:")

            print(normVal)

# When ord = 1

            normVal = numpy.linalg.norm(a1, ord = 1)

            print("1-norm of the matrix:")

            print(normVal)

# When ord = -1

            normVal = numpy.linalg.norm(a1, ord = -1)

            print("-1-norm of the matrix:")

            print(normVal)

# When ord = 2

            normVal = numpy.linalg.norm(a1, ord = -2)

            print("2-norm of the matrix:")

            print(normVal)

# When ord = -2

            normVal = numpy.linalg.norm(a1, ord = -2)

            print("-2-norm of the matrix:")

            print(normVal)

# When ord=inf

            normVal = numpy.linalg.norm(a1, ord=numpy.inf)

            print("Infinity norm of the matrix:")

            print(normVal)

# When ord=-inf

            normVal = numpy.linalg.norm(a1, ord=-numpy.inf)

            print("Negative infinity norm of the matrix:")

            print(normVal)

Output

Output
Square matrix:

            [[-2 -4]

             [ 3  5]]

            Euclidean norm of the matrix:

            7.3484692283495345

            1-norm of the matrix:

            9.0

            -1-norm of the matrix:

            5.0

            2-norm of the matrix:

            0.272352646999216

            -2-norm of the matrix:

            0.272352646999216

            Infinity norm of the matrix:

            8.0

            Negative infinity norm of the matrix:

            6.0