| 3 | import inspect |
| 4 | |
| 5 | class Superposition(Set): |
| 6 | def __init__(self,*argl): |
| 7 | super(Superposition,self).__init__(self) |
| 8 | for arg in argl: |
| 9 | if isinstance(arg,Set): |
| 10 | self |= arg |
| 11 | elif isinstance(arg,list) or isinstance(arg,tuple): |
| 12 | self |= Set(arg) |
| 13 | else: |
| 14 | self.add(arg) |
| 15 | |
| 16 | def eigenstates(self): |
| 17 | return list(self) |
| 18 | |
| 19 | def _cartesian(self,other,op,order=True): |
| 20 | if order: |
| 21 | if isinstance(other,Superposition): |
| 22 | s = self.__class__([op(x,y) for x in self for y in other]) |
| 23 | else: |
| 24 | s = self.__class__([op(x,other) for x in self]) |
| 25 | else: |
| 26 | if isinstance(other,Superposition): |
| 27 | s = self.__class__([op(y,x) for x in self for y in other]) |
| 28 | else: |
| 29 | s = self.__class__([op(other,x) for x in self]) |
| 30 | |
| 31 | return s |
| 32 | def __add__(self,other): |
| 33 | return self._cartesian(other,operator.add) |
| 34 | |
| 35 | __radd__ = __add__ |
| 36 | |
| 37 | def __sub__(self,other): |
| 38 | return self._cartesian(other,operator.sub) |
| 39 | |
| 40 | def __rsub__(self,other): |
| 41 | return self._cartesian(other,operator.sub,False) |
| 42 | |
| 43 | def __mod__(self,other): |
| 44 | return self._cartesian(other,operator.mod) |
| 45 | |
| 46 | def __rmod__(self,other): |
| 47 | return self._cartesian(other,operator.mod,False) |
| 48 | |
| 49 | def __mul__(self,other): |
| 50 | return self._cartesian(other,operator.mul) |
| 51 | |
| 52 | __rmul__ = __mul__ |
| 53 | |
| 54 | def __div__(self,other): |
| 55 | return self._cartesian(other,operator.div) |
| 56 | |
| 57 | def __rdiv__(self,other): |
| 58 | return self._cartesian(other,operator.div,False) |
| 59 | |
| 60 | def _comp(self,other,op): |
| 61 | return self.__class__([x for x in self if op(x,other)]) |
| 62 | |