def is_uniq(l):
    a=set()
    for i in l:
        a.add(i)
    b= list(a)
    if len(l)==len(b):
        return True
    else:
        return False       
def is_diag(X):
    l=X.eigenvalues()
    if is_uniq(l):
        return True
    else:
        return False
def diagonalize(X):
    if is_diag(X):
        a= X.eigenvectors_right()
        l=[]
        for i in a:
            l.append(list(i[1][0]))
        S=transpose(matrix(l))
        D=(S^(-1))*X*S
        return D,S
    else:
        print "not diag"   
A1=matrix([[1,1],[1,1]])
diagonalize(A1)
out:(
[2 0]  [ 1  1]
[0 0], [ 1 -1]
)
#Therefore A1=S*D*S^(-1)
#Where S=matrix([[1,1],[1,-1]]) & D=matrix([[2,0],[0,0]])

A2=matrix([[2,1],[0,0]])
diagonalize(A2)
out:(
[2 0]  [ 1  1]
[0 0], [ 0 -2]
)
#Therefore A2=S1*D1*S1^(-1)
#Where S1=matrix([[1,1],[1,-2]]) & D1=matrix([[2,0],[0,0]])

Strang-250-1-U (last edited 2010-12-17 05:15:20 by rupampy)