-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_lib.py
More file actions
244 lines (199 loc) · 8.17 KB
/
main_lib.py
File metadata and controls
244 lines (199 loc) · 8.17 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#function that'll determine if matrix is adequate
def rowCheck(arr):
entryCount = len(arr[0].split())
for row in arr:
if len(row.split()) != entryCount:
return False
return True
# object that forms a matrix from an array of row vectors
class Matrix:
#constructor
def __init__(self,arr):
if rowCheck(arr):
self.arr = []
for row in arr:
self.arr.append(row.split())
for row in range(0, len(self.arr)):
for entry in range(0, len(self.arr[row])):
self.arr[row][entry] = float(self.arr[row][entry])
else:
self.arr = []
#accessors:
def getEntry(self, row, column):
if row > 0 and column > 0:
return self.arr[row-1][column-1]
def getRowDim(self):
return len(self.arr[0])
def getColDim(self):
return len(self.arr)
def getRowVect(self, rowNum):
if rowNum > 0:
return self.arr[rowNum-1]
def getColVect(self, colNum):
if colNum > 0:
vectList = []
for row in self.arr:
vectList.append(row[colNum-1])
return vectList
#mutators:
def replace(self, row, col, entry): #replaces an entry in the matrix
self.arr[row-1][col-1] = float(entry)
def swap(self,rowA,rowB): #elem row opp: row swapping
self.arr[rowA-1], self.arr[rowB-1] = self.arr[rowB-1], self.arr[rowA-1]
def mult(self,row,coeff): #elem row opp: multiplying row by a non zero constant
if coeff != 0:
for entry in range(0,len(self.arr[row-1])):
self.arr[row-1][entry] = coeff * self.arr[row-1][entry]
def addNTimes(self, rowA, rowB, coeff): #elem row opp: adding a multiple of one row to another
if coeff != 0:
for entry in range(0, len(self.arr[rowA-1])):
self.arr[rowA-1][entry] = self.arr[rowA-1][entry] + coeff * self.arr[rowB-1][entry]
def printMtrx(*mtrx): #prints out the matrix
for mat in list(mtrx):
for time in range(1,mat.getColDim()+1):
print(mat.getRowVect(time))
print('\n')
def genNullMtrx(rowDim, colDim):
rowStr = ''
matList = []
for elem in range(0,rowDim):
rowStr += '0 '
for elem in range(0,colDim):
matList.append(rowStr)
return Matrix(matList)
#MATRIX ARITHMETIC
def rowMatch(*mats): #verifies that rows have the same length
dim = list(mats)[0].getRowDim()
for mat in list(mats):
if mat.getRowDim() != dim:
return False
return True
def colMatch(*mats): #verifies that columns have the length
dim = list(mats)[0].getColDim()
for mat in list(mats):
if len(mat.getColVect(1)) != dim:
return False
return True
def vectSum(*vect):
vector = []
for elem in range(0, len(list(vect)[0])):
sum = 0
for indVect in list(vect):
sum += indVect[elem]
vector.append(sum)
return vector
def mtrxAdd(*mats):
if rowMatch(*mats) and colMatch(*mats):
mtrxRslt = genNullMtrx(list(mats)[0].getRowDim(), list(mats)[0].getColDim())
for mtrxNum in range(0,len(list(mats))):
for rowNum in range(0,mtrxRslt.getColDim()):
mtrxRslt.arr[rowNum] = vectSum(mtrxRslt.arr[rowNum], list(mats)[mtrxNum].arr[rowNum])
return mtrxRslt
def scalarMult(matrix, coeff):
for row in range(1, matrix.getColDim()+1):
matrix.mult(row, coeff)
print(matrix.arr)
return matrix
def transpose(matrix):
vectList = []
strList = []
for column in range(1,matrix.getRowDim()+1):
vectList.append(matrix.getColVect(column))
for vect in vectList:
vectStr = ''
for entry in vect:
vectStr += (str(entry) + ' ')
strList.append(vectStr)
return Matrix(strList)
def matMult(mtrxA, mtrxB): #Will only work with two matrices and will serve as part of a function that can take an infinite amount of matrices
# #FIX MEEEE!!!! Problem: Some column vectors are 0
if mtrxA.getRowDim() == mtrxB.getColDim(): #condition for matrix multiplication to work
matRslt = genNullMtrx(mtrxA.getColDim(), mtrxB.getRowDim()) #generates an appropriate sized matrix full of zeros
for rowNum in range(1, matRslt.getColDim()+1):
for colNum in range(1, matRslt.getRowDim()+1):
entry = 0
for entryNum in range(1,mtrxA.getRowDim()+1):
entry += (mtrxA.getEntry(rowNum,entryNum) * mtrxB.getEntry(entryNum, colNum)) #slight mistale around here
matRslt.replace(rowNum, colNum, entry)
return matRslt
def trace(mtrx):
if mtrx.getRowDim() == mtrx.getColDim():
trace = 0
for i in range(1,mtrx.getRowDim()+1):
trace += mtrx.getEntry(i,i)
return trace
def chngPivot(i=1,j=1, i_new = 0, j_new = 0): return i + i_new, j + j_new
def entryCheck(mtrx,i,j): return mtrx.getEntry(i,j) == 0 #checks if pivot is 0 or not
def elimPrtcl(mtrx,i,j): #what to do when pivot isn't 0
rowList = list(range(1,mtrx.getColDim()+1)) #creates list of row numbers
rowList.__delitem__(i-1) #delete row number of where pivot is
mtrx.mult(i,1/mtrx.getEntry(i,j)) #multiply row of pivot by it's inverse to make pivot 1
for rowNum in rowList:
mtrx.addNTimes(rowNum,i, - (mtrx.getEntry(rowNum,j)/mtrx.getEntry(i,j)))
def swapPrtcl(mtrx,i,j): #what to do when pivot is 0
rowList = list(range(1, mtrx.getColDim() + 1))
rowList.__delitem__(i - 1)
for rowNum in rowList:
if mtrx.getEntry(rowNum,j-1) != 1:
mtrx.swap(i, rowNum)
break
def oneCheck(vect): #checks if a vector has a one in it
for entry in vect:
if entry == 1:
return True
return False
def soleOneCheck(vect): #checks if a vector only has a one and zeros
onesList = []
zerosList = []
for entry in vect:
if entry == 1: onesList.append(True)
elif entry == 0: zerosList.append(True)
return len(onesList) == 1 and len(zerosList) == len(vect) - 1
def fullZeroCheck(vect): #checks if a vector is all zeros
for entry in vect:
if entry != 0:
return False
return True
def leadingOneCheck(vect): #checks if a row vector has a leading one
for entry in vect:
if entry == 1:
return True
elif entry != 0 and entry != 1:
return False
def rref_check(*mtrx): #check if a matrix is in rref
boolList = [] #List of bools representing whether matrices are in rref or not
for mtr in list(mtrx):
printMtrx(mtr)
condList = []
print('Verifying columns')
for col in transpose(mtr).arr: #Determines in which columns the leading ones are found
condList.append(((oneCheck(col) and soleOneCheck(col)) or fullZeroCheck(col)) )
print('condList: ', condList)
print('condList length:', len(condList))
print('Column dimension:', mtr.getColDim())
if len(condList) > mtr.getColDim(): #Eliminates any data from columns we needn't worry about
print('Eliminating useless data')
condList.sort()
for instance in range(0,len(condList)- mtr.getColDim()):
condList.pop(0)
print('condList; ', condList)
print('Verifying rows')
for row in mtr.arr: #Determines whether every row has leading one or is full of zeroes
condList.append(leadingOneCheck(row) or fullZeroCheck(row))
print('condList: ', condList)
condList.sort()
boolList.append(condList[0])
return boolList
def rref(mtrx): #Not done
i,j = 1,1
while rref_check(mtrx) == [False]:
print("While condition cleared")
if entryCheck(mtrx,i,j):
print("Initiating Swap Protocol")
swapPrtcl(mtrx,i,j)
else:
print("Initiating Elimination Protocol")
elimPrtcl(mtrx,i,j)
i,j = i+1,j+1
printMtrx(mtrx)
return mtrx