-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathludec.py
More file actions
28 lines (22 loc) · 589 Bytes
/
ludec.py
File metadata and controls
28 lines (22 loc) · 589 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# Author: Alex Gezerlis
# Numerical Methods in Physics with Python (CUP, 2020)
from triang import forsub, backsub, testcreate, testsolve
import numpy as np
def ludec(A):
n = A.shape[0]
U = np.copy(A)
L = np.identity(n)
for j in range(n-1):
for i in range(j+1,n):
coeff = U[i,j]/U[j,j]
U[i,j:] -= coeff*U[j,j:]
L[i,j] = coeff
return L, U
def lusolve(A,bs):
L, U = ludec(A)
ys = forsub(L,bs)
xs = backsub(U,ys)
return xs
if __name__ == '__main__':
A, bs = testcreate(4,21)
testsolve(lusolve,A,bs)