2

[python 2.7 and numpy v1.11.1] I am looking at matrix condition numbers and am trying to compute the condition number for a matrix without using the function np.linalg.cond().

Based on numpy's documentation, the definition of a matrix's condition number is, "the norm of x times the norm of the inverse of x."

||X|| * ||X^-1||

for the matrix

a = np.matrix([[1, 1, 1],
               [2, 2, 1],
               [3, 3, 0]])

print np.linalg.cond(a)

1.84814479698e+16

print np.linalg.norm(a) * np.linalg.norm(np.linalg.inv(a))

2.027453660713377e+17

Where is the mistake in my computation?

Thanks!

jeffalltogether
  • 145
  • 1
  • 1
  • 8

3 Answers3

7

norm uses the Frobenius norm for matrix by default,when cond uses 2-norm:

In [347]: np.linalg.cond(a)
Out[347]: 38.198730775206172

In [348]:np.linalg.norm(a,2)*np.linalg.norm(np.linalg.inv(a),2)
Out[348]: 38.198730775206243

In [349]: np.linalg.norm(a)*np.linalg.norm(np.linalg.inv(a))
Out[349]: 39.29814570824248
B. M.
  • 18,243
  • 2
  • 35
  • 54
7

You are trying to compute the condition using the Frobenius Norm definition. That is an optional parameter to the condition computation.

print(np.linalg.norm(a)*np.linalg.norm(np.linalg.inv(a)))
print(np.linalg.cond(a, p='fro'))

Produces

2.02745366071e+17
2.02745366071e+17
drublackberry
  • 238
  • 1
  • 7
1

NumPy cond() is currently buggy. There will come a time when we will fix it but for now if you are doing this for linear equation solutions you can use SciPy linalg.solve which will either produce an error for exact singularity or a warning if reciprocal condition number is below threshold and nothing if the array is invertible.

percusse
  • 3,006
  • 1
  • 14
  • 28