# Ticket #2578: trac-2578.patch

File trac-2578.patch, 2.2 KB (added by craigcitro, 14 years ago)
• ## sage/combinat/combinat.py

# HG changeset patch
# User Craig Citro <craigcitro@gmail.com>
# Date 1232700825 28800
# Node ID 4a893db47087376304bd6d9732a10fb35d6929f7
# Parent  f8fb0bf3e547d65a0f90f5b27d543c8cf4ca9da0
Fix trac #2578 (avoid Maxima for bernoulli polynomial).

diff -r f8fb0bf3e547 -r 4a893db47087 sage/combinat/combinat.py
 a #***************************************************************************** from sage.interfaces.all import gap, maxima from sage.rings.all import QQ, ZZ from sage.rings.arith import bernoulli, binomial from sage.rings.polynomial.polynomial_element import Polynomial from sage.misc.sage_eval import sage_eval from sage.libs.all import pari from sage.misc.prandom import randint def bernoulli_polynomial(x,n): r""" Return the nth Bernoulli polynomial as a polynomial in x. In particular, if x is anything other than a variable, this will simply be the nth Bernoulli polynomial evaluated at x. The generating function for the Bernoulli polynomials is $\frac{t e^{xt}}{e^t-1}= \sum_{n=0}^\infty B_n(x) \frac{t^n}{n!}. \frac{t e^{xt}}{e^t-1}= \sum_{n=0}^\infty B_n(x) \frac{t^n}{n!},$ and they are given directly by $B_n(x) = \sum_{i=0}^n \binomial{n}{i}B_{n-i}x^i.$ One has $B_n(x) = - n\zeta(1 - n,x)$, where $\zeta(s,x)$ is the sage: y = QQ['y'].0 sage: bernoulli_polynomial(y,5) y^5 - 5/2*y^4 + 5/3*y^3 - 1/6*y sage: bernoulli_polynomial(y,5)(12) 199870 sage: bernoulli_polynomial(12,5) 199870 REFERENCES: http://en.wikipedia.org/wiki/Bernoulli_polynomials """ return sage_eval(maxima.eval("bernpoly(x,%s)"%n), {'x':x}) try: n = ZZ(n) except TypeError: raise TypeError, "second argument must be an integer" if isinstance(x, Polynomial): return x.parent()([ binomial(n,i) * bernoulli(n-i) for i in range(n+1) ]) else: return sum([ binomial(n,i) * bernoulli(i) * x**(n-i) for i in range(n+1) ])