diff --git a/src/matrix.py b/src/matrix.py index 3da103b..4489d56 100644 --- a/src/matrix.py +++ b/src/matrix.py @@ -1,4 +1,5 @@ import numpy +from numpy import linalg class Matrix: @@ -47,7 +48,8 @@ class Matrix: elif structure == "tridiagonal": if len(data) != 3: raise ValueError("If structure is tridiagonal, then the given data must be of length 3") - tridiag = numpy.diag([data[0]] * (n-1), -1) + numpy.diag([data[1]] * n, 0) + numpy.diag([data[2]] * (n-1), 1) + tridiag = numpy.diag([data[0]] * (n - 1), -1) + numpy.diag([data[1]] * n, 0) + numpy.diag( + [data[2]] * (n - 1), 1) self.__data__ = tridiag self.__shape__ = tridiag.shape # Case: Matrix(list, str, int) @@ -64,7 +66,8 @@ class Matrix: self.__shape__ = data.shape self.__data__ = data else: - raise ValueError("Only following signatures are allowed: (numpy.ndarray), (list, tuple), (list, str, int), (str, int)") + raise ValueError( + "Only following signatures are allowed: (numpy.ndarray), (list, tuple), (list, str, int), (str, int)") def get_data(self): """ @@ -126,16 +129,47 @@ class Matrix: def __rsub__(self, other): return -self + other + def __mul__(self, other): + if isinstance(other, Matrix): + if self.__shape__[1] != other.__shape__[0]: + raise ValueError( + "The amount of columns of the first operand must match the amount of rows of the second operand") + return Matrix(self.__data__ @ other.__data__) + elif isinstance(other, int) or isinstance(other, float): + return Matrix(other * self.__data__) + else: + raise ValueError("Only a number or another ``Matrix`` can be multiplied to a ``Matrix``") + + def __rmul__(self, other): + return self * other + def __truediv__(self, other): if isinstance(other, int) or isinstance(other, float): return self.__data__ / other else: raise ValueError("A ``Matrix`` can only be divided ba a number") - def __mul__(self, other): - if isinstance(other, Matrix): - if self.__shape__[1] != other.__shape__[0]: - raise ValueError("The amount of columns of the first operand must match the amount of rows of the second operand") - return Matrix(self.__data__ * other.__data__) - elif isinstance(other, int) or isinstance(other, float): - ... + def norm(self, f: str = "frobenius"): + """ + Calculates the norm of the 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" + + :return: the norm as a number + """ + t = "fro" + if f == "colsum": + t = 1 + elif f == "rowsum": + t = numpy.inf + return linalg.norm(self.__data__, t) + + def __getitem__(self, key): + return self.__data__[key] + + def __setitem__(self, key, value): + self.__data__[key] = value + diff --git a/test/test_matrix.py b/test/test_matrix.py index 8d415cd..758fce1 100644 --- a/test/test_matrix.py +++ b/test/test_matrix.py @@ -180,15 +180,6 @@ class TestMatrix(TestCase): self.assertEqual(expected, actual) - def test_should_div_matrix_by_scalar(self): - m = Matrix([5, 10, 15, 20], (2, 2)) - s = 5 - - actual = m / s - expected = Matrix([1, 2, 3, 4], (2, 2)) - - self.assertEqual(expected, actual) - def test_should_raise_value_missmatch_error_while_dividing_with_other_than_scalar(self): m = Matrix([1, 2, 3, 4], (2, 2)) o = "" @@ -218,3 +209,155 @@ class TestMatrix(TestCase): m2 = Matrix([3, 4], (2, 1)) self.assertRaises(ValueError, lambda: m1 * m2) + + def test_should_mul_scalar_to_matrix(self): + m = Matrix([1, 2, 3, 4], (2, 2)) + s = 5 + + actual = m * s + expected = Matrix([5, 10, 15, 20], (2, 2)) + + self.assertEqual(expected, actual) + + def test_should_rmul_scalar_to_matrix(self): + m = Matrix([1, 2, 3, 4], (2, 2)) + s = 5 + + actual = s * m + expected = Matrix([5, 10, 15, 20], (2, 2)) + + self.assertEqual(expected, actual) + + def test_should_div_matrix_by_scalar(self): + m = Matrix([5, 10, 15, 20], (2, 2)) + s = 5 + + actual = m / s + expected = Matrix([1, 2, 3, 4], (2, 2)) + + self.assertEqual(expected, actual) + + def test_should_return_frobenius_norm(self): + m = Matrix([1, 2, 3, 4], (2, 2)) + + actual = m.norm() + expected = 5.477 + + self.assertAlmostEqual(expected, actual, 3) + + def test_should_return_colsum_norm(self): + m = Matrix([1, 2, 3, 4], (2, 2)) + + actual = m.norm("colsum") + expected = 6 + + self.assertEqual(expected, actual) + + def test_should_return_rowsum_norm(self): + m = Matrix([1, 2, 3, 4], (2, 2)) + + actual = m.norm("rowsum") + expected = 7 + + self.assertEqual(expected, actual) + + def test_should_return_first_element(self): + m = Matrix([1, 2, 3, 4, 5, 6, 7, 8, 9], (3, 3)) + + actual = m[0, 0] + expected = 1 + + self.assertEqual(expected, actual) + + def test_should_return_last_element(self): + m = Matrix([1, 2, 3, 4, 5, 6, 7, 8, 9], (3, 3)) + + actual = m[2, 2] + expected = 9 + + self.assertEqual(expected, actual) + + def test_should_return_first_row(self): + m = Matrix([1, 2, 3, 4, 5, 6, 7, 8, 9], (3, 3)) + + actual = m[0] + expected = Matrix([1, 2, 3], (1, 3)) + + self.assertEqual(expected, actual) + + def test_should_return_last_row_except_last_element(self): + m = Matrix([1, 2, 3, 4, 5, 6, 7, 8, 9], (3, 3)) + + actual = m[2, 0:2] + expected = Matrix([7, 8], (1, 2)) + + self.assertEqual(expected, actual) + + def test_should_return_mid_column(self): + m = Matrix([1, 2, 3, 4, 5, 6, 7, 8, 9], (3, 3)) + + actual = m[:, 1] + expected = Matrix([2, 5, 8], (1, 3)) + + self.assertEqual(expected, actual) + + def test_should_return_first_column_except_middle_element(self): + m = Matrix([1, 2, 3, 4, 5, 6, 7, 8, 9], (3, 3)) + + actual = m[[0, 2], 0] + expected = Matrix([1, 7], (1, 2)) + + self.assertEqual(expected, actual) + + def test_should_return_mid_submatrix(self): + m = Matrix(list(range(1, 17)), (4, 4)) + + actual = m[1:3, 1:3] + expected = Matrix([6, 7, 10, 11], (2, 2)) + + self.assertEqual(expected, actual) + + def test_should_set_first_element(self): + m = Matrix([1, 2, 3, 4, 5, 6, 7, 8, 9], (3, 3)) + + m[0, 0] = 10 + actual = m + expected = Matrix([10, 2, 3, 4, 5, 6, 7, 8, 9], (3, 3)) + + self.assertEqual(expected, actual) + + def test_should_set_mid_column(self): + m = Matrix([1, 2, 3, 4, 5, 6, 7, 8, 9], (3, 3)) + + m[:, 1] = [20, 50, 80] + actual = m + expected = Matrix([1, 20, 3, 4, 50, 6, 7, 80, 9], (3, 3)) + + self.assertEqual(expected, actual) + + def test_should_set_last_row(self): + m = Matrix([1, 2, 3, 4, 5, 6, 7, 8, 9], (3, 3)) + + m[2] = [70, 80, 90] + actual = m + expected = Matrix([1, 2, 3, 4, 5, 6, 70, 80, 90], (3, 3)) + + self.assertEqual(expected, actual) + + def test_should_set_first_row_except_mid_element(self): + m = Matrix([1, 2, 3, 4, 5, 6, 7, 8, 9], (3, 3)) + + m[0, [0, 2]] = [10, 30] + actual = m + expected = Matrix([10, 2, 30, 4, 5, 6, 7, 8, 9], (3, 3)) + + self.assertEqual(expected, actual) + + def test_should_set_mid_submatrix(self): + m = Matrix(list(range(1, 17)), (4, 4)) + + m[1:3, 1:3] = [[60, 70], [100, 110]] + actual = m + expected = Matrix([1, 2, 3, 4, 5, 60, 70, 8, 9, 100, 110, 12, 13, 14, 15, 16], (4, 4)) + + self.assertEqual(expected, actual)