AppDividend
Latest Code Tutorials

np.linalg.cond in Python: The Complete Guide

The numpy linalg cond() function computes the condition number of a matrix. The cond() function can return the condition number using one of seven different forms, depending on the value of p.

Numpy linalg cond()

The np.linalg.cond() function is used to find a condition number of the matrix. The linalg cond() function returns the condition number using one of the 7 norms, and the return value depends upon the given value  below:

  1. None: 2-norm, computed directly using the SVD
  2. ‘fro’: Frobenius norm
  3. Inf: max(sum(abs(x), axis=1))
  4. -inf: min(sum(abs(x+), axis=1))
  5. 1: max(sum(abs(x), axis=0))
  6. -1: min(sum(abs(x), axis=0))
  7. 2: 2-norm (largest sign value)
  8. -2: smallest singular value

Syntax

numpy.linalg.cond ( array, condition_value)

Parameters

The linalg cond() function takes two main arguments:

  1. array: The array whose condition number we have to find.
  2. condition_value: This value is one of the 8 values given above.

Return Value

The linalg cond() function returns the condition number ( float type ).

Programming Example

See the following code.

# Programming example of linalg.cond
from numpy import linalg as LA
import numpy as np
arr = np.array([[2, 0, 2], [3, 1, 0], [1, 0, 1]])
print("The array is:\n", arr)

# Finding the cond of the array
print(LA.cond(arr))
# When cond='fro'
print(LA.cond(arr, 'fro'))
# When cond=infinite
print(LA.cond(arr, np.inf))
print(LA.cond(arr, -np.inf))
# When cond = 1,2
print(LA.cond(arr, 1))
print(LA.cond(arr, 2))

Output

The array is:
 [[2 0 2]
 [3 1 0]
 [1 0 1]]
1.1866952516534238e+17
inf
inf
inf
inf
1.1866952516534238e+17

Explanation

In this program, we have first declared the numpy array of size 3×3, and we have printed it.

Then we have called linalg.cond(), which we have imported as LA from the numpy library. We can see that we got different outputs for different conditions.

This output depends on the given array and the given condition.

That’s it for this tutorial.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.