I need to implement a LU decomposition and then compare it to the np.linalg.solve
function from numpy.
The function in the code (see below) runs without any problems, but when I use it to solve a matrix I keep getting an error:
IndexError: list index out of range
on the line:
L[i][j] = (A2[i][j] - s2) / U[j][j]
Here is the whole code:
def matrixMul(A, B):
TB = zip(*B)
return [[sum(ea*eb for ea,eb in zip(a,b)) for b in TB] for a in A]
def pivotize(m):
#Creates the pivoting matrix for m.
n = len(m)
ID = [[float(i == j) for i in range(n)] for j in range(n)]
for j in range(n):
row = max(range(j, n), key=lambda i: abs(m[i][j]))
if j != row:
ID[j], ID[row] = ID[row], ID[j]
return ID
def lu(A):
#Decomposes a nxn matrix A by PA=LU and returns L, U and P.
n = len(A)
L = [[0.0] * n for i in range(n)]
U = [[0.0] * n for i in range(n)]
P = pivotize(A)
A2 = matrixMul(P, A)
for j in range(n):
L[j][j] = 1.0
for i in range(j+1):
s1 = sum(U[k][j] * L[i][k] for k in range(i))
U[i][j] = A2[i][j] - s1
for i in range(j, n):
s2 = sum(U[k][j] * L[i][k] for k in range(j))
L[i][j] = (A2[i][j] - s2) / U[j][j]
return (L)
A = np.array([[1,1,3],[5,3,1],[2,3,1]])
b = np.array([2,3,-1])
print('LU factorization: ', lu(A))
A = np.array([[1,1,3],[5,3,1],[2,3,1]])
b = np.array([2,3,-1])
print('Internal solver : ', np.linalg.solve(A,b))
Any ideas? Thanks!
Your matrixMul
method is not correct. Try this:
matrixMul([[1, 0], [0, 1]], [[1, 0], [0, 1]])
This is multiplying two identity matrices, and should return [[1, 0], [0, 1]]
. When I run it, it returns [[1, 0], []]
. This means that A2
, inside your lu
has only a single row, the rest are empty. Thus the index error when i == 1
and j == 0
.
This reason for the failure is that TB
is a zip
object. Those can only be iterated over a single time, consuming the iterator. I don't actually think you need the TB
object at all, just iterate over elements of B
.
def matrixMul(A, B):
return [[sum(ea*eb for ea,eb in zip(a,b)) for b in B] for a in A]
This returns the desired output:
>>> matrixMul([[1, 0], [0, 1]], [[1, 0], [0, 1]])
>>> [[1, 0], [0, 1]]
EDIT:
By the way, there are still other problems with your solution. Yours and NumPy's versions still don't match. But the solution here will fix your index error.