Skip to content

Instantly share code, notes, and snippets.

@angellicacardozo
Last active July 6, 2024 22:39
Show Gist options
  • Save angellicacardozo/4b35e15aa21af890b4a8fedef9891401 to your computer and use it in GitHub Desktop.
Save angellicacardozo/4b35e15aa21af890b4a8fedef9891401 to your computer and use it in GitHub Desktop.
LU decomposition with Python
def LU(A):
n = len(A) # Give us total of lines
# (1) Extract the b vector
b = [0 for i in range(n)]
for i in range(0,n):
b[i]=A[i][n]
# (2) Fill L matrix and its diagonal with 1
L = [[0 for i in range(n)] for i in range(n)]
for i in range(0,n):
L[i][i] = 1
# (3) Fill U matrix
U = [[0 for i in range(0,n)] for i in range(n)]
for i in range(0,n):
for j in range(0,n):
U[i][j] = A[i][j]
n = len(U)
# (4) Find both U and L matrices
for i in range(0,n): # for i in [0,1,2,..,n]
# (4.1) Find the maximun value in a column in order to change lines
maxElem = abs(U[i][i])
maxRow = i
for k in range(i+1, n): # Interacting over the next line
if(abs(U[k][i]) > maxElem):
maxElem = abs(U[k][i]) # Next line on the diagonal
maxRow = k
# (4.2) Swap the rows pivoting the maxRow, i is the current row
for k in range(i, n): # Interacting column by column
tmp=U[maxRow][k]
U[maxRow][k]=U[i][k]
U[i][k]=tmp
# (4.3) Subtract lines
for k in range(i+1,n):
c = -U[k][i]/float(U[i][i])
L[k][i] = c # (4.4) Store the multiplier
for j in range(i, n):
U[k][j] += c*U[i][j] # Multiply with the pivot line and subtract
# (4.5) Make the rows bellow this one zero in the current column
for k in range(i+1, n):
U[k][i]=0
n = len(L)
# (5) Perform substitutioan Ly=b
y = [0 for i in range(n)]
for i in range(0,n,1):
y[i] = b[i]/float(L[i][i])
for k in range(0,i,1):
y[i] -= y[k]*L[i][k]
n = len(U)
# (6) Perform substitution Ux=y
x = [0 in range(n)]
for i in range(n-1,-1,-1):
x[i] = y[i]/float(U[i][i])
for k in range (i-1,-1,-1):
U[i] -= x[i]*U[i][k]
return x
@kstavratis
Copy link

I'm sorry, but I have to ask.
Won't there be an "index out of range" error in line 8? Matrix A has 0,1,....,n-1 and not n.

@paulomann
Copy link

I think that's because the last column is the vector b

@fatemenajafi135
Copy link

There is an error in line 64

@angellicacardozo
Copy link
Author

@kstavratis @paulomann @Violet135 I not working on it nowadays; it has been a long time since the last time I saw this piece of code. It was a piece for an individual report. Please, add more information like the input you guys are testing with and I can try to reproduce and fix it here.

@p10rahulm
Copy link

Please change line 62 from:
x = [0 in range(n)]
to
x = [0 for i in range(n)]

to fix the error

@angellicacardozo
Copy link
Author

It may solve those problems

Please change line 62 from:
x = [0 in range(n)]
to
x = [0 for i in range(n)]

to fix the error

@xx-zhang
Copy link

@angellicacardozo
can you give a example A as LU input . i have tried A=[[2,1,4],[1,4,6]] that run error with the line 64.
can you give me a advice, thank you.

@angellicacardozo
Copy link
Author

Yes, give me few minutes to fix the code.
Are you guys studying it ?
I didn't expect so many comments on this code. Are you in basic education? College ?

@angellicacardozo
can you give a example A as LU input . i have tried A=[[2,1,4],[1,4,6]] that run error with the line 64.
can you give me a advice, thank you.

@angellicacardozo
Copy link
Author

angellicacardozo commented Jul 19, 2020

@angellicacardozo
can you give a example A as LU input . i have tried A=[[2,1,4],[1,4,6]] that run error with the line 64.
can you give me a advice, thank you.

@xx-zhang please refer to https://github.com/angellicacardozo/linear-algebra-methods/blob/master/P02GAULU.py in order to try out a better version for this method implementation.

For anyone else who is using this gist to perform a homework I do recommend you to take a look at mikofski solution for example.

@kitsiosvas
Copy link

In line 55 "y[i] = b[i]/float(L[i][i])" , isn't the division redundant? I mean the matrix L is a lower triangular (nxn) matrix with 1s in the main diagonal right? So the element L[i][i] should always be 1, no?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment