Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Compute the condition number of a matrix in linear algebra in Python
The condition number of a matrix measures how sensitive the solution of a linear system is to changes in the input. A low condition number indicates a well-conditioned matrix, while a high condition number suggests an ill-conditioned matrix. In Python, we use numpy.linalg.cond() to compute this value.
Syntax
numpy.linalg.cond(x, p=None)
Parameters
x: The matrix whose condition number is sought.
p: Order of the norm used in computation (None, 1, -1, 2, -2, 'fro').
Basic Example
Let's compute the condition number of a 3x3 matrix ?
import numpy as np
from numpy import linalg as LA
# Create a matrix
matrix = np.array([[1, 1, 0],
[1, 0, 1],
[1, 0, 0]])
print("Matrix:")
print(matrix)
# Compute condition number
cond_num = LA.cond(matrix)
print(f"\nCondition Number: {cond_num}")
Matrix: [[1 1 0] [1 0 1] [1 0 0]] Condition Number: 3.7320508075688776
Using Different Norms
The condition number can be calculated using different norms ?
import numpy as np
from numpy import linalg as LA
matrix = np.array([[2, 1],
[1, 2]])
print("Matrix:")
print(matrix)
# Different norm types
norms = [None, 1, -1, 2, 'fro']
for norm in norms:
cond_num = LA.cond(matrix, p=norm)
print(f"Condition number (p={norm}): {cond_num:.4f}")
Matrix: [[2 1] [1 2]] Condition number (p=None): 3.0000 Condition number (p=1): 3.0000 Condition number (p=-1): 3.0000 Condition number (p=2): 3.0000 Condition number (p=fro): 3.0000
Well-conditioned vs Ill-conditioned Matrices
Let's compare a well-conditioned matrix with an ill-conditioned one ?
import numpy as np
from numpy import linalg as LA
# Well-conditioned matrix (identity matrix)
well_conditioned = np.eye(3)
print("Well-conditioned matrix:")
print(well_conditioned)
print(f"Condition number: {LA.cond(well_conditioned):.4f}")
# Ill-conditioned matrix
ill_conditioned = np.array([[1, 1, 1],
[1, 1.0001, 1],
[1, 1, 1.0001]])
print("\nIll-conditioned matrix:")
print(ill_conditioned)
print(f"Condition number: {LA.cond(ill_conditioned):.4f}")
Well-conditioned matrix: [[1. 0. 0.] [0. 1. 0.] [0. 0. 1.]] Condition number: 1.0000 Ill-conditioned matrix: [[1. 1. 1. ] [1. 1.0001 1. ] [1. 1. 1.0001 ]] Condition number: 40001.0000
Key Points
| Condition Number | Matrix Type | Meaning |
|---|---|---|
| 1 | Well-conditioned | Perfect numerical stability |
| 1-100 | Well-conditioned | Good numerical stability |
| >1000 | Ill-conditioned | Poor numerical stability |
| ? | Singular | Matrix is not invertible |
Conclusion
The condition number is crucial for assessing numerical stability in linear algebra computations. Use numpy.linalg.cond() to evaluate matrix conditioning before solving linear systems or performing matrix inversions.
