Skip to content

Instantly share code, notes, and snippets.

@ahrzb
Created November 6, 2015 08:33
Show Gist options
  • Save ahrzb/ec3666dbfd70c2d48944 to your computer and use it in GitHub Desktop.
Save ahrzb/ec3666dbfd70c2d48944 to your computer and use it in GitHub Desktop.
import numpy as np
import math
r = np.random.randint(0, 30, (3, 3)).astype('d')
def spectral_radius(m):
return max(abs(y) for y in np.linalg.eigvals(m))
def matrix_norm2(m):
return math.sqrt(spectral_radius(m.dot(m.T)))
def matrix_sensitivity(m):
return matrix_norm2(m) * matrix_norm2(np.linalg.inv(m))
d = (1 / np.diag(r)) * np.eye(3)
print "Matrix"
print r
print "Condition Number:", matrix_sensitivity(r)
print
print "D"
print d
print
print "Well Conditioned"
print d.dot(r)
print "Condition Number:", matrix_sensitivity(d.dot(r))
print
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment