-
Notifications
You must be signed in to change notification settings - Fork 0
/
P1_Echelon.py
99 lines (77 loc) · 1.85 KB
/
P1_Echelon.py
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import numpy as np
pivot_counter = 0
inconsistense = False
def echln(A,i,j):
m,n = A.shape
for x in range(i,m):
if A[x,j] != 0 :
if x >i :
A[[x, i]] = A[[i, x]]
break
else :
return A , 1
A[i] = (A[i] / A[i,j] )
A[i+1:] += -1 * ( A[i] * (A[i+1:,j:j+1]))
for w in range(i,m):
for z in range(j,n):
if abs(A[w,z]) < 0.0001 :
A[w,z] = 0
return A , 0
def reduced_echln(A,i):
n,m = A.shape
for j in range(0,n):
if A[i,j] != 0:
break
else:
if A[i,-1] != 0:
global inconsistense
inconsistense = True
return A
global pivot_counter
pivot_counter +=1
A[i] = A[i] / A[i,j]
A[:i] -= A[i] * A[:i,j:j+1]
return A
n=int(input("Enter the coefficient matrix size : "))
print("\n enter coefficient matrix: \n")
A=np.zeros((n,n))
for i in range(n):
A[i] =input().split()
print("\n enter constant terms vector in a row \n")
b = np.zeros((1,n))
b[0] = input().split()
b = b.reshape((n,1))
A= np.hstack((A, b))
print(A)
print()
print("".center(40,"8"))
print("echelon step by step : ")
print()
for t in range(0,n):
A,x = echln(A,t,t)
k = t
while(x == 1 and k<n):
k+=1
A,x = echln(A,t,k)
print(A)
print("====================================")
print()
print("".center(40,"8"))
print()
print("reduced_echelon step by step : ")
for i in range(n-1,-1,-1):
A = reduced_echln(A,i)
print(A)
print("=====================================")
print()
print("".center(40,"8"))
print()
if inconsistense :
print(" matrix is inconsistent")
elif pivot_counter < n :
print(" matrix has infinte answer")
else :
print("answer : ")
b = A[:,-1]
b = b.reshape((n,1))
print(b)