import numpy as np def forward_elimination(A, b, n): """ Calculates the forward part of Gaussian elimination. """ for row in range(0, n-1): for i in range(row+1, n): factor = A[i,row] / A[row,row] for j in range(row, n): A[i,j] = A[i,j] - factor * A[row,j] b[i] = b[i] - factor * b[row] print('A = \n%s and b = %s' % (A,b)) return A, b def back_substitution(a, b, n): """" Does back substitution, returns the Gauss result. """ x = np.zeros((n,1)) x[n-1] = b[n-1] / a[n-1, n-1] for row in range(n-2, -1, -1): sums = b[row] for j in range(row+1, n): sums = sums - a[row,j] * x[j] x[row] = sums / a[row,row] return x def gauss(A, b): """ This function performs Gauss elimination without pivoting. """ n = A.shape[0] # Check for zero diagonal elements if any(np.diag(A)==0): raise ZeroDivisionError(('Division by zero will occur; ' 'pivoting currently not supported')) A, b = forward_elimination(A, b, n) return back_substitution(A, b, n) # Main program starts here if __name__ == '__main__': A = np.array([[1, 1, 1, 1], [1, -2, -2, -2], [1, 4, -4, 1], [1, -5, -5, -3]]) b = np.array([0, 4, 2, -4]) x = gauss(A, b) print('Gauss result is x = \n %s' % x)