Commits

tomo cocoa committed 6faf00a

update

Comments (0)

Files changed (2)

 # -*- coding: utf-8 -*-
 
 import ring
+from abc import ABCMeta, abstractmethod
+
 
 class Field(ring.Ring):
-    pass
+    __metaclass__ = ABCMeta
 
+    @abstractmethod
+    def __rdiv__(self, other):
+        pass
 
+
+
+def inversed(value):
+    if not isinstance(value, Field):
+        raise TypeError()
+    return 1 / value
     def __str__(self):
         return str(self.value)
 
+    def __repr__(self):
+        return '<group.Integer value: {0}>'.format(self.value)
+
     def __eq__(self, other):
         if type(self) == type(other):
             return self.value == other.value
 
 class Rational(Group):
     @staticmethod
-    def from_Integer(integer):
-        return Rational(integer, 1)
+    def as_Rational(integer):
+        if type(integer) == Rational:
+            return integer
+        elif type(integer) == Integer:
+            return Rational(integer.value, 1)
+        elif type(integer) == int:
+            return Rational(integer, 1)
+
+        raise TypeError(('the argument "integer" is in a type of {0} ' + \
+                             'and its value is "{1}". ' + \
+                             'it must be in a type of {2} or {3}') \
+                             .format(type(integer), integer, Integer, int))
 
     def __init__(self, numerator=0, denominator=1):
         assert not denominator == 0
     def __str__(self):
         return '{0}/{1}'.format(self.numerator, self.denominator)
 
+    def __repr__(self):
+        return '<group.Rational numerator: {0}, denominator: {1}>' \
+            .format(self.numerator, self.denominator)
+
     def __eq__(self, other):
         if type(self) == type(other):
             return self.numerator * other.denominator == \
                 self.denominator * other.numerator
-        elif type(other) == Integer:
-            return self == Rational.from_Integer(other)
-        else:
+        try:
+            return self == Rational.as_Rational(other)
+        except TypeError:
             return False
 
     def __add__(self, other):
         def __str__(self):
             return '({0}, {1})'.format(self.value1, self.value2)
 
+        def __repr__(self):
+            return '<Prod value1: {0}, value2: {1}>' \
+                .format(repr(self.value1), repr(self.value2))
+
         def __eq__(self, other):
             return self.value1 == other.value1 and self.value2 == other.value2
 
     return Prod
 
 
+def residue(modulo):
+    class Residue(Group):
+        def __init__(self, value=0):
+            self.value = value
+            self._residue()
+
+        def _residue(self):
+            self.value %= modulo
+
+        def __str__(self):
+            return '{0} mod {1}'.format(self.value, modulo)
+
+        def __repr__(self):
+            return '<Residue value: {0}, module: {1}>' \
+                .format(self.value, modulo)
+
+        def __eq__(self, other):
+            if type(self) == type(other):
+                self._residue()
+                other._residue()
+                return self.value == other.value
+            else:
+                return False
+
+        def __add__(self, other):
+            if type(self) == type(other):
+                return Residue(self.value + other.value)
+            else:
+                return other.__radd__(self)
+
+        def __neg__(self):
+            return Residue(- self.value)
+
+    Residue.ZERO = Residue(0)
+    Residue.modulo = modulo
+
+    return Residue
+
+
 if __name__ == '__main__':
     pass