Advertisement
JoelSjogren

Untitled

Mar 8th, 2016
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.74 KB | None | 0 0
  1. class PixPix:
  2.     def __init__(self, a, b, c, d):
  3.         self.a, self.b, self.c, self.d = a, b, c, d
  4.    
  5.     def __mul__(self, x):
  6.         a, b, c, d = self.a, self.b, self.c, self.d
  7.         e, f, g, h = x.a, x.b, x.c, x.d
  8.         if d == e and c == f:
  9.             return PixPix(a, b, g, h)
  10.    
  11.     def __repr__(self):
  12.         return '[{},{},{},{}]'.format(self.a, self.b, self.c, self.d)
  13.    
  14.     def __eq__(self, x):
  15.         return (self.a, self.b, self.c, self.d) == (x.a, x.b, x.c, x.d)
  16.  
  17. class Maxel:
  18.     def __init__(self, *pixs):
  19.         self.pixs = pixs
  20.    
  21.     def __add__(self, x):
  22.         return Maxel(self.pixs + x.pixs)
  23.    
  24.     def __mul__(self, x):
  25.         return Maxel(*(i*j for i in self.pixs for j in x.pixs if i*j is not None))
  26.  
  27.     def __repr__(self):
  28.         return '[' + ' '.join(map(repr, self.pixs)) + ']'
  29.    
  30.     def __eq__(self, x):
  31.         if len(self.pixs) != len(x.pixs):
  32.             return False
  33.         a = list(self.pixs)
  34.         b = list(x.pixs)
  35.         while a:
  36.             if a[0] not in b:
  37.                 return False
  38.             b.remove(a[0])
  39.             a.remove(a[0])
  40.         return True
  41.    
  42.     def __pow__(self, n):
  43.         assert n == int(n) and n >= 1
  44.         a = self
  45.         for _ in range(n-1):
  46.             a *= self
  47.         return a
  48.  
  49. m = Maxel(PixPix(2,1,1,1), PixPix(2,2,2,1), PixPix(1,1,2,2), PixPix(1,2,1,2))
  50.  
  51. """
  52.  
  53. >>> m
  54. [[2,1,1,1] [2,2,2,1] [1,1,2,2] [1,2,1,2]]
  55. >>> m**4
  56. [[2,1,1,2] [2,2,2,2] [1,1,1,1] [1,2,2,1]]
  57. >>> m**4 == (m**4)*(m**4)
  58. True
  59. >>> m == m**5
  60. True
  61. >>> m**2 == m**4
  62. False
  63. >>> [[2*p.a+p.b-2, 2*p.d+p.c-2] for p in m.pixs]
  64. [[3, 1], [4, 2], [1, 4], [2, 3]]
  65.  
  66. Record [3,1] etc in a matrix.
  67.  
  68. [0 0 0 1; 0 0 1 0; 1 0 0 0; 0 1 0 0] = [0 -1; 1 0] = i
  69.  
  70. """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement