-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrix.py
More file actions
90 lines (73 loc) · 3.45 KB
/
Matrix.py
File metadata and controls
90 lines (73 loc) · 3.45 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
'''
@author William Ray Johnson
9/4/17
'''
from fractions import gcd
class Matrix:
def __init__(self, rows, cols, values):
self.rows = rows
self.cols = cols
self.buildMatrix(values)
def __eq__(self, other):
return self.getMatrixValue() == other.getMatrixValue()
def buildMatrix(self, values):
self.matrix = []
for row in range(self.rows):
self.matrix.append(values[:self.cols])
values = values[self.cols:]
def getMatrixValue(self):
return self.matrix
def getSize(self):
return str(self.rows) + "x" + str(self.cols)
def setMatrixValue(self, matrix):
self.matrix = matrix
def multiply(self, matrix):
matrixA = self.matrix
matrixB = matrix.getMatrixValue()
returnMatrix = Matrix(len(matrixA),
len(matrixB[0]), ['-'] * (len(matrixA) * len(matrixB[0])))
matrixC = returnMatrix.getMatrixValue()
for row in range(len(matrixA)):
for col in range(len(matrixB[0])):
matrixC[row][col] = 0
for mid in range(len(matrixA[0])):
matrixC[row][col] = matrixC[row][col] + matrixA[row][mid] * matrixB[mid][col]
returnMatrix.setMatrixValue(matrixC)
return returnMatrix
def calcAdjugateMatrix(self):
if self.getSize() == '2x2':
adjugateMatrix = Matrix(2,2,[self.matrix[1][1], -self.matrix[0][1],
-self.matrix[1][0], self.matrix[0][0]])
elif self.getSize() == '3x3':
A = (self.matrix[1][1] * self.matrix[2][2] -
self.matrix[1][2] * self.matrix[2][1])
B = -(self.matrix[1][0] * self.matrix[2][2] -
self.matrix[1][2] * self.matrix[2][0])
C = (self.matrix[1][0] * self.matrix[2][1] -
self.matrix[1][1] * self.matrix[2][0])
D = -(self.matrix[0][1] * self.matrix[2][2] -
self.matrix[0][2] * self.matrix[2][1])
E = (self.matrix[0][0] * self.matrix[2][2] -
self.matrix[0][2] * self.matrix[2][0])
F = -(self.matrix[0][0] * self.matrix[2][1] -
self.matrix[0][1] * self.matrix[2][0])
G = (self.matrix[0][1] * self.matrix[1][2] -
self.matrix[0][2] * self.matrix[1][1])
H = -(self.matrix[0][0] * self.matrix[1][2] -
self.matrix[0][2] * self.matrix[1][0])
I = (self.matrix[0][0] * self.matrix[1][1] -
self.matrix[0][1] * self.matrix[1][0])
adjugateMatrix = Matrix(3,3,[A,D,G,B,E,H,C,F,I])
return adjugateMatrix
def calcDeterminant(self):
if self.getSize() == '2x2':
determinant = self.matrix[0][0] * self.matrix[1][1] - self.matrix[0][1] * self.matrix[1][0]
elif self.getSize() == '3x3':
A = (self.matrix[1][1] * self.matrix[2][2] -
self.matrix[1][2] * self.matrix[2][1])
B = -(self.matrix[1][0] * self.matrix[2][2] -
self.matrix[1][2] * self.matrix[2][0])
C = (self.matrix[1][0] * self.matrix[2][1] -
self.matrix[1][1] * self.matrix[2][0])
determinant = self.matrix[0][0] * A + self.matrix[0][1] * B + self.matrix[0][2] * C
return determinant**-1