Skip to content

Instantly share code, notes, and snippets.

@tlkahn
Last active October 15, 2024 02:20
Show Gist options
  • Save tlkahn/fd8d7f67f94b68a4e90bcda910c6b0ff to your computer and use it in GitHub Desktop.
Save tlkahn/fd8d7f67f94b68a4e90bcda910c6b0ff to your computer and use it in GitHub Desktop.
import numpy as np
def gaussian_elimination(A, b):
"""
Gaussian elimination is a method for solving systems of linear equations. It involves transforming a matrix into row echelon form through a series of elementary row operations. Key points:
1. Used to solve linear systems: $Ax = b$
2. Steps:
- Convert to augmented matrix [A|b]
- Transform to row echelon form
- Back-substitute to find solutions
3. Complexity: $O(n^3)$ for an $n \times n$ matrix
"""
n = len(b)
for i in range(n):
A[i] = np.append(A[i], b[i])
for i in range(n):
A[i] = A[i] / A[i][i]
for j in range(i + 1, n):
A[j] = A[j] - A[i] * A[j][i]
x = np.zeros(n)
for i in range(n - 1, -1, -1):
x[i] = A[i][-1] - np.dot(A[i][i:-1], x[i:])
return x
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment