# pyarith / ring.py

 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117``` ```#!/usr/local/bin/python # -*- coding: utf-8 -*- from abc import ABCMeta, abstractmethod from six import with_metaclass from func import gcd import group import logics.prop as lp class Ring(group.Group): @abstractmethod def __mul__(self, other): pass class CommutativeRing(Ring): pass class Integer(CommutativeRing, group.Integer): @staticmethod def as_Integer(integer): if type(integer) == Integer: return integer elif type(integer) == int: return Integer(integer) 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 __repr__(self): return ''.format(self.value) def __mul__(self, other): if type(self) == type(other): return self.__class__(self.value * other.value) else: return other.__rmul__(self) Integer.ZERO = Integer(0) Integer.ONE = Integer(1) class Rational(CommutativeRing, group.Rational): @staticmethod def as_Rational(integer): if type(integer) == Rational: return integer elif type(integer) == Integer: return Rational(integer.value, 1) elif type(integer) == group.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 either {2}, {3} or {4}' .format(type(integer), integer, Rational, Integer, int) ) def __repr__(self): return '' \ .format(self.numerator, self.denominator) def __mul__(self, other): if type(self) == type(other): return self.__class__(self.numerator * other.numerator, self.denominator * other.denominator) else: return other.__rmul__(self) Rational.ZERO = Rational(0, 1) Rational.ONE = Rational(1, 1) class BooleanAlgebra(CommutativeRing): def __init__(self, value=lp.F): self.value = value def __str__(self): return '{{{0}}}'.format(self.value) def __repr__(self): return '' \ .format(str(self)) def __eq__(self, other): if type(self) == type(other): return lp.equivalent_DNF(self.value, other.value) else: return False def __add__(self, other): if type(self) == type(other): return BooleanAlgebra(self.value ^ other.value) else: return other.__radd__(self) def __mul__(self, other): if type(self) == type(other): return BooleanAlgebra(self.value & other.value) else: return other.__radd__(self) def __neg__(self): return self BooleanAlgebra.ZERO = BooleanAlgebra() BooleanAlgebra.ONE = BooleanAlgebra(lp.T) ```