Step 1: Implement your own gaussian elimination algorithm
without numpy
• A matrix is a list of list
• Implement function Augmented_Matrix(M)
• M is the matrix
• Return the augmented matrix of M
• Implement function Inverse(M)
• M is the matrix
• Return the inverse of matrix M
I. Without numpy
# Defines a function named gaussian_elimination that takes a matrix A
as a list of lists as an argument
def gaussian_elimination(A):
# Determine the dimension of the matrix
n = len(A)
# This loop iterates through each row of the matrix, starting with
the first (index 0) and ending with the last (index n - 1)
for i in range(n):
diag_element = A[i][i] # Retrieves the value of the diagonal
element in the current row i.
A[i] = [elem / diag_element for elem in A[i]] # Divides each
element of the current row i by the diagonal element diag_element to
produce 1 on the diagonal.
# This nested loop iterates through the matrix rows below the
current row i.
for j in range(i + 1, n):
multiplier = A[j][i] # Retrieves the value of the element in
the current row j and column i that is below the diagonal element in
the current row.
A[j] = [A[j][k] - multiplier * A[i][k] for k in
range(len(A[i]))]
# Performs the operation of “subtracting a multiple” of row i from row
j.
# multiplier is the factor by which row i is multiplied.
# This loop goes through each element in row j (index k) and subtracts
multiplier * the corresponding element in row i.
# This step is intended to zero out the elements below the diagonal
element in each row.
for i in range(n - 1, -1, -1):
# This loop iterates through the rows in reverse order,
starting with the very last row (index n - 1) and ending with the first
(index 0).
for j in range(i - 1, -1, -1):
# This nested loop iterates through the rows above the current row i.
multiplier = A[j][i]
# Retrieves the value of the element in the current row j and column i
that is above the diagonal element in the current row.
A[j] = [A[j][k] - multiplier * A[i][k] for k in
range(len(A[i]))]
# Performs the "subtract multiple" operation on row i from row j.
# This step is designed to zero out the elements above the diagonal
element in each row.
return A # Returns the transformed matrix A.
# Using the code on this matrix
A = [[5, 3, 1, 1, 0, 0],
[3, 9, 4, 0, 1, 0],
[1, 3, 5, 0, 0, 1]]
result = gaussian_elimination(A)
for row in result:
print(row)
Code without comments:
def gaussian_elimination(A):
n = len(A)
for i in range(n):
diag_element = A[i][i]
A[i] = [elem / diag_element for elem in A[i]]
for j in range(i + 1, n):
multiplier = A[j][i]
A[j] = [A[j][k] - multiplier * A[i][k] for k in
range(len(A[i]))]
for i in range(n - 1, -1, -1):
for j in range(i - 1, -1, -1):
multiplier = A[j][i]
A[j] = [A[j][k] - multiplier * A[i][k] for k in
range(len(A[i]))]
return A
A = [[5, 3, 1, 1, 0, 0],
[3, 9, 4, 0, 1, 0],
[1, 3, 5, 0, 0, 1]]
result = gaussian_elimination(A)
for row in result:
print(row)
II. With numpy
import numpy as np
def gaussian_elimination(A):
# Matrix dimension
n = len(A)
for i in range(n):
# We reduce the diagonal element to 1
A[i] = A[i] / A[i][i]
# Zero out the elements below the current leading element
for j in range(i + 1, n):
A[j] = A[j] - A[j][i] * A[i]
for i in range(n - 1, -1, -1):
# Zero out the elements above the current leading element
for j in range(i - 1, -1, -1):
A[j] = A[j] - A[j][i] * A[i]
return A
# Example of use
A = [Link]([[5, 3, 1, 1, 0, 0],
[3, 9, 4, 0, 1, 0],
[1, 3, 5, 0, 0, 1]], dtype=float)
result = gaussian_elimination(A)
print(result)
import [Link] as sm #Use the library for statistical modeling
import [Link] as plt #The library is used to plot graphs.
import pandas as pd #Used to work with data in the form of tables
# Data from the table
depth = [5000, 5200, 6000, 6538, 7109, 7556, 8005, 8207, 8210, 8600,
9026, 9197, 9926, 10813, 13800, 14311]
cost = [2596.8, 3328.0, 3181.1, 3198.4, 4779.9, 5905.6, 5769.2, 8089.5,
4813.1, 5618.7, 7736.0, 6788.3, 7840.8, 8882.5, 10489.5, 12506.6]
# Create a Pandas DataFrame for easier manipulation
data = [Link]({'Depth': depth, 'Cost': cost})
# Fit a linear regression model
model = [Link]('Cost ~ Depth', data=data)
results = [Link]()
# Print the model summary
print([Link]())
# Get the regression coefficients
intercept = [Link][0]
slope = [Link][1]
# Predict the cost for a new depth (example: 6590 meters)
new_depth = 6590
predicted_cost = intercept + slope * new_depth
print(f"Predicted cost for depth {new_depth}: {predicted_cost}")
# Plot the data and the regression line
[Link](data['Depth'], data['Cost'])
[Link](data['Depth'], [Link], color='red')
[Link]('Depth')
[Link]('Cost')
[Link]('Linear Regression of Oil Well Drilling Costs')
[Link]()