def testPow(self): f = univar.SortedPolynomial({0: 1, 1: 2, 4: 3}) g = univar.SortedPolynomial({1: -1, 3: 2}) h1 = univar.SortedPolynomial({0: 1, 1: 4, 2: 4, 4: 6, 5: 12, 8: 9}) h2 = univar.SortedPolynomial({3: -1, 5: 6, 7: -12, 9: 8}) self.assertEqual(h1, f.square()) self.assertEqual(h2, g**3)
def testDegree(self): f = univar.BasicPolynomial({1: 2, 4: -1}) self.assertEqual(4, termorder.ascending_order.degree(f)) g = univar.SortedPolynomial([(1, 2), (4, -1)]) self.assertEqual(4, termorder.ascending_order.degree(g)) zero = univar.BasicPolynomial(()) self.assertTrue(termorder.ascending_order.degree(zero) < 0)
def testScalarMul(self): b = univar.BasicPolynomial([(0, 1), (1, 4), (2, 10), (3, 12)]) s = univar.SortedPolynomial([(0, 1), (1, 4), (2, 10), (3, 12)]) d = univar.BasicPolynomial([(0, 2), (1, 8), (2, 20), (3, 24)]) self.assertEqual(d, b * 2) self.assertEqual(d, 2 * b) self.assertEqual(d, s * 2) self.assertEqual(d, 2 * s)
def testInject(self): f = univar.SortedPolynomial([(0, 1), (1, 3), (2, 2)]) X = "X" self.assertTrue(uniutil.VariableProvider not in f.__class__.__bases__) uniutil.inject_variable(f, X) self.assertEqual(X, f.getVariable()) self.assertEqual([X], f.getVariableList()) self.assertTrue(uniutil.VariableProvider in f.__class__.__bases__) self.assertEqual("VarSortedPolynomial", f.__class__.__name__) f += f self.assertEqual("VarSortedPolynomial", f.__class__.__name__) # varname lost self.assertTrue(hasattr(f, "getVariable")) self.assertRaises(AttributeError, f.getVariable)
def testSub(self): f = univar.SortedPolynomial([(1, 2), (4, 3)], True) g = univar.SortedPolynomial([(1, 2), (3, 7)], True) d = univar.SortedPolynomial([(3, -7), (4, 3)], True) self.assertEqual(d, f - g) h1 = univar.SortedPolynomial([(0, 1), (1, 4), (2, 4)]) h2 = univar.SortedPolynomial([(2, 6), (3, 12)]) d2 = univar.SortedPolynomial([(0, 1), (1, 4), (2, -2), (3, -12)]) self.assertEqual(d2, h1 - h2)
def testAdd(self): f = univar.SortedPolynomial([(1, 2), (4, 3)], True) g = univar.SortedPolynomial([(1, -2), (3, 7)], True) s = univar.SortedPolynomial([(3, 7), (4, 3)], True) self.assertEqual(s, f + g) h1 = univar.SortedPolynomial([(0, 1), (1, 4), (2, 4)]) h2 = univar.SortedPolynomial([(2, 6), (3, 12)]) s2 = univar.SortedPolynomial([(0, 1), (1, 4), (2, 10), (3, 12)]) self.assertEqual(s2, h1 + h2)
def testMul(self): s = univar.SortedPolynomial([(0, 1), (1, 4), (2, 10), (3, 12)], True) d = univar.SortedPolynomial([(0, 1), (1, 4), (2, -2), (3, -12)], True) m = univar.SortedPolynomial([ (0, 1), (1, 4), (2, 4) ]).square() - univar.SortedPolynomial([(2, 6), (3, 12)]).square() self.assertEqual(m, s * d) self.assertEqual(m, s.ring_mul(d)) self.assertEqual(m, s.ring_mul_karatsuba(d)) self.assertEqual(m, d * s) self.assertEqual(m, d.ring_mul(s)) self.assertEqual(m, d.ring_mul_karatsuba(s)) s = univar.SortedPolynomial([(0, 1), (1, 4), (2, 10), (3, 12)]) d = univar.SortedPolynomial([(0, 1), (1, 4), (2, -2), (3, -12)]) m = univar.SortedPolynomial([ (0, 1), (1, 4), (2, 4) ]).square() - univar.SortedPolynomial([(2, 6), (3, 12)]).square() self.assertEqual(m, s * d) self.assertEqual(m, s.ring_mul(d)) self.assertEqual(m, s.ring_mul_karatsuba(d)) self.assertEqual(m, d * s) self.assertEqual(m, d.ring_mul(s)) self.assertEqual(m, d.ring_mul_karatsuba(s))
def setUp(self): self.b = univar.BasicPolynomial({0: 1, 1: 4, 2: 4, 4: 6, 5: 12, 8: 9}) self.s = univar.SortedPolynomial({0: 1, 1: 4, 2: 4, 4: 6, 5: 12, 8: 9})
def testSubstitution(self): f = univar.SortedPolynomial([(0, 1), (1, 2), (4, 3)], True) self.assertEqual(45, f(-2))
def testLeadingTerm(self): f = univar.BasicPolynomial({1: 2, 4: -1}) self.assertEqual((4, -1), termorder.ascending_order.leading_term(f)) g = univar.SortedPolynomial([(1, 2), (4, -1)]) self.assertEqual((4, -1), termorder.ascending_order.leading_term(g))
def testLeadingCoefficient(self): f = univar.BasicPolynomial({1: 2, 4: -1}) self.assertEqual(-1, termorder.ascending_order.leading_coefficient(f)) g = univar.SortedPolynomial([(1, 2), (4, -1)]) self.assertEqual(-1, termorder.ascending_order.leading_coefficient(g))
def testFormatSorted(self): g = univar.SortedPolynomial([(1, 2), (4, -1)]) asc_str = "2 * Y - Y ** 4" self.assertEqual(asc_str, termorder.ascending_order.format(g, "Y"))