from day01.ex02.vector import Vector class Matrix: def __init__(self, data, shape=None): self.data = data self.shape = shape @staticmethod def is_matrix(matrix): if not(isinstance(matrix, list)): return False for i, value in enumerate(matrix): if not(value[i], list): return False return True @staticmethod def __shape_to_data(value): ret = [] nbr = 0.0 for row in range(value[0]): ret.append([]) for col in range(value[1]): ret[row].append(nbr) return ret @staticmethod def __count_data(value): row = len(value) col = len(value[0]) return (row, col) @property def data(self): return self._data @property def shape(self): return self._shape @data.setter def data(self, value): try: if not value: raise ValueError elif not(isinstance(value, tuple)): if not(self.is_matrix(value)): raise TypeError else: self._data = value elif isinstance(value, tuple): self._data = self.__shape_to_data(value) self.shape = value except ValueError: print("valueerror") except TypeError: print("typeerror") @shape.setter def shape(self, value): try: if value is None: self._shape = self.__count_data(self.data) elif not(isinstance(value, tuple)): raise TypeError elif len(value) > 2: raise ValueError else: self._shape = value except TypeError: print("typeerror") except ValueError: print("valueerror") def __str__(self): txt = f"Matrix: {self.data}" return txt def __mul__(self, other): if isinstance(other, Vector): return matrix = Matrix([[0.0, 1.0, 2.0, 3.0], [4.0, 5.0, 6.0, 7.0]]) print("Matrix1", matrix) print(matrix.shape) matrix2 = Matrix((3, 3)) print("Matrix2", matrix2) print(matrix2.shape) matrix3 = Matrix([[0.0, 1.0, 2.0], [3.0, 4.0, 5.0], [6.0, 7.0, 8.0]], (3, 3)) print("Matrix3", matrix3) print(matrix3.shape) vector = Vector(5)