Python program for Choleski decomposition

''' L = choleski(a)
    Choleski decomposition: [L][L]transpose = [a]
    x = choleskiSol(L,b)
    Solution phase of Choleski's decomposition method
from numpy import dot
from math import sqrt
import error
def choleski(a):
    n = len(a)
    for k in range(n):
            a[k,k] = sqrt(a[k,k] - dot(a[k,0:k],a[k,0:k]))
        except ValueError:
            error.err('Matrix is not positive definite')
        for i in range(k+1,n):
            a[i,k] = (a[i,k] - dot(a[i,0:k],a[k,0:k]))/a[k,k]
    for k in range(1,n): a[0:k,k] = 0.0
    return a
def choleskiSol(L,b):
    n = len(b)
  # Solution of [L]{y} = {b}  
    for k in range(n):
        b[k] = (b[k] - dot(L[k,0:k],b[0:k]))/L[k,k]
  # Solution of [L_transpose]{x} = {y}      
    for k in range(n-1,-1,-1):
        b[k] = (b[k] - dot(L[k+1:n,k],b[k+1:n]))/L[k,k]
    return b

Enjoyed this post? Share it!


Leave a comment

Your email address will not be published.