Esempio n. 1
0
    def __pow__(self, exp):
        if not isinstance(exp, int):
            raise TypeError("Exponent must be an integer.")
        elif exp < 0:
            return 1 / (self ** (-exp))
        elif exp == 0:
            return Polynomial("1")
        elif not self:
            return Polynomial()
        elif self.is_monomial():
            m = list(self.monomials[0])
            m[0] = m[0] ** exp
            m[1] = {l: e * exp for l, e in iteritems(m[1])}
            return Polynomial.from_iterable([m])
        elif self.is_binomial():
            a = Polynomial.from_iterable([self._monomials[0]])
            b = Polynomial.from_iterable([self._monomials[1]])
            return sum(coeff * a ** (exp - k) * b ** k
                       for k, coeff in enumerate(bin_coeffs(exp)))

        return functools.reduce(operator.mul, [self] * exp)
Esempio n. 2
0
    def test_bin_coeffs(self):
        g1 = bin_coeffs(4)
        g2 = bin_coeffs(n=5, start=1, stop=4, step=3)
        g3 = bin_coeffs(3, 1, 3, 2)
        g4 = bin_coeffs(4, 4, 1)
        g5 = bin_coeffs(5, 4, 0, -2)
        g6 = bin_coeffs(4)
        g7 = bin_coeffs(5, 0, 3)
        g8 = bin_coeffs(5, 2, 3)

        self.assertEqual(g1.next(), 1)
        self.assertEqual(g1.send(3), 4)
        self.assertEqual(g1.send(0), 1)
        self.assertEqual(list(g1), [4, 6, 4, 1])
        self.assertIsInstance(g1, types.GeneratorType)

        self.assertEqual(g2.next(), 5)
        self.assertEqual(g2.next(), 5)
        self.assertEqual(g2.send(3), 10)
        self.assertRaises(StopIteration, g2.next)
        self.assertIsInstance(g2, types.GeneratorType)

        self.assertEqual(list(g3), [3, 1])
        self.assertIsInstance(g3, types.GeneratorType)
        g3 = bin_coeffs(3, 0, 3, 2)
        self.assertEqual(list(g3), [1, 3])

        self.assertEqual(list(g4), [1, 4, 6, 4])

        self.assertEqual(list(g5), [5, 10, 1])

        g6.next()
        g7.next()
        g8.next()

        self.assertRaises(TypeError, g6.send, [])
        g6 = bin_coeffs(4)
        g6.next()

        self.assertRaises(TypeError, g6.send, {})
        g6 = bin_coeffs(4)
        g6.next()

        self.assertRaises(TypeError, g6.send, 1.5)
        g6 = bin_coeffs(4)
        g6.next()

        self.assertRaises(ValueError, g6.send, -1)
        g6 = bin_coeffs(4)
        g6.next()

        self.assertRaises(ValueError, g6.send, 5)
        g6 = bin_coeffs(4)
        g6.next()

        self.assertRaises(ValueError, g7.send, 4)
        g7 = bin_coeffs(5, 0, 3)
        g7.next()

        self.assertRaises(ValueError, g8.send, 1)

        self.assertRaises(TypeError, bin_coeffs, 4, 1, 4, 5, 1)
        self.assertRaises(TypeError, bin_coeffs, pippo=1)
        self.assertRaises(TypeError, lambda x: bin_coeffs(x).next(), 1.5)
        self.assertRaises(TypeError, lambda x: bin_coeffs(x).next(), [])
        self.assertRaises(TypeError, lambda x: bin_coeffs(x).next(), {})
        self.assertRaises(TypeError, lambda x: bin_coeffs(x).next())

        self.assertRaises(ValueError, lambda x, y: bin_coeffs(x, y).next(),
                          4, 5)
        self.assertRaises(ValueError,
                          lambda x, y, z, t: bin_coeffs(x, y, z, t).next(),
                          4, 1, 6, 2)
        self.assertRaises(ValueError,
                          lambda x, y, z, t: bin_coeffs(x, y, z, t).next(),
                          4, 1, 4, 0)
        self.assertRaises(ValueError,
                          lambda x, y, z: bin_coeffs(x, y, z).next(),
                          4, -1, 4)