28 Aralık 2011 Çarşamba

LU decomposition with Python

Python is an object-oriented programming language, and it's a good alternative to Matlab for scientific computing with numpy and matplotlib modules (very easy to install). Pyhton has some advanteges over Matlab for example indices start from zero, it's free and has clean syntax .We've already had the Matlab code for LU decomposition what about implementation for Python? Same fashion as in Matlab L and U matrices will kepted in A.

from numpy import *
 A=array([[4.,-2.,2.],[-2.,2.,-4.],[2.,-4.,11.]])
"""
mustafadeniz@itu.edu.tr
"""
def mylu(A):
    n=len(A)
    for k in range(0,n-1):
        for i in range(k+1,n):
            if A[i,k] != 0:
                A[i,k]=A[i,k]/A[k,k]
                A[i,k+1:n]=A[i,k+1:n]-A[i,k]*A[k,k+1:n]
    return A

Hiç yorum yok:

Yorum Gönder