def bell(n): if n < 0: raise ValueError('Bell polynomials only defined for n >= 0') if n == 0: return ONE if n == 1: return x return sum(stirling2(n, k) * x ** k for k in xrange(n + 1))
def testStirling2(self): py.test.raises(ValueError, lambda: funcs.stirling2(1, -1)) assert funcs.stirling2(0, 0) == 1 assert funcs.stirling2(2, 1) == 1 assert funcs.stirling2(12, 3) == 86526 assert funcs.stirling2(13, 13) == 1 assert funcs.stirling2(13, 12) == 78
def touchard(n): ''' Returns the *n-th* Touchard polynomial in ``x``. :rtype: :class:`pypol.Polynomial` **Examples** :: >>> touchard(0) + 1 >>> touchard(1) + x >>> touchard(2) + x^2 + x >>> touchard(12) + x^12 + 66x^11 + 7498669301432319/4398046511104x^10 + 22275x^9 + 159027x^8 + 627396x^7 + 1323652x^6 + 1379400x^5 + 611501x^4 + 86526x^3 + 2047x^2 + x The Touchard polynomials also satisfy: :math:`T_n(1) = B_n` where :math:`B_n` is the *n-th* Bell number (:func:`funcs.bell_num`):: >>> long(touchard(19)(1)) == long(bell_num(19)) True The more *n* become greater, the more it loses precision:: >>> long(touchard(23)(1)) == long(bell_num(23)) False >>> abs(long(touchard(23)(1)) - long(bell_num(23))) 8L >>> long(touchard(45)(1)) == long(bell_num(45)) False >>> abs(long(touchard(45)(1)) - long(bell_num(45))) 19342813113834066795298816L >>> long(touchard(123)(1)) == long(bell_num(123)) False >>> abs(long(touchard(123)(1)) - long(bell_num(123))) 429106803807439187983719223678319701219747465049443431177466446916319867062128867811451292833717675081551803755143128885524389827706879L ''' if n < 0: return NULL if n == 0: return ONE return sum(stirling2(n, k) * x ** k for k in xrange(n + 1))