Refactor matrix.py
This commit is contained in:
parent
897fba6fb4
commit
004ad7ce65
@ -93,7 +93,7 @@ class Matrix:
|
|||||||
def __transpose_internal__(self):
|
def __transpose_internal__(self):
|
||||||
rows = self.__shape__[0]
|
rows = self.__shape__[0]
|
||||||
cols = self.__shape__[1]
|
cols = self.__shape__[1]
|
||||||
transposed_data = [[0 for _ in range(rows)] for _ in range(cols)]
|
transposed_data = [([0] * rows) for _ in range(cols)]
|
||||||
for i in range(rows):
|
for i in range(rows):
|
||||||
for j in range(cols):
|
for j in range(cols):
|
||||||
transposed_data[j][i] = self.__data__[i][j]
|
transposed_data[j][i] = self.__data__[i][j]
|
||||||
@ -194,9 +194,7 @@ class Matrix:
|
|||||||
def __mul_matrix_internal__(self, other):
|
def __mul_matrix_internal__(self, other):
|
||||||
rows = self.__shape__[0]
|
rows = self.__shape__[0]
|
||||||
cols = other.__shape__[1]
|
cols = other.__shape__[1]
|
||||||
|
new_data = [([0] * rows) for _ in range(cols)]
|
||||||
new_data = [[0 for _ in range(rows)] for _ in range(cols)]
|
|
||||||
|
|
||||||
for i in range(rows):
|
for i in range(rows):
|
||||||
for k in range(cols):
|
for k in range(cols):
|
||||||
new_data[i][k] = sum([self.__data__[i][j] * other.__data__[j][k] for j in range(self.__shape__[1])])
|
new_data[i][k] = sum([self.__data__[i][j] * other.__data__[j][k] for j in range(self.__shape__[1])])
|
||||||
@ -221,6 +219,33 @@ class Matrix:
|
|||||||
def __rmul__(self, other):
|
def __rmul__(self, other):
|
||||||
return self * other
|
return self * other
|
||||||
|
|
||||||
|
def __norm_frobenius__(self):
|
||||||
|
rows = self.__shape__[0]
|
||||||
|
cols = self.__shape__[1]
|
||||||
|
abs_sum = 0
|
||||||
|
for i in range(rows):
|
||||||
|
for j in range(cols):
|
||||||
|
abs_sum += abs(self.__data__[i][j]) ** 2
|
||||||
|
return math.sqrt(abs_sum)
|
||||||
|
|
||||||
|
def __norm_colsum__(self):
|
||||||
|
rows = self.__shape__[0]
|
||||||
|
cols = self.__shape__[1]
|
||||||
|
col_sums = [0] * cols
|
||||||
|
for j in range(cols):
|
||||||
|
for i in range(rows):
|
||||||
|
col_sums[j] += abs(self.__data__[i][j])
|
||||||
|
return max(col_sums)
|
||||||
|
|
||||||
|
def __norm_rowsum__(self):
|
||||||
|
rows = self.__shape__[0]
|
||||||
|
cols = self.__shape__[1]
|
||||||
|
row_sums = [0] * rows
|
||||||
|
for i in range(rows):
|
||||||
|
for j in range(cols):
|
||||||
|
row_sums[i] += abs(self.__data__[i][j])
|
||||||
|
return max(row_sums)
|
||||||
|
|
||||||
def norm(self, f: str = "frobenius"):
|
def norm(self, f: str = "frobenius"):
|
||||||
"""
|
"""
|
||||||
Calculates the norm of the matrix.
|
Calculates the norm of the matrix.
|
||||||
@ -228,33 +253,18 @@ class Matrix:
|
|||||||
A norm is a positive definit, absolute homogeneous and subadditive function.
|
A norm is a positive definit, absolute homogeneous and subadditive function.
|
||||||
For Matrices a norm is also sub-multiplicative.
|
For Matrices a norm is also sub-multiplicative.
|
||||||
|
|
||||||
:param f: The norm to be used, could be either "frobenius", "rowsum" or "colsum"
|
:param f: The norm to be used, could be either "frobenius", "row sum" or "col sum"
|
||||||
|
|
||||||
:return: the norm as a number
|
:return: the norm as a number
|
||||||
"""
|
"""
|
||||||
norm = 0
|
|
||||||
|
|
||||||
rows = self.__shape__[0]
|
|
||||||
cols = self.__shape__[1]
|
|
||||||
|
|
||||||
if f == "frobenius":
|
if f == "frobenius":
|
||||||
abs_sum = 0
|
norm = self.__norm_frobenius__()
|
||||||
for i in range(rows):
|
|
||||||
for j in range(cols):
|
|
||||||
abs_sum += abs(self.__data__[i][j]) ** 2
|
|
||||||
norm = math.sqrt(abs_sum)
|
|
||||||
elif f == "col sum":
|
elif f == "col sum":
|
||||||
row_sum = [0 for _ in range(cols)]
|
norm = self.__norm_colsum__()
|
||||||
for j in range(cols):
|
|
||||||
for i in range(rows):
|
|
||||||
row_sum[j] += abs(self.__data__[i][j])
|
|
||||||
norm = max(row_sum)
|
|
||||||
elif f == "row sum":
|
elif f == "row sum":
|
||||||
col_sum = [0 for _ in range(rows)]
|
norm = self.__norm_rowsum__()
|
||||||
for i in range(rows):
|
else:
|
||||||
for j in range(cols):
|
raise ValueError(f"Parameter f must be either \"frobenius\", \"row sum\" or \"col sum\"")
|
||||||
col_sum[i] += abs(self.__data__[i][j])
|
|
||||||
norm = max(col_sum)
|
|
||||||
return norm
|
return norm
|
||||||
|
|
||||||
def __getitem__(self, key):
|
def __getitem__(self, key):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user