def testReduce(self): F = (multiutil.polynomial( { (2, 0): rational.Integer(1), (1, 2): rational.Integer(2) }, rational.theRationalField, 2), multiutil.polynomial( { (1, 1): rational.Integer(1), (0, 3): rational.Integer(2), (0, 0): rational.Integer(-1) }, rational.theRationalField, 2)) rgb_expected = [ multiutil.polynomial({(1, 0): rational.Integer(1)}, rational.theRationalField, 2), multiutil.polynomial( { (0, 3): rational.Integer(1), (0, 0): rational.Rational(-1, 2) }, rational.theRationalField, 2) ] G_from_F = groebner.normal_strategy(F, self.lex) rgb = groebner.reduce_groebner(G_from_F, self.lex) self.assertEqual(2, len(rgb)) self.assertEqual(rgb_expected, rgb)
def testBuchberger(self): """ test Buchberger's algorithm. http://www.geocities.com/famancin/buchberger.html """ F = (multiutil.polynomial( { (2, 0): rational.Integer(1), (1, 2): rational.Integer(2) }, rational.theRationalField, 2), multiutil.polynomial( { (1, 1): rational.Integer(1), (0, 3): rational.Integer(2), (0, 0): rational.Integer(-1) }, rational.theRationalField, 2)) G_from_F = groebner.buchberger(F, self.lex) G_expected = list(F + (multiutil.polynomial( {(1, 0): rational.Integer(1)}, rational.theRationalField, 2), multiutil.polynomial( { (0, 3): rational.Integer(2), (0, 0): rational.Integer(-1) }, rational.theRationalField, 2))) self.assertEqual(len(G_expected), len(G_from_F)) for p, q in zip(G_expected, G_from_F): self.assertEqualUptoUnit(p, q)
def testNormalStrategy(self): """ test Buchberger's algorithm (normal strategy). same example with testBuchberger. """ F = (multiutil.polynomial( { (2, 0): rational.Integer(1), (1, 2): rational.Integer(2) }, rational.theRationalField, 2), multiutil.polynomial( { (1, 1): rational.Integer(1), (0, 3): rational.Integer(2), (0, 0): rational.Integer(-1) }, rational.theRationalField, 2)) G_from_F = groebner.normal_strategy(F, self.lex) G_expected = list(F + (multiutil.polynomial( {(1, 0): rational.Integer(1)}, rational.theRationalField, 2), multiutil.polynomial( { (0, 3): rational.Integer(2), (0, 0): rational.Integer(-1) }, rational.theRationalField, 2))) self.assertEqual(len(G_expected), len(G_from_F)) for p, q in zip(G_expected, G_from_F): self.assertEqualUptoUnit(p, q)
def __mul__(self, other): """ Output composite field of self and other. """ common_options = {"coeffring": rational.theIntegerRing, "number_of_variables": 2} flist = [((d, 0), c) for (d, c) in enumerate(self.polynomial)] f = multiutil.polynomial(flist, **common_options) g = zpoly(other.polynomial) diff = multiutil.polynomial({(1, 0): 1, (0, 1):-1}, **common_options) compos = f.resultant(g(diff), 0) return NumberField([compos[i] for i in range(compos.degree() + 1)])
def _zero_polynomial(self): """ Return the zero polynomial in the polynomial ring. """ if self.number_of_variables == 1: import nzmath.poly.uniutil as uniutil return uniutil.polynomial((), self._coefficient_ring) else: import nzmath.poly.multiutil as multiutil return multiutil.polynomial((), coeffring=self._coefficient_ring, number_of_variables=self.number_of_variables)
def _prepared_polynomial(self, preparation): """ Return a polynomial from given preparation, which is suited for the first argument of uni-/multi-variable polynomials. """ if self.number_of_variables == 1: import nzmath.poly.uniutil as uniutil return uniutil.polynomial(preparation, self._coefficient_ring) else: import nzmath.poly.multiutil as multiutil return multiutil.polynomial(preparation, self._coefficient_ring)
def testXYZ(self): ctx = {} multiutil.prepare_indeterminates("S T X Y", ctx) self.assertTrue("X" in ctx) for var in ctx: exec "self.%s = ctx['%s']" % (var, var) self.assertTrue(self.S) self.assertTrue(self.T) Z = rational.theIntegerRing self.XY = multiutil.polynomial({(0, 0, 1, 1): 1}, Z) self.assertEqual(self.XY, self.X * self.Y)
def _constant_polynomial(self, seed): """ Return a constant polynomial made from a constant seed. seed should not be zero. """ if self.number_of_variables == 1: import nzmath.poly.uniutil as uniutil return uniutil.polynomial({0: seed}, self._coefficient_ring) else: import nzmath.poly.multiutil as multiutil const = (0,) * self.number_of_variables return multiutil.polynomial({const: seed}, self._coefficient_ring)
def testZero(self): zero = multiutil.polynomial({}, self.Z, 2) self.assertEqual(zero, self.Z2.zero)
def testOne(self): one = multiutil.polynomial({(0, 0): 1}, self.Z) self.assertEqual(one, self.Z2.one)
def testCreateElement(self): """ createElement method is tweaked in uniutil. """ one = multiutil.polynomial({(0, 0): 1}, self.Z) self.assertEqual(one, self.Z2.createElement(1))