From 004ad7ce65eaec52eb17ce300a8a0cce66f13f47 Mon Sep 17 00:00:00 2001 From: Niklas Birk Date: Sat, 16 Mar 2024 14:14:36 +0100 Subject: [PATCH] Refactor matrix.py --- src/matrix.py | 60 ++++++++++++++++++++++++++++++--------------------- 1 file changed, 35 insertions(+), 25 deletions(-) diff --git a/src/matrix.py b/src/matrix.py index afcc479..660b707 100644 --- a/src/matrix.py +++ b/src/matrix.py @@ -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. @@ -228,33 +253,18 @@ class Matrix: A norm is a positive definit, absolute homogeneous and subadditive function. 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 """ - 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):