def __iter__(self): if self.root.isZero(): yield Monom.zero elif self.root.isOne(): yield Monom.one else: monom, path = [], [] i = self.root while i.var >= 0: monom.append(i.var) path.append(i) i = i.mul yield Monom(vars=monom) while path: while path and path[-1].add.isZero(): path.pop() monom.pop() if path: i = path.pop().add monom.pop() while i != ZDD._one: monom.append(i.var) path.append(i) i = i.mul if monom == []: yield Monom.one break yield Monom(vars=monom)
def setUp(self): Monom.size = 4 self.variables = ( Monom(vars=[0]), # a Monom(vars=[1]), # b Monom(vars=[2]), # c Monom(vars=[3]), # d )
def test_lm(self): a, b, c, d = self.B.gens m_abc = Monom((1, 1, 1, 0)) m_b = Monom((0, 1, 0, 0)) _1 = self.B.one f = a*b*c + c*d + a*b + _1 g = c*d + b self.assertEqual(f.lm(), m_abc) self.assertEqual(g.lm(), m_b)
class Element: def __init__(self, element): self.element = element self.char_dict = {} self.index = 0 def split_element(self): for x in self.element: if x == '^': self.index = 1 self.check_element(x) return self.char_dict def check_element(self, char): if self.index == 1: if char == '^': pass elif 'power' in self.char_dict: self.char_dict['power'] += char else: self.char_dict['power'] = char elif char.isdigit(): if 'coeff' in self.char_dict: self.char_dict['coeff'] += char else: self.char_dict['coeff'] = char else: self.char_dict['value'] = char def edit_element(self): self.split_element() if not 'coeff' in self.char_dict: self.char_dict['coeff'] = 1 if not 'value' in self.char_dict: self.char_dict['value'] = 'x' self.char_dict['power'] = 0 else: if not 'power' in self.char_dict: self.char_dict['power'] = 1 def return_monom(self): self.edit_element() self.monom = Monom(int(self.char_dict['coeff']), self.char_dict[ 'value'], int(self.char_dict['power'])) self.monom.derivative() return self.monom
def test_mul_monom(self): a, b, c, d = self.B.gens m_a = Monom((1, 0, 0, 0)) p = a*b + b + c r = p * m_a self.assertEqual(r, a*c)
def test_add_monom(self): a, b, c, d = self.B.gens m_ab = Monom((1, 1, 0, 0)) p = a*b + b + c r = p + m_ab self.assertEqual(r, b + c)
def test_init(self): a, b, c, d = self.B.gens z1 = ZDD(1) self.assertEqual(z1, b) m = Monom((1, 1, 1, 1)) z = ZDD(monom=m) self.assertEqual(z, a*b*c*d)
def test_init(self): abcd = Monom((1, 1, 1, 1)) ab = Monom(vars=[0, 1]) abc = Monom(vars=[0, 1, 2]) c = Monom(vars=[2]) d = Monom(vars=[3]) _0 = Monom() self.assertEqual(abcd, (1, 1, 1, 1)) self.assertEqual(abc, (1, 1, 1, 0)) self.assertEqual(ab, (1, 1, 0, 0)) self.assertEqual(c, (0, 0, 1, 0)) self.assertEqual(d, (0, 0, 0, 1)) self.assertTrue(_0.isZero())
def lm(self): if self.root.isZero(): return Monom.zero elif self.root.isOne(): return Monom.one else: if self._lm is None: monom = [] i = self.root while i.var >= 0: monom.append(i.var) i = i.mul self._lm = Monom(vars=monom) return self._lm
def __init__(self, n, poly_type="list"): Monom.size = n if poly_type == "list": Poly.ring = self self.one = Poly.one self.zero = Poly.zero self.gens = [Poly([Monom(vars=[i])]) for i in range(n)] elif poly_type == "zdd": ZDD.ring = self one = ZDD() one.setOne() zero = ZDD() zero.setZero() self.one = one self.zero = zero self.gens = [ZDD(i) for i in range(n)]
def return_monom(self): self.edit_element() self.monom = Monom(int(self.char_dict['coeff']), self.char_dict[ 'value'], int(self.char_dict['power'])) self.monom.derivative() return self.monom
def test_zero_poly(self): p1 = Poly([Monom([1, 0, 0, 0])]) p2 = Poly([Monom([1, 0, 0, 0])]) self.assertEqual(p1 + p2, Poly.zero)