def gauss_elimination(A, B):
n = len(B)
# Applying Gauss elimination
for i in range(n):
# Partial pivoting
max_index = i
for j in range(i+1, n):
if abs(A[j][i]) > abs(A[max_index][i]):
max_index = j
A[i], A[max_index] = A[max_index], A[i]
B[i], B[max_index] = B[max_index], B[i]
# Forward elimination
for j in range(i+1, n):
factor = A[j][i] / A[i][i]
for k in range(i, n):
A[j][k] -= factor * A[i][k]
B[j] -= factor * B[i]
# Back substitution
X = [0] * n
for i in range(n - 1, -1, -1):
X[i] = B[i] / A[i][i]
for j in range(i):
B[j] -= A[j][i] * X[i]
return X
Example equations: ax + by + cz + dw = e
Replace the coefficients and constants with your actual values
A = [
[2, 1, -1, 3], # Coefficients of x
[1, 1, 1, 2], # Coefficients of y
[1, -1, 2, 1], # Coefficients of z
[3, -2, 1, 4] # Coefficients of w
]
B = [4, 7, 1, 12] # Constants on the right-hand side of the equations
Solve the system of equations
solution = gauss_elimination(A, B)
print("Solution:", solution