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
LU etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster
LU etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster
28 Aralık 2011 Çarşamba
20 Haziran 2011 Pazartesi
LU Decomposition
ONE SQUARE MATRIX = TWO TRIANGULAR MATRIX
A=LU
The factors are L and U triangular matrices. U is the upper triangular matrix with the pivots on diagonal. Eliminitaion takes A to U, reversing this step back U to A takes, L. L is lower triangular matrix and has all 1's its diagonal. Let's factorize a random matrix without row exchanges;
>>A=round(10*rand(n,n)) % creates n by n matrix (n=arbitrary)
%%%%%%%%%%%%%%%%%%%%
% Author: Mustafa DENIZ %
% Contact: mustafdeniz@itu.edu.tr %
%%%%%%%%%%%%%%%%%%%%%
[n,n]=size(A);
tolerance=1.e-8;
for k = 1:n
if abs(A(k, k)) < tolerance
end
L(k, k) = 1;
for i = k+1:n
L(i,k) = A(i, k) / A(k, k); %Multipliers for column k are put into L
for j = k+1:n %Elimination beyond row k and column k.
A(i, j) = A(i, j) - L(i, k)*A(k, j);
end
end
for j = k:n
U(k, j) = A(k, j);
end
end
Matlab has a command also for LU factorization;
>> [U,L]=lu(A)
References:
Strang. G., 2009. Introduction to Linear Algebra, Fourth Edition, Wellesley Cambridge Press.
William H.P. et al, 2007. Numerical Recipes 3rd Edition: The Art of Scientific Computing, Cambridge University Press.
A=LU
The factors are L and U triangular matrices. U is the upper triangular matrix with the pivots on diagonal. Eliminitaion takes A to U, reversing this step back U to A takes, L. L is lower triangular matrix and has all 1's its diagonal. Let's factorize a random matrix without row exchanges;
>>A=round(10*rand(n,n)) % creates n by n matrix (n=arbitrary)
%%%%%%%%%%%%%%%%%%%%
% Author: Mustafa DENIZ %
% Contact: mustafdeniz@itu.edu.tr %
%%%%%%%%%%%%%%%%%%%%%
[n,n]=size(A);
tolerance=1.e-8;
for k = 1:n
if abs(A(k, k)) < tolerance
end
L(k, k) = 1;
for i = k+1:n
L(i,k) = A(i, k) / A(k, k); %Multipliers for column k are put into L
for j = k+1:n %Elimination beyond row k and column k.
A(i, j) = A(i, j) - L(i, k)*A(k, j);
end
end
for j = k:n
U(k, j) = A(k, j);
end
end
Matlab has a command also for LU factorization;
>> [U,L]=lu(A)
References:
Strang. G., 2009. Introduction to Linear Algebra, Fourth Edition, Wellesley Cambridge Press.
William H.P. et al, 2007. Numerical Recipes 3rd Edition: The Art of Scientific Computing, Cambridge University Press.
Kaydol:
Kayıtlar (Atom)