-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patha4.py
More file actions
147 lines (126 loc) · 3.72 KB
/
a4.py
File metadata and controls
147 lines (126 loc) · 3.72 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
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
### PREYANSH RASTOGI {2017176}
### VAIBHAV SARDA {2017320}
### IP HW 4
def input_matrix():
'''
Takes input from the user in the following form:
first line ----> no. of rows (no. of sub-lists in a matrix)
next line ----> no. of columns (no. of elements in each list)
next line ----> space-separated elements of a particular row
'''
A=[]
m=int(input())
n=int(input())
for i in range (m):
x=[]
k=str(input())
z=k.split(' ')
for j in range (n):
x.append(int(z[j]))
A.append(x)
return A
def Row_Transformation(A,x,row1,row2):
'''
Performs row transformation on matrix A in the following manner:
row2 ----> row2 + x*(row1)
Preconditions:
● A is a matrix containing integers
● x is of type double
● The possible values for the arguments row1 and row2 are 0, ..., nrows - 1, where nrows in number of rows in A
'''
i=0
for c in A[row2]:
c+=x*A[row1][i]
A[row2][i]=c
i+=1
def swapRows(A,row1,row2):
'''
Swaps two rows of a given matrix.
It takes three arguments:
1) A ----> represents the matrix
2) Possible values for parameters row1 and row2 are 0,1,2,...,nrows-1
Here, nrows is number of rows in A.
'''
temp=A[row1]
A[row1]=A[row2]
A[row2]=temp
def swapColumns(A,col):
'''
Swaps a column (col) of matrix A with the last column of matrix A
'''
m=len(A)
n=len(A[0])
for i in range(m):
temp = A[i][col]
A[i][col] = A[i][n-1]
A[i][n-1] = temp
def transpose(A):
'''
Takes transpose of a matrix A and returns the transposed matrix
'''
m=len(A)
n=len(A[0])
B=[]
for i in range(n):
x=[]
for j in range(m):
a=A[j][i]
x.append(a)
B.append(x)
return B
def MatrixRank(A):
'''
Finds rank of matrix A.
Takes a nested list (representing a matrix) as an argument
and returns an integer representing the rank of A.
Precondition: A is a matrix containing integer elements.
'''
row=0
m=len(A)
n=len(A[0])
if n > m:
B = transpose(A)
A = B
m=len(A)
n=len(A[0])
rank=n
g=0
while row <= rank-1:
y= A[row][row]
if y != 0:
for i in range (m):
x=A[i][row]
if x != 0 and i!=row:
Row_Transformation(A,y-1,i,i)
Row_Transformation(A,-x,row,i)
else:
if row == m-1:
rank=rank-1
else:
flag=0
for i in range(row,m):
if A[i][row] != 0 and flag ==0 :
j = i
flag = 1
if flag == 1:
swapRows(A,j,row)
row=row-1
else:
temp=1
for i in range(m):
if A[i][row]!=A[i][n-1] :
temp=0
swapColumns(A,row)
if temp == 0:
row-=1
rank-=1
else:
g+=1
row+=1
rank=rank-g
return rank
if __name__=='__main__':
A=[]
A=input_matrix()
rank=MatrixRank(A)
print(rank)