1
0

Refactor matrix.py

This commit is contained in:
Niklas Birk 2024-03-16 14:14:36 +01:00
parent 897fba6fb4
commit 004ad7ce65

View File

@ -93,7 +93,7 @@ class Matrix:
def __transpose_internal__(self):
rows = self.__shape__[0]
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 j in range(cols):
transposed_data[j][i] = self.__data__[i][j]
@ -194,9 +194,7 @@ class Matrix:
def __mul_matrix_internal__(self, other):
rows = self.__shape__[0]
cols = other.__shape__[1]
new_data = [[0 for _ in range(rows)] for _ in range(cols)]
new_data = [([0] * rows) for _ in range(cols)]
for i in range(rows):
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])])
@ -221,6 +219,33 @@ class Matrix:
def __rmul__(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"):
"""
Calculates the norm of the matrix.
@ -232,29 +257,14 @@ class Matrix:
:return: the norm as a number
"""
norm = 0
rows = self.__shape__[0]
cols = self.__shape__[1]
if f == "frobenius":
abs_sum = 0
for i in range(rows):
for j in range(cols):
abs_sum += abs(self.__data__[i][j]) ** 2
norm = math.sqrt(abs_sum)
norm = self.__norm_frobenius__()
elif f == "col sum":
row_sum = [0 for _ in range(cols)]
for j in range(cols):
for i in range(rows):
row_sum[j] += abs(self.__data__[i][j])
norm = max(row_sum)
norm = self.__norm_colsum__()
elif f == "row sum":
col_sum = [0 for _ in range(rows)]
for i in range(rows):
for j in range(cols):
col_sum[i] += abs(self.__data__[i][j])
norm = max(col_sum)
norm = self.__norm_rowsum__()
else:
raise ValueError(f"Parameter f must be either \"frobenius\", \"row sum\" or \"col sum\"")
return norm
def __getitem__(self, key):