Skip to content

Instantly share code, notes, and snippets.

@ssghost
Last active January 12, 2025 06:45
Show Gist options
  • Save ssghost/5c7c5ba8db2cdf6a88066403d1438a63 to your computer and use it in GitHub Desktop.
Save ssghost/5c7c5ba8db2cdf6a88066403d1438a63 to your computer and use it in GitHub Desktop.
def gauss_elim(m: List[List[float]], eps: float = 1.0/(10**10)) -> bool:
(h, w) = (len(m), len(m[0]))
for i in range(h):
maxrow = i
for j in range(i+1, h):
if abs(m[j][i]) > abs(m[maxrow][i]):
maxrow = j
(m[i], m[maxrow]) = (m[maxrow], m[i])
if abs(m[i][i]) <= eps:
return False
for j in range(i+1, h):
c = m[j][i] / m[i][i]
for x in range(i, w):
m[j][x] -= m[i][x] * c
for i in range(h-1, 0-1, -1):
c = m[i][i]
for j in range(0,i):
for x in range(w-1, i-1, -1):
m[j][x] -= m[i][x] * m[j][i] / c
m[i][i] /= c
for x in range(h, w):
m[i][x] /= c
return True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment