Advertisement
Unlimiter

Matrix

Dec 9th, 2018
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.92 KB | None | 0 0
  1. # TODO: complete 'Matrix' class
  2. class Matrix:
  3.     def __init__(self, *iterables, former=None, width=0, height=0, start=0):
  4.         if former:
  5.             self.__rows = []
  6.             tmp_row = []
  7.             for i in range(height):
  8.                 for j in range(width):
  9.                     tmp_row.append(former(i, j) + start)
  10.                 self.__rows.append(tmp_row)
  11.                 tmp_row = []
  12.         else:
  13.             from unlimiter import itertools
  14.             if not itertools.of_iterables(iterables):
  15.                 raise TypeError("all arguments must be iterables of numbers")
  16.             if not itertools.of_equal_iterables(iterables):
  17.                 raise ArithmeticError("all arguments must be of the same size")
  18.             if not itertools.of_deep_type(iterables, int, float, complex):
  19.                 raise ValueError("all arguments must contain numerical items")
  20.             self.__rows = list(iterables)
  21.         self.__i = -1
  22.  
  23.     def __eq__(self, other):
  24.         if type(other) == type(self):
  25.             return self.__rows == other.rows
  26.  
  27.     def __ne__(self, other):
  28.         if type(other) == type(self):
  29.             return self.__rows != other.rows
  30.  
  31.     def __lt__(self, other):
  32.         if type(other) == type(self):
  33.             return self.__rows < other.rows
  34.  
  35.     def __gt__(self, other):
  36.         if type(other) == type(self):
  37.             return self.__rows > other.rows
  38.  
  39.     def __le__(self, other):
  40.         if type(other) == type(self):
  41.             return self.__rows <= other.rows
  42.  
  43.     def __ge__(self, other):
  44.         if type(other) == type(self):
  45.             return self.__rows >= other.rows
  46.  
  47.     def __len__(self):
  48.         return self.__m * self.__n
  49.  
  50.     def __getitem__(self, key):
  51.         if type(key) == int:
  52.             return self.__rows[key][0]
  53.         return self.__rows[key[0]][key[1]]
  54.  
  55.     def __setitem__(self, key, value):
  56.         if type(key) == int:
  57.             self.__rows[key][0] = value
  58.         else:
  59.             self.__rows[key[0]][key[1]] = value
  60.  
  61.     def __delitem__(self, key):
  62.         if type(key) == int:
  63.             self.__rows[key][0] = 0
  64.         else:
  65.             self.__rows[key[0]][key[1]] = 0
  66.  
  67.     def __iter__(self):
  68.         self.__i = -1
  69.         return self
  70.  
  71.     def __next__(self):
  72.         if self.__i + 1 == len(self.__rows):
  73.             raise StopIteration()
  74.         self.__i += 1
  75.         return self.__rows[self.__i]
  76.  
  77.     def __reversed__(self):
  78.         return list(
  79.             map(
  80.                 lambda i: list(reversed(i)),
  81.                 list(reversed(self.__rows))
  82.             )
  83.         )
  84.  
  85.     def __contains__(self, item):
  86.         if type(item) in (int, float, complex):
  87.             for row in self.__rows:
  88.                 for col in row:
  89.                     if item == col:
  90.                         return True
  91.             return False
  92.         elif hasattr(item, '__iter__'):
  93.             return item in self.__rows
  94.         return False
  95.  
  96.     def __add__(self, other):
  97.         if type(other) in (int, float, complex):
  98.             return type(self)(
  99.                 *list(
  100.                     map(
  101.                         lambda i: list(map(lambda j: j + other, i)),
  102.                         self.__rows
  103.                     )
  104.                 )
  105.             )
  106.         elif type(other) == type(self) and self.dimensions == other.dimensions:
  107.             tmp1 = []
  108.             tmp2 = []
  109.             for i in range(self.dimensions[0]):
  110.                 for j in range(self.dimensions[1]):
  111.                     tmp2.append(self[i, j] + other[i, j])
  112.                 tmp1.append(tmp2)
  113.                 tmp2 = []
  114.             return type(self)(*tmp1)
  115.  
  116.     def __sub__(self, other):
  117.         if type(other) in (int, float, complex):
  118.             return type(self)(
  119.                 *list(
  120.                     map(
  121.                         lambda i: list(map(lambda j: j - other, i)),
  122.                         self.__rows
  123.                     )
  124.                 )
  125.             )
  126.         elif type(other) == type(self) and self.dimensions == other.dimensions:
  127.             tmp1 = []
  128.             tmp2 = []
  129.             for i in range(self.dimensions[0]):
  130.                 for j in range(self.dimensions[1]):
  131.                     tmp2.append(self[i, j] - other[i, j])
  132.                 tmp1.append(tmp2)
  133.                 tmp2 = []
  134.             return type(self)(*tmp1)
  135.  
  136.     def __mul__(self, other):
  137.         if type(other) in (int, float, complex):
  138.             return type(self)(
  139.                 *list(
  140.                     map(
  141.                         lambda i: list(map(lambda j: j * other, i)),
  142.                         self.__rows
  143.                     )
  144.                 )
  145.             )
  146.         elif type(other) == type(self) and self.dimensions == other.dimensions:
  147.             tmp1 = []
  148.             tmp2 = []
  149.             for i in range(self.dimensions[0]):
  150.                 for j in range(self.dimensions[1]):
  151.                     tmp2.append(self[i, j] * other[i, j])
  152.                 tmp1.append(tmp2)
  153.                 tmp2 = []
  154.             return type(self)(*tmp1)
  155.  
  156.     def __div__(self, other):
  157.         if type(other) in (int, float, complex):
  158.             return type(self)(
  159.                 *list(
  160.                     map(
  161.                         lambda i: list(map(lambda j: j / other, i)),
  162.                         self.__rows
  163.                     )
  164.                 )
  165.             )
  166.         elif type(other) == type(self) and self.dimensions == other.dimensions:
  167.             tmp1 = []
  168.             tmp2 = []
  169.             for i in range(self.dimensions[0]):
  170.                 for j in range(self.dimensions[1]):
  171.                     tmp2.append(self[i, j] / other[i, j])
  172.                 tmp1.append(tmp2)
  173.                 tmp2 = []
  174.             return type(self)(*tmp1)
  175.  
  176.     def __floordiv__(self, other):
  177.         if type(other) in (int, float, complex):
  178.             return type(self)(
  179.                 *list(
  180.                     map(
  181.                         lambda i: list(map(lambda j: j // other, i)),
  182.                         self.__rows
  183.                     )
  184.                 )
  185.             )
  186.         elif type(other) == type(self) and self.dimensions == other.dimensions:
  187.             tmp1 = []
  188.             tmp2 = []
  189.             for i in range(self.dimensions[0]):
  190.                 for j in range(self.dimensions[1]):
  191.                     tmp2.append(self[i, j] // other[i, j])
  192.                 tmp1.append(tmp2)
  193.                 tmp2 = []
  194.             return type(self)(*tmp1)
  195.  
  196.     def __mod__(self, other):
  197.         if type(other) in (int, float, complex):
  198.             return type(self)(
  199.                 *list(
  200.                     map(
  201.                         lambda i: list(map(lambda j: j % other, i)),
  202.                         self.__rows
  203.                     )
  204.                 )
  205.             )
  206.         elif type(other) == type(self) and self.dimensions == other.dimensions:
  207.             tmp1 = []
  208.             tmp2 = []
  209.             for i in range(self.dimensions[0]):
  210.                 for j in range(self.dimensions[1]):
  211.                     tmp2.append(self[i, j] % other[i, j])
  212.                 tmp1.append(tmp2)
  213.                 tmp2 = []
  214.             return type(self)(*tmp1)
  215.  
  216.     def __pow__(self, other):
  217.         if type(other) in (int, float, complex):
  218.             return type(self)(
  219.                 *list(
  220.                     map(
  221.                         lambda i: list(map(lambda j: j ** other, i)),
  222.                         self.__rows
  223.                     )
  224.                 )
  225.             )
  226.         elif type(other) == type(self) and self.dimensions == other.dimensions:
  227.             tmp1 = []
  228.             tmp2 = []
  229.             for i in range(self.dimensions[0]):
  230.                 for j in range(self.dimensions[1]):
  231.                     tmp2.append(self[i, j] ** other[i, j])
  232.                 tmp1.append(tmp2)
  233.                 tmp2 = []
  234.             return type(self)(*tmp1)
  235.  
  236.     def __and__(self, other):
  237.         if type(other) in (int, float, complex):
  238.             return type(self)(
  239.                 *list(
  240.                     map(
  241.                         lambda i: list(map(lambda j: j & other, i)),
  242.                         self.__rows
  243.                     )
  244.                 )
  245.             )
  246.         elif type(other) == type(self) and self.dimensions == other.dimensions:
  247.             tmp1 = []
  248.             tmp2 = []
  249.             for i in range(self.dimensions[0]):
  250.                 for j in range(self.dimensions[1]):
  251.                     tmp2.append(self[i, j] & other[i, j])
  252.                 tmp1.append(tmp2)
  253.                 tmp2 = []
  254.             return type(self)(*tmp1)
  255.  
  256.     def __or__(self, other):
  257.         if type(other) in (int, float, complex):
  258.             return type(self)(
  259.                 *list(
  260.                     map(
  261.                         lambda i: list(map(lambda j: j | other, i)),
  262.                         self.__rows
  263.                     )
  264.                 )
  265.             )
  266.         elif type(other) == type(self) and self.dimensions == other.dimensions:
  267.             tmp1 = []
  268.             tmp2 = []
  269.             for i in range(self.dimensions[0]):
  270.                 for j in range(self.dimensions[1]):
  271.                     tmp2.append(self[i, j] | other[i, j])
  272.                 tmp1.append(tmp2)
  273.                 tmp2 = []
  274.             return type(self)(*tmp1)
  275.  
  276.     def __xor__(self, other):
  277.         if type(other) in (int, float, complex):
  278.             return type(self)(
  279.                 *list(
  280.                     map(
  281.                         lambda i: list(map(lambda j: j ^ other, i)),
  282.                         self.__rows
  283.                     )
  284.                 )
  285.             )
  286.         elif type(other) == type(self) and self.dimensions == other.dimensions:
  287.             tmp1 = []
  288.             tmp2 = []
  289.             for i in range(self.dimensions[0]):
  290.                 for j in range(self.dimensions[1]):
  291.                     tmp2.append(self[i, j] ^ other[i, j])
  292.                 tmp1.append(tmp2)
  293.                 tmp2 = []
  294.             return type(self)(*tmp1)
  295.  
  296.     def __lshift__(self, other):
  297.         if type(other) in (int, float, complex):
  298.             return type(self)(
  299.                 *list(
  300.                     map(
  301.                         lambda i: list(map(lambda j: j << other, i)),
  302.                         self.__rows
  303.                     )
  304.                 )
  305.             )
  306.         elif type(other) == type(self) and self.dimensions == other.dimensions:
  307.             tmp1 = []
  308.             tmp2 = []
  309.             for i in range(self.dimensions[0]):
  310.                 for j in range(self.dimensions[1]):
  311.                     tmp2.append(self[i, j] << other[i, j])
  312.                 tmp1.append(tmp2)
  313.                 tmp2 = []
  314.             return type(self)(*tmp1)
  315.  
  316.     def __rshift__(self, other):
  317.         if type(other) in (int, float, complex):
  318.             return type(self)(
  319.                 *list(
  320.                     map(
  321.                         lambda i: list(map(lambda j: j >> other, i)),
  322.                         self.__rows
  323.                     )
  324.                 )
  325.             )
  326.         elif type(other) == type(self) and self.dimensions == other.dimensions:
  327.             tmp1 = []
  328.             tmp2 = []
  329.             for i in range(self.dimensions[0]):
  330.                 for j in range(self.dimensions[1]):
  331.                     tmp2.append(self[i, j] >> other[i, j])
  332.                 tmp1.append(tmp2)
  333.                 tmp2 = []
  334.             return type(self)(*tmp1)
  335.  
  336.     def __bool__(self):
  337.         return bool(self.__rows)
  338.  
  339.     def __repr__(self):
  340.         return f"<{self.__m}x{self.__n} Matrix at {hex(id(self))}>"
  341.  
  342.     def __copy__(self):
  343.         tmp1 = []
  344.         tmp2 = []
  345.         for i in range(self.dimensions[0]):
  346.             for j in range(self.dimensions[1]):
  347.                 tmp2.append(self[i, j])
  348.             tmp1.append(tmp2)
  349.             tmp2 = []
  350.         return type(self)(*tmp1)
  351.  
  352.     def __deepcopy__(self, memo):
  353.         tmp1 = []
  354.         tmp2 = []
  355.         for i in range(self.dimensions[0]):
  356.             for j in range(self.dimensions[1]):
  357.                 tmp2.append(self[i, j])
  358.             tmp1.append(tmp2)
  359.             tmp2 = []
  360.         tmp = type(self)(*tmp1)
  361.         tmp._Matrix__i = self.__i
  362.         return tmp
  363.  
  364.     @property
  365.     def __m(self):
  366.         return len(self.__rows)
  367.  
  368.     @property
  369.     def __n(self):
  370.         return len(self.__rows[0]) if len(self.__rows) != 0 else 0
  371.  
  372.     @property
  373.     def dimensions(self):
  374.         return self.__m, self.__n
  375.  
  376.     @property
  377.     def copy(self):
  378.         tmp1 = []
  379.         tmp2 = []
  380.         for i in range(self.dimensions[0]):
  381.             for j in range(self.dimensions[1]):
  382.                 tmp2.append(self[i, j])
  383.             tmp1.append(tmp2)
  384.             tmp2 = []
  385.         return type(self)(*tmp1)
  386.  
  387.     def clear(self):
  388.         self.__rows.clear()
  389.         self.__i = -1
  390.  
  391.     @property
  392.     def sum(self):
  393.         from unlimiter import itertools
  394.         return sum(itertools.melt(self))
  395.  
  396.     @property
  397.     def rows(self):
  398.         return self.__rows
  399.  
  400.     @property
  401.     def cols(self):
  402.         tmp_cols = []
  403.         tmp_col = []
  404.         for j in range(self.__n):
  405.             for row in self.__rows:
  406.                 tmp_col.append(row[j])
  407.             tmp_cols.append(tmp_col)
  408.             tmp_col = []
  409.         return tmp_cols
  410.  
  411.     def transpose(self):
  412.         self.__rows = self.cols
  413.  
  414.     def transposed(self):
  415.         return type(self)(*self.cols)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement