Perform LU decomposition of a square matrix. Decompose matrix A into a lower triangular matrix L and an upper triangular matrix U such that A = L * U (or PA = LU with partial pivoting).
from typing import List, Tuple
def lu_decomposition(
matrix: List[List[float]]
) -> Tuple[List[List[float]], List[List[float]]]:
n = len(matrix)
L = [[0.0] * n for _ in range(n)]
U = [row[:] for row in matrix]
for i in range(n):
L[i][i] = 1.0
for col in range(n):
# Partial pivoting
max_row = col
for row in range(col + 1, n):
if abs(U[row][col]) > abs(U[max_row][col]):
max_row = row
if max_row != col:
U[col], U[max_row] = U[max_row], U[col]
# Swap L entries for columns already processed
for k in range(col):
L[col][k], L[max_row][k] = L[max_row][k], L[col][k]
if abs(U[col][col]) < 1e-12:
continue
for row in range(col + 1, n):
factor = U[row][col] / U[col][col]
L[row][col] = factor
for j in range(col, n):
U[row][j] -= factor * U[col][j]
return L, U