Calculating the singular values of one or more matrices using NumPy

The singular values of one or more matrices can be calculated using the function svdvals() of linalg module from the NumPy library.

Linear Algebra Updated 10 Apr 2025 1 min read

Overview

  • The function linalg.svdvals() calculates the singular values of one or more matrices and returns the results as an ndarray.
  • The returned ndarray excludes the orthogonal matrices U, V obtained from the factorization the array A using Singular Value Decomposition.

Example

python
# Example Python program that finds the singular values of

            # multiple matrices in one go using the function linalg.svdvals()

            import numpy

# Create an upper triangular matrix

            elems     = numpy.arange(1, 18, 2)

            upperTriangular  = elems.reshape(3, 3)

upperTriangular[1][0] = 0

            upperTriangular[2][0] = 0

            upperTriangular[2][1] = 0

print("Matrix 1:")

            print(upperTriangular)

# Create a lower triangular matrix

            elems     = numpy.arange(2, 19, 2)

            lowerTriangular  = elems.reshape(3, 3)

lowerTriangular[0][1] = 0

            lowerTriangular[0][2] = 0

            lowerTriangular[1][2] = 0

print("Matrix 2:")

            print(lowerTriangular)

svdValues = numpy.linalg.svdvals([upperTriangular, lowerTriangular])

print("Singular values of matrix 1:")

            print(svdValues[0])

print("Singular values of matrix 2:")

            print(svdValues[1])

Output

Output
Matrix 1:

            [[ 1  3  5]

             [ 0  9 11]

             [ 0  0 17]]

            Matrix 2:

            [[ 2  0  0]

             [ 8 10  0]

             [14 16 18]]

            Singular values of matrix 1:

            [21.66412661  7.46801882  0.94568139]

            Singular values of matrix 2:

            [29.67443546  7.81125888  1.55309851]