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:
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
"""
if isinstance(data, numpy.ndarray):
try:
data.shape[1]
except IndexError:
self.__shape__ = (data.shape[0], 1)
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):
# Case Matrix(str, int)
if n is not None and model is not None:
... # TODO: what shall one do here?
# Case: Matrix(list, str, int)
elif n is not None and structure is not None and data is not None:
if structure == "unity":
... # TODO: what does it mean?
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)
self.__data__ = tridiag
self.__shape__ = tridiag.shape
elif isinstance(model, str) and isinstance(n, int):
... # TODO: what shall one do here?
# Case: Matrix(list, str, int)
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:
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``
: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):
return (self.__data__ == other.__data__).all()
to_compare = other.__data__
elif isinstance(other, list):
return (self.__data__ == numpy.array(other)).all()
to_compare = numpy.array(other)
elif isinstance(other, numpy.ndarray):
return (self.__data__ == other).all()
to_compare = other
else:
raise ValueError("Matrix type is not comparable to type of given ``other``")
return (self.__data__ == to_compare).all()
def __str__(self):
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):
...