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 b  
    190190#*****************************************************************************
    191191from sage.interfaces.all import gap, maxima
    192192from sage.rings.all import QQ, ZZ
     193from sage.rings.arith import bernoulli, binomial
     194from sage.rings.polynomial.polynomial_element import Polynomial
    193195from sage.misc.sage_eval import sage_eval
    194196from sage.libs.all import pari
    195197from sage.misc.prandom import randint
     
    21062108
    21072109def bernoulli_polynomial(x,n):
    21082110    r"""
     2111    Return the nth Bernoulli polynomial as a polynomial in x. In
     2112    particular, if x is anything other than a variable, this will
     2113    simply be the nth Bernoulli polynomial evaluated at x.
     2114   
    21092115    The generating function for the Bernoulli polynomials is
    21102116    \[
    2111      \frac{t e^{xt}}{e^t-1}= \sum_{n=0}^\infty B_n(x) \frac{t^n}{n!}.
     2117     \frac{t e^{xt}}{e^t-1}= \sum_{n=0}^\infty B_n(x) \frac{t^n}{n!},
     2118    \]
     2119    and they are given directly by
     2120    \[
     2121     B_n(x) = \sum_{i=0}^n \binomial{n}{i}B_{n-i}x^i.
    21122122    \]
    21132123
    21142124    One has $B_n(x) = - n\zeta(1 - n,x)$, where $\zeta(s,x)$ is the
     
    21192129        sage: y = QQ['y'].0
    21202130        sage: bernoulli_polynomial(y,5)
    21212131        y^5 - 5/2*y^4 + 5/3*y^3 - 1/6*y
     2132        sage: bernoulli_polynomial(y,5)(12)
     2133        199870
     2134        sage: bernoulli_polynomial(12,5)
     2135        199870
    21222136
    21232137    REFERENCES:
    21242138        http://en.wikipedia.org/wiki/Bernoulli_polynomials
    21252139    """
    2126     return sage_eval(maxima.eval("bernpoly(x,%s)"%n), {'x':x})
     2140    try:
     2141        n = ZZ(n)
     2142    except TypeError:
     2143        raise TypeError, "second argument must be an integer"
     2144
     2145    if isinstance(x, Polynomial):
     2146        return x.parent()([ binomial(n,i) * bernoulli(n-i) for i in range(n+1) ])
     2147    else:
     2148        return sum([ binomial(n,i) * bernoulli(i) * x**(n-i) for i in range(n+1) ])