-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrixModule.py
More file actions
271 lines (244 loc) · 9.88 KB
/
matrixModule.py
File metadata and controls
271 lines (244 loc) · 9.88 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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
import math
import numpy as np
from vectorModule import vector
def isclose(a, b, rel_tol=1e-09, abs_tol=0.0):
return abs(a-b) <= max(rel_tol * max(abs(a), abs(b)), abs_tol)
class matrix(object):
# SW : I wrote this code assuming that the columnList is an array of vectors
def __init__(self, columnList, dimension=None):
if dimension:
self.columnVectors = columnList
self.rowN = len(columnList)
self.colN = columnList[0].dimension
self.dimension = self.rowN * self.colN
self.determinant = -1
self.rank = -1
elif isinstance(columnList, matrix):
vecList = []
list = []
for i in range(columnList.rowN):
for j in range(columnList.colN):
list.append(columnList.columnVectors[i].get_element(j))
vecList.append(vector(list))
list = []
self.columnVectors = vecList
self.rowN = len(vecList)
self.colN = vecList[0].dimension
self.dimension = self.rowN * self.colN
else:
raise Exception(
"can not make matrix with non-matrix and non-array-of-vector object")
def createIdentity(self, size): # create (size) X (size) identity matrix
vecList = []
for i in range(size):
list = []
for j in range(size):
if i == j:
list.append(1)
else:
list.append(0)
vecList.append(vector(list))
p = matrix(vecList, size * size)
return p
# following index parameters used in functions starts from 0
def getDim(self):
return self.rowN * self.colN
def getMatrixElement(self, row, col):
return self.columnVectors[row].get_element(col)
def divideRow(self, indexRow, scalar):
self.columnVectors[indexRow] = self.columnVectors[indexRow] / scalar
def addRow(self, addingRow, subjectRow, scalar):
# subjectRow -> subjectRow + addingRow * scalar
self.columnVectors[subjectRow] = self.columnVectors[subjectRow] + \
self.columnVectors[addingRow] * scalar
def subRow(self, addingRow, subjectRow, scalar):
if isinstance(scalar, float) or isinstance(scalar, int):
self.addRow(addingRow, subjectRow, -scalar)
else:
raise Exception("the scalar is not a float")
def GE(self): # Return new Ref Matrix (Gauss Elimination
tmpMatrix = matrix(self)
for i in range(tmpMatrix.rowN):
pivRow = tmpMatrix.findRow(i)
if pivRow != i:
self.switchRow(i, pivRow)
pivot = tmpMatrix.columnVectors[i].get_element(i)
if isclose(pivot, 0):
return tmpMatrix
tmpMatrix.divideRow(i, pivot)
for j in range(i + 1, tmpMatrix.colN):
tmpMatrix.subRow(
i, j, tmpMatrix.columnVectors[j].get_element(i))
if i == (tmpMatrix.rowN - 1):
break
return tmpMatrix
def GEBS(self):
tmpMatrix = matrix(self)
for i in range(tmpMatrix.rowN):
pivRow = tmpMatrix.findRow(i)
if pivRow != i:
self.switchRow(i, pivRow)
pivot = tmpMatrix.columnVectors[i].get_element(i)
if isclose(pivot, 0):
return tmpMatrix
tmpMatrix.divideRow(i, pivot)
for j in range(i + 1, tmpMatrix.colN):
tmpMatrix.subRow(
i, j, tmpMatrix.columnVectors[j].get_element(i))
if i == (tmpMatrix.rowN - 1):
break
for i in range(tmpMatrix.rowN - 1, 0, -1):
for j in range(i - 1, -1, -1):
pivot = tmpMatrix.columnVectors[j].get_element(i)
tmpMatrix.subRow(i, j, pivot)
return tmpMatrix
def doGE(self, v): # Retur matrix v that does same ERO
if isinstance(v, matrix):
tmpMatrix = matrix(self)
newM = matrix(v)
for i in range(tmpMatrix.rowN):
pivRow = tmpMatrix.findRow(i)
if pivRow != i:
self.switchRow(i, pivRow)
v.switchRow(i, pivRow)
pivot = tmpMatrix.columnVectors[i].get_element(i)
if isclose(pivot, 0):
return newM
newM.divideRow(i, pivot)
tmpMatrix.divideRow(i, pivot)
for j in range(i + 1, tmpMatrix.colN):
newM.subRow(
i, j, tmpMatrix.columnVectors[j].get_element(i))
tmpMatrix.subRow(
i, j, tmpMatrix.columnVectors[j].get_element(i))
if i == (tmpMatrix.rowN - 1):
break
return newM
def doGEBS(self, v):
if isinstance(v, matrix):
tmpMatrix = matrix(self)
newM = matrix(v)
for i in range(tmpMatrix.rowN):
pivRow = tmpMatrix.findRow(i)
if pivRow != i:
self.switchRow(i, pivRow)
v.switchRow(i, pivRow)
pivot = tmpMatrix.columnVectors[i].get_element(i)
if isclose(pivot, 0):
return newM
newM.divideRow(i, pivot)
tmpMatrix.divideRow(i, pivot)
for j in range(i + 1, tmpMatrix.colN):
newM.subRow(
i, j, tmpMatrix.columnVectors[j].get_element(i))
tmpMatrix.subRow(
i, j, tmpMatrix.columnVectors[j].get_element(i))
if i == (tmpMatrix.rowN - 1):
break
for i in range(tmpMatrix.rowN - 1, 0, -1):
for j in range(i - 1, -1, -1):
pivot = tmpMatrix.columnVectors[j].get_element(i)
newM.subRow(i, j, pivot)
tmpMatrix.subRow(i, j, pivot)
return newM
def getInverseMatrix(self):
iM = self.createIdentity(self.rowN)
tmpMatrix = self.doGEBS(iM)
return tmpMatrix
def getInverseMatrixNP(self):
arr = self.toNumpyArray()
inv = np.linalg.inv(arr)
vecList=[]
for i in inv:
vecList.append(vector(i))
newMat = matrix(vecList, self.colN)
return newMat
def getRank(self):
tmpMatrix = self.doGEBS()
cnt = 0
for i in range(tmpMatrix.rowN):
if isclose(tmpMatrix.getMatrixElement(i, i), 1):
cnt = cnt + 1
self.rank = cnt
return cnt
def getDeterminant(self):
tmpMatrix = self.GEBS()
det = 1
for i in range(self.rowN):
det *= self.getMatrixElement(i, i)
self.determinant = det
return det
def getDeterminantNP(self):
arr = self.toNumpyArray()
det = np.linalg.det(arr)
return det
def spans(self, v):
# todo: finds column vectors of the matrix spans v(==v is an element of the column space)
# self * x = v (x, v = vector)
if isinstance(v, vector):
rrefMatrix = self.doGEBS(v)
return rrefMatrix
else:
raise Exception("matrix can not span non-vector object")
def __add__(self, other):
if isinstance(other, matrix):
newVecs = []
for i in self.colN:
if self.rolN != other.rolN or self.colN != other.colN:
newVecs.append(
self.columnVectors[i] + other.columnVectors[i])
return matrix(newVecs)
def __mul__(self, other):
# todo: implement scalar multiplication and matrix multiplication
if isinstance(other, int) or isinstance(other, float):
newMatirx = []
for i in self.rowN:
newRow = []
for j in self.colN:
newRow.append(self.columVectors[i][j]*other)
newMatirx.append(newRow)
elif isinstance(other, matrix):
if self.colN != other.rowN:
raise Exception(
"the column number of subject matrix and row number of multiplier matrix is not same")
newMatrix = []
for i in range(self.rowN):
newRow = []
for j in range(other.colN):
sum = 0
for k in range(self.colN):
sum += self.columnVectors[i].get_element(
k) * other.columnVectors[k].get_element(j)
newRow.append(sum)
newMatrix.append(vector(newRow, len(newRow)))
return matrix(newMatrix, 25)
def getLittleMatrix(self, startRow, endRow, startCol, endCol):
vecList = []
for i in range(startRow, endRow + 1):
list = []
for j in range(startCol, endCol + 1):
list.append(self.getMatrixElement(i, j))
vecList.append(vector(list, len(list)))
return matrix(vecList, (endRow - startRow + 1) * (endCol - startCol + 1))
def __str__(self):
msg = ""
msg += "Dimension : {} X {}\n".format(self.rowN, self.colN)
for i in range(self.rowN):
for j in range(self.colN):
msg += "{} ".format(self.columnVectors[i].get_element(j))
msg += "\n"
return msg
def findRow(self, column):
for i in range(self.rowN):
if not(isclose(self.getMatrixElement(i, column), 0)):
return i
def switchRow(self, row1, row2):
a = self.columnVectors[row1]
self.columnVectors[row1] = self.columnVectors[row2]
self.columnVectors[row2] = a
def toNumpyArray(self):
vecList = []
for i in range(self.rowN):
vecList.append(self.columnVectors[i].coorList)
arr = np.array(vecList)
return arr