Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # TODO: complete 'Matrix' class
- class Matrix:
- def __init__(self, *iterables, former=None, width=0, height=0, start=0):
- if former:
- self.__rows = []
- tmp_row = []
- for i in range(height):
- for j in range(width):
- tmp_row.append(former(i, j) + start)
- self.__rows.append(tmp_row)
- tmp_row = []
- else:
- from unlimiter import itertools
- if not itertools.of_iterables(iterables):
- raise TypeError("all arguments must be iterables of numbers")
- if not itertools.of_equal_iterables(iterables):
- raise ArithmeticError("all arguments must be of the same size")
- if not itertools.of_deep_type(iterables, int, float, complex):
- raise ValueError("all arguments must contain numerical items")
- self.__rows = list(iterables)
- self.__i = -1
- def __eq__(self, other):
- if type(other) == type(self):
- return self.__rows == other.rows
- def __ne__(self, other):
- if type(other) == type(self):
- return self.__rows != other.rows
- def __lt__(self, other):
- if type(other) == type(self):
- return self.__rows < other.rows
- def __gt__(self, other):
- if type(other) == type(self):
- return self.__rows > other.rows
- def __le__(self, other):
- if type(other) == type(self):
- return self.__rows <= other.rows
- def __ge__(self, other):
- if type(other) == type(self):
- return self.__rows >= other.rows
- def __len__(self):
- return self.__m * self.__n
- def __getitem__(self, key):
- if type(key) == int:
- return self.__rows[key][0]
- return self.__rows[key[0]][key[1]]
- def __setitem__(self, key, value):
- if type(key) == int:
- self.__rows[key][0] = value
- else:
- self.__rows[key[0]][key[1]] = value
- def __delitem__(self, key):
- if type(key) == int:
- self.__rows[key][0] = 0
- else:
- self.__rows[key[0]][key[1]] = 0
- def __iter__(self):
- self.__i = -1
- return self
- def __next__(self):
- if self.__i + 1 == len(self.__rows):
- raise StopIteration()
- self.__i += 1
- return self.__rows[self.__i]
- def __reversed__(self):
- return list(
- map(
- lambda i: list(reversed(i)),
- list(reversed(self.__rows))
- )
- )
- def __contains__(self, item):
- if type(item) in (int, float, complex):
- for row in self.__rows:
- for col in row:
- if item == col:
- return True
- return False
- elif hasattr(item, '__iter__'):
- return item in self.__rows
- return False
- def __add__(self, other):
- if type(other) in (int, float, complex):
- return type(self)(
- *list(
- map(
- lambda i: list(map(lambda j: j + other, i)),
- self.__rows
- )
- )
- )
- elif type(other) == type(self) and self.dimensions == other.dimensions:
- tmp1 = []
- tmp2 = []
- for i in range(self.dimensions[0]):
- for j in range(self.dimensions[1]):
- tmp2.append(self[i, j] + other[i, j])
- tmp1.append(tmp2)
- tmp2 = []
- return type(self)(*tmp1)
- def __sub__(self, other):
- if type(other) in (int, float, complex):
- return type(self)(
- *list(
- map(
- lambda i: list(map(lambda j: j - other, i)),
- self.__rows
- )
- )
- )
- elif type(other) == type(self) and self.dimensions == other.dimensions:
- tmp1 = []
- tmp2 = []
- for i in range(self.dimensions[0]):
- for j in range(self.dimensions[1]):
- tmp2.append(self[i, j] - other[i, j])
- tmp1.append(tmp2)
- tmp2 = []
- return type(self)(*tmp1)
- def __mul__(self, other):
- if type(other) in (int, float, complex):
- return type(self)(
- *list(
- map(
- lambda i: list(map(lambda j: j * other, i)),
- self.__rows
- )
- )
- )
- elif type(other) == type(self) and self.dimensions == other.dimensions:
- tmp1 = []
- tmp2 = []
- for i in range(self.dimensions[0]):
- for j in range(self.dimensions[1]):
- tmp2.append(self[i, j] * other[i, j])
- tmp1.append(tmp2)
- tmp2 = []
- return type(self)(*tmp1)
- def __div__(self, other):
- if type(other) in (int, float, complex):
- return type(self)(
- *list(
- map(
- lambda i: list(map(lambda j: j / other, i)),
- self.__rows
- )
- )
- )
- elif type(other) == type(self) and self.dimensions == other.dimensions:
- tmp1 = []
- tmp2 = []
- for i in range(self.dimensions[0]):
- for j in range(self.dimensions[1]):
- tmp2.append(self[i, j] / other[i, j])
- tmp1.append(tmp2)
- tmp2 = []
- return type(self)(*tmp1)
- def __floordiv__(self, other):
- if type(other) in (int, float, complex):
- return type(self)(
- *list(
- map(
- lambda i: list(map(lambda j: j // other, i)),
- self.__rows
- )
- )
- )
- elif type(other) == type(self) and self.dimensions == other.dimensions:
- tmp1 = []
- tmp2 = []
- for i in range(self.dimensions[0]):
- for j in range(self.dimensions[1]):
- tmp2.append(self[i, j] // other[i, j])
- tmp1.append(tmp2)
- tmp2 = []
- return type(self)(*tmp1)
- def __mod__(self, other):
- if type(other) in (int, float, complex):
- return type(self)(
- *list(
- map(
- lambda i: list(map(lambda j: j % other, i)),
- self.__rows
- )
- )
- )
- elif type(other) == type(self) and self.dimensions == other.dimensions:
- tmp1 = []
- tmp2 = []
- for i in range(self.dimensions[0]):
- for j in range(self.dimensions[1]):
- tmp2.append(self[i, j] % other[i, j])
- tmp1.append(tmp2)
- tmp2 = []
- return type(self)(*tmp1)
- def __pow__(self, other):
- if type(other) in (int, float, complex):
- return type(self)(
- *list(
- map(
- lambda i: list(map(lambda j: j ** other, i)),
- self.__rows
- )
- )
- )
- elif type(other) == type(self) and self.dimensions == other.dimensions:
- tmp1 = []
- tmp2 = []
- for i in range(self.dimensions[0]):
- for j in range(self.dimensions[1]):
- tmp2.append(self[i, j] ** other[i, j])
- tmp1.append(tmp2)
- tmp2 = []
- return type(self)(*tmp1)
- def __and__(self, other):
- if type(other) in (int, float, complex):
- return type(self)(
- *list(
- map(
- lambda i: list(map(lambda j: j & other, i)),
- self.__rows
- )
- )
- )
- elif type(other) == type(self) and self.dimensions == other.dimensions:
- tmp1 = []
- tmp2 = []
- for i in range(self.dimensions[0]):
- for j in range(self.dimensions[1]):
- tmp2.append(self[i, j] & other[i, j])
- tmp1.append(tmp2)
- tmp2 = []
- return type(self)(*tmp1)
- def __or__(self, other):
- if type(other) in (int, float, complex):
- return type(self)(
- *list(
- map(
- lambda i: list(map(lambda j: j | other, i)),
- self.__rows
- )
- )
- )
- elif type(other) == type(self) and self.dimensions == other.dimensions:
- tmp1 = []
- tmp2 = []
- for i in range(self.dimensions[0]):
- for j in range(self.dimensions[1]):
- tmp2.append(self[i, j] | other[i, j])
- tmp1.append(tmp2)
- tmp2 = []
- return type(self)(*tmp1)
- def __xor__(self, other):
- if type(other) in (int, float, complex):
- return type(self)(
- *list(
- map(
- lambda i: list(map(lambda j: j ^ other, i)),
- self.__rows
- )
- )
- )
- elif type(other) == type(self) and self.dimensions == other.dimensions:
- tmp1 = []
- tmp2 = []
- for i in range(self.dimensions[0]):
- for j in range(self.dimensions[1]):
- tmp2.append(self[i, j] ^ other[i, j])
- tmp1.append(tmp2)
- tmp2 = []
- return type(self)(*tmp1)
- def __lshift__(self, other):
- if type(other) in (int, float, complex):
- return type(self)(
- *list(
- map(
- lambda i: list(map(lambda j: j << other, i)),
- self.__rows
- )
- )
- )
- elif type(other) == type(self) and self.dimensions == other.dimensions:
- tmp1 = []
- tmp2 = []
- for i in range(self.dimensions[0]):
- for j in range(self.dimensions[1]):
- tmp2.append(self[i, j] << other[i, j])
- tmp1.append(tmp2)
- tmp2 = []
- return type(self)(*tmp1)
- def __rshift__(self, other):
- if type(other) in (int, float, complex):
- return type(self)(
- *list(
- map(
- lambda i: list(map(lambda j: j >> other, i)),
- self.__rows
- )
- )
- )
- elif type(other) == type(self) and self.dimensions == other.dimensions:
- tmp1 = []
- tmp2 = []
- for i in range(self.dimensions[0]):
- for j in range(self.dimensions[1]):
- tmp2.append(self[i, j] >> other[i, j])
- tmp1.append(tmp2)
- tmp2 = []
- return type(self)(*tmp1)
- def __bool__(self):
- return bool(self.__rows)
- def __repr__(self):
- return f"<{self.__m}x{self.__n} Matrix at {hex(id(self))}>"
- def __copy__(self):
- tmp1 = []
- tmp2 = []
- for i in range(self.dimensions[0]):
- for j in range(self.dimensions[1]):
- tmp2.append(self[i, j])
- tmp1.append(tmp2)
- tmp2 = []
- return type(self)(*tmp1)
- def __deepcopy__(self, memo):
- tmp1 = []
- tmp2 = []
- for i in range(self.dimensions[0]):
- for j in range(self.dimensions[1]):
- tmp2.append(self[i, j])
- tmp1.append(tmp2)
- tmp2 = []
- tmp = type(self)(*tmp1)
- tmp._Matrix__i = self.__i
- return tmp
- @property
- def __m(self):
- return len(self.__rows)
- @property
- def __n(self):
- return len(self.__rows[0]) if len(self.__rows) != 0 else 0
- @property
- def dimensions(self):
- return self.__m, self.__n
- @property
- def copy(self):
- tmp1 = []
- tmp2 = []
- for i in range(self.dimensions[0]):
- for j in range(self.dimensions[1]):
- tmp2.append(self[i, j])
- tmp1.append(tmp2)
- tmp2 = []
- return type(self)(*tmp1)
- def clear(self):
- self.__rows.clear()
- self.__i = -1
- @property
- def sum(self):
- from unlimiter import itertools
- return sum(itertools.melt(self))
- @property
- def rows(self):
- return self.__rows
- @property
- def cols(self):
- tmp_cols = []
- tmp_col = []
- for j in range(self.__n):
- for row in self.__rows:
- tmp_col.append(row[j])
- tmp_cols.append(tmp_col)
- tmp_col = []
- return tmp_cols
- def transpose(self):
- self.__rows = self.cols
- def transposed(self):
- return type(self)(*self.cols)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement