-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2.1_CP1.py
More file actions
31 lines (23 loc) · 774 Bytes
/
Copy path2.1_CP1.py
File metadata and controls
31 lines (23 loc) · 774 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# Create a program for "naive" Gaussian elimination.
import numpy as np
def main(A):
# Shape of the matrix in question (3, 4) in our question.
(n, m) = np.shape(A)
# Perform gaussian elimination - algorithm borrowed from Jordan Barry
for j in range(n - 1):
for i in range(j + 1, n):
A[i, :] = -(A[i, j] / A[j, j]) * A[j, :] + A[i, :]
return A
# Backsubstitution
def backsub(A):
(n, m) = np.shape(A)
x = np.zeros(m - 1)
for i in np.arange(n - 1, -1, -1):
x[i] = (A[i, -1] - A[i, 0:m - 1] @ x) / A[i, i]
return x
if __name__ == '__main__':
A = np.array([[2, -2, -1, -2], [4, 1, -2, 1], [-2, 1, -1, -3]])
print("Gausselim:")
print(main(A))
print("Backsubstitution:")
print(backsub(A))