forked from kangwonlee/nmisp_py
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgauss_jordan.py
More file actions
50 lines (36 loc) · 1.23 KB
/
Copy pathgauss_jordan.py
File metadata and controls
50 lines (36 loc) · 1.23 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import math
import numpy as np
def inv(A:np.ndarray, b_verbose:bool=True) -> np.ndarray:
return elimination(
np.array(
np.hstack(
[A, np.identity(A.shape[0])]
)
)*1.0,
b_verbose=b_verbose,
)
def elimination(AX:np.ndarray, b_verbose:bool=True) -> np.ndarray:
# pivot loop
for p in range(AX.shape[0]):
if b_verbose:
print(f"Row {p+1} is now the Pivot Row.")
one_over_pivot = 1.0 / AX[p, p]
if not math.isclose(1, one_over_pivot):
if b_verbose:
print(f"Normalize Row {p+1} with {one_over_pivot}.")
# normalize the pivot row
for j in range(AX.shape[1]):
AX[p, j] *= one_over_pivot
if b_verbose:
print(AX)
# row loop
for i in range(AX.shape[0]):
if i != p and (not math.isclose(AX[i, p], 0)):
# row operation
multiplier = - AX[i, p]
if b_verbose:
print(f"Row {i+1} += ({multiplier}) x Row {p+1}")
AX[i, :] += multiplier * AX[p, :]
if b_verbose:
print(AX)
return AX[:, AX.shape[0]:]