-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatriz.py
148 lines (129 loc) · 7.14 KB
/
matriz.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
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
class Matriz:
def __init__(self, data):
self.data = data
def __add__(self, other):
if isinstance(other, Matriz):
if len(self.data) == len(other.data) and len(self.data[0]) == len(other.data[0]):
result = [[self.data[i][j] + other.data[i][j] for j in range(len(self.data[0]))] for i in range(len(self.data))]
return Matriz(result)
else:
raise ValueError("As dimensões das matrizes não são compatíveis para a adição.")
else:
raise TypeError("A adição é suportada apenas entre objetos Matriz.")
def __sub__(self, other):
if isinstance(other, Matriz):
if len(self.data) == len(other.data) and len(self.data[0]) == len(other.data[0]):
result = [[self.data[i][j] - other.data[i][j] for j in range(len(self.data[0]))] for i in range(len(self.data))]
return Matriz(result)
else:
raise ValueError("As dimensões das matrizes não são compatíveis para a subtração.")
else:
raise TypeError("A subtração é suportada apenas entre objetos Matriz.")
def __mul__(self, other):
if isinstance(other, (int, float)):
result = [[self.data[i][j] * other for j in range(len(self.data[0]))] for i in range(len(self.data))]
return Matriz(result)
elif isinstance(other, Matriz):
if len(self.data[0]) == len(other.data):
result = [[sum(self.data[i][k] * other.data[k][j] for k in range(len(self.data[0]))) for j in range(len(other.data[0]))] for i in range(len(self.data))]
return Matriz(result)
else:
raise ValueError("As dimensões das matrizes não são compatíveis para a multiplicação.")
else:
raise TypeError("A multiplicação é suportada apenas entre objetos Matriz e escalares.")
def transpose(self):
result = [[self.data[j][i] for j in range(len(self.data))] for i in range(len(self.data[0]))]
return Matriz(result)
def traco(self):
if len(self.data) == len(self.data[0]):
return sum(self.data[i][i] for i in range(len(self.data)))
else:
raise ValueError("O traço só pode ser calculado para matrizes quadradas.")
class MatrizQuadrada(Matriz):
def determinante(self):
if len(self.data) == len(self.data[0]):
if len(self.data) == 1:
return self.data[0][0]
elif len(self.data) == 2:
return self.data[0][0] * self.data[1][1] - self.data[0][1] * self.data[1][0]
else:
determinant = 0
for j in range(len(self.data[0])):
cofactor = self.data[0][j] * self.cofator(0, j)
determinant += cofactor
return determinant
else:
raise ValueError("O determinante só pode ser calculado para matrizes quadradas.")
def cofator(self, i, j):
submatrix = [[self.data[x][y] for y in range(len(self.data[0])) if y != j] for x in range(1, len(self.data))]
submatrix = MatrizQuadrada(submatrix)
return ((-1) ** (i + j)) * submatrix.determinante()
class MatrizDiagonal(Matriz):
def __add__(self, other):
if isinstance(other, MatrizDiagonal) and len(self.data) == len(other.data) and len(self.data[0]) == len(other.data[0]):
result = [[self.data[i][j] + other.data[i][j] if i == j else self.data[i][j] for j in range(len(self.data[0]))] for i in range(len(self.data))]
return MatrizDiagonal(result)
else:
return super().__add__(other)
def __sub__(self, other):
if isinstance(other, MatrizDiagonal) and len(self.data) == len(other.data) and len(self.data[0]) == len(other.data[0]):
result = [[self.data[i][j] - other.data[i][j] if i == j else self.data[i][j] for j in range(len(self.data[0]))] for i in range(len(self.data))]
return MatrizDiagonal(result)
else:
return super().__sub__(other)
def __mul__(self, other):
if isinstance(other, (int, float)):
result = [[self.data[i][j] * other if i == j else self.data[i][j] for j in range(len(self.data[0]))] for i in range(len(self.data))]
return MatrizDiagonal(result)
else:
return super().__mul__(other)
def determinante(self):
if len(self.data) == len(self.data[0]):
determinant = 1
for i in range(len(self.data)):
determinant *= self.data[i][i]
return determinant
else:
raise ValueError("O determinante só pode ser calculado para matrizes diagonais.")
class MatrizTriangularSuperior(Matriz):
def __init__(self, data):
super().__init__(data)
def __add__(self, other):
if isinstance(other, MatrizTriangularSuperior) and len(self.data) == len(other.data) and len(self.data[0]) == len(other.data[0]):
result = [[self.data[i][j] + other.data[i][j] if j >= i else self.data[i][j] for j in range(len(self.data[0]))] for i in range(len(self.data))]
return MatrizTriangularSuperior(result)
else:
return super().__add__(other)
def __sub__(self, other):
if isinstance(other, MatrizTriangularSuperior) and len(self.data) == len(other.data) and len(self.data[0]) == len(other.data[0]):
result = [[self.data[i][j] - other.data[i][j] if j >= i else self.data[i][j] for j in range(len(self.data[0]))] for i in range(len(self.data))]
return MatrizTriangularSuperior(result)
else:
return super().__sub__(other)
def __mul__(self, other):
if isinstance(other, (int, float)):
result = [[self.data[i][j] * other for j in range(len(self.data[0]))] for i in range(len(self.data))]
return MatrizTriangularSuperior(result)
else:
return super().__mul__(other)
class MatrizTriangularInferior(Matriz):
def __init__(self, data):
super().__init__(data)
def __add__(self, other):
if isinstance(other, MatrizTriangularInferior) and len(self.data) == len(other.data) and len(self.data[0]) == len(other.data[0]):
result = [[self.data[i][j] + other.data[i][j] if j <= i else self.data[i][j] for j in range(len(self.data[0]))] for i in range(len(self.data))]
return MatrizTriangularInferior(result)
else:
return super().__add__(other)
def __sub__(self, other):
if isinstance(other, MatrizTriangularInferior) and len(self.data) == len(other.data) and len(self.data[0]) == len(other.data[0]):
result = [[self.data[i][j] - other.data[i][j] if j <= i else self.data[i][j] for j in range(len(self.data[0]))] for i in range(len(self.data))]
return MatrizTriangularInferior(result)
else:
return super().__sub__(other)
def __mul__(self, other):
if isinstance(other, (int, float)):
result = [[self.data[i][j] * other for j in range(len(self.data[0]))] for i in range(len(self.data))]
return MatrizTriangularInferior(result)
else:
return super().__mul__(other)