1
0

Reformat constructor of class Matrix in matrix.py to match to non-None values instead of instance checking;

More implementation of operators in class Matrix
This commit is contained in:
Niklas Birk 2023-12-25 19:25:33 +01:00
parent 71fa91644e
commit b9c03b5d5f
2 changed files with 184 additions and 18 deletions

View File

@ -33,18 +33,11 @@ class Matrix:
:rtype: Matrix :rtype: Matrix
""" """
if isinstance(data, numpy.ndarray): # Case Matrix(str, int)
try: if n is not None and model is not None:
data.shape[1] ... # TODO: what shall one do here?
except IndexError: # Case: Matrix(list, str, int)
self.__shape__ = (data.shape[0], 1) elif n is not None and structure is not None and data is not None:
else:
self.__shape__ = data.shape
self.__data__ = data
elif isinstance(data, list) and isinstance(shape, tuple):
self.__shape__ = shape
self.__data__ = numpy.array(data).reshape(shape)
elif isinstance(data, list) and isinstance(structure, str) and isinstance(n, int):
if structure == "unity": if structure == "unity":
... # TODO: what does it mean? ... # TODO: what does it mean?
elif structure == "diagonal": elif structure == "diagonal":
@ -57,8 +50,19 @@ class Matrix:
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.__data__ = tridiag
self.__shape__ = tridiag.shape self.__shape__ = tridiag.shape
elif isinstance(model, str) and isinstance(n, int): # Case: Matrix(list, str, int)
... # TODO: what shall one do here? elif shape is not None and data is not None:
self.__shape__ = shape
self.__data__ = numpy.array(data).reshape(shape)
# Case: Matrix(numpy.ndarray)
elif data is not None:
try:
data.shape[1]
except IndexError:
self.__shape__ = (data.shape[0], 1)
else:
self.__shape__ = data.shape
self.__data__ = data
else: 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)")
@ -85,16 +89,53 @@ class Matrix:
Return ``self==value`` Return ``self==value``
:param other: The object to compare to; must be either a ``Matrix``, a ``list`` or a ``numpy.ndarray`` :param other: The object to compare to; must be either a ``Matrix``, a ``list`` or a ``numpy.ndarray``
:return: True if data in the matrix is totally equal to the given data in other, otherwise False :return: True if data in the matrix are equal to the given data in other for each component, otherwise False
""" """
if isinstance(other, Matrix): if isinstance(other, Matrix):
return (self.__data__ == other.__data__).all() to_compare = other.__data__
elif isinstance(other, list): elif isinstance(other, list):
return (self.__data__ == numpy.array(other)).all() to_compare = numpy.array(other)
elif isinstance(other, numpy.ndarray): elif isinstance(other, numpy.ndarray):
return (self.__data__ == other).all() to_compare = other
else: else:
raise ValueError("Matrix type is not comparable to type of given ``other``") raise ValueError("Matrix type is not comparable to type of given ``other``")
return (self.__data__ == to_compare).all()
def __str__(self): def __str__(self):
return str(self.__data__) return str(self.__data__)
def __neg__(self):
return Matrix(-self.__data__)
def __add__(self, other):
if isinstance(other, Matrix):
if self.__shape__ != other.__shape__:
raise ValueError("The shape of the operands must be the same")
return self.__data__ + other.__data__
elif isinstance(other, int) or isinstance(other, float):
return self.__data__ + other
else:
raise ValueError("Only a number or another ``Matrix`` can be added to a ``Matrix``")
def __radd__(self, other):
return self + other
def __sub__(self, other):
return self + (-other)
def __rsub__(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):
...

View File

@ -86,6 +86,18 @@ class TestMatrix(TestCase):
expected = [[1, -1, 0, 0], [-1, 1, -1, 0], [0, -1, 1, -1], [0, 0, -1, 1]] expected = [[1, -1, 0, 0], [-1, 1, -1, 0], [0, -1, 1, -1], [0, 0, -1, 1]]
self.assertEqual(expected, actual) self.assertEqual(expected, actual)
def test_matrices_should_be_equal(self):
m1 = Matrix([1, 2, 3, 4], (2, 2))
m2 = Matrix([1, 2, 3, 4], (2, 2))
self.assertEqual(m1, m2)
def test_matrices_should_not_be_equal(self):
m1 = Matrix([1, 2, 3, 4], (2, 2))
m2 = Matrix([1, 2, 3, 3], (2, 2))
self.assertNotEqual(m1, m2)
def test_should_transpose_matrix(self): def test_should_transpose_matrix(self):
data = [1, 2, 3, 4, 5, 6, 7, 8, 9] data = [1, 2, 3, 4, 5, 6, 7, 8, 9]
actual = Matrix(data, (3, 3)) actual = Matrix(data, (3, 3))
@ -93,3 +105,116 @@ class TestMatrix(TestCase):
actual = actual.transpose() actual = actual.transpose()
expected = [[1, 4, 7], [2, 5, 8], [3, 6, 9]] expected = [[1, 4, 7], [2, 5, 8], [3, 6, 9]]
self.assertEqual(expected, actual) self.assertEqual(expected, actual)
def test_should_negate_matrix(self):
m = Matrix([1, 2, 3, 4], (2, 2))
actual = -m
expected = Matrix([-1, -2, -3, -4], (2, 2))
self.assertEqual(expected, actual)
def test_should_add_two_matrices(self):
m1 = Matrix([1, 2, 3, 4], (2, 2))
m2 = Matrix([4, 3, 2, 1], (2, 2))
actual = m1 + m2
expected = Matrix([5, 5, 5, 5], (2, 2))
self.assertEqual(expected, actual)
def test_should_add_scalar_to_matrix(self):
m = Matrix([1, 2, 3, 4], (2, 2))
s = 5
actual = m + s
expected = Matrix([6, 7, 8, 9], (2, 2))
self.assertEqual(expected, actual)
def test_should_radd_scalar_to_matrix(self):
m = Matrix([1, 2, 3, 4], (2, 2))
s = 5
actual = s + m
expected = Matrix([6, 7, 8, 9], (2, 2))
self.assertEqual(expected, actual)
def test_should_raise_shape_missmatch_error_while_adding_vectors(self):
m1 = Matrix([1, 2, 3, 4], (2, 2))
m2 = Matrix([1, 2], (2, 1))
self.assertRaises(ValueError, lambda: m1 + m2)
def test_should_raise_value_missmatch_error_while_adding_vectors(self):
m = Matrix([1, 2, 3, 4], (2, 2))
o = ""
self.assertRaises(ValueError, lambda: m + o)
def test_should_sub_two_matrices(self):
m1 = Matrix([1, 2, 3, 4], (2, 2))
m2 = Matrix([4, 3, 2, 1], (2, 2))
actual = m1 - m2
expected = Matrix([-3, -1, 1, 3], (2, 2))
self.assertEqual(expected, actual)
def test_should_sub_scalar_of_matrix(self):
m = Matrix([1, 2, 3, 4], (2, 2))
s = 5
actual = m - s
expected = Matrix([-4, -3, -2, -1], (2, 2))
self.assertEqual(expected, actual)
def test_should_rsub_scalar_of_matrix(self):
m = Matrix([1, 2, 3, 4], (2, 2))
s = 5
actual = s - m
expected = Matrix([4, 3, 2, 1], (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_raise_value_missmatch_error_while_dividing_with_other_than_scalar(self):
m = Matrix([1, 2, 3, 4], (2, 2))
o = ""
self.assertRaises(ValueError, lambda: m / o)
def test_should_mul_matrices_1(self):
m1 = Matrix([1, 2], (2, 1))
m2 = Matrix([3, 4], (1, 2))
actual = m1 * m2
expected = Matrix([3, 4, 6, 8], (2, 2))
self.assertEqual(expected, actual)
def test_should_mul_matrices_2(self):
m1 = Matrix([1, 2], (1, 2))
m2 = Matrix([3, 4], (2, 1))
actual = m1 * m2
expected = Matrix([11], (1, 1))
self.assertEqual(expected, actual)
def test_should_raise_shape_missmatch_error_while_multiplying_matrices(self):
m1 = Matrix([1, 2], (2, 1))
m2 = Matrix([3, 4], (2, 1))
self.assertRaises(ValueError, lambda: m1 * m2)