Ticket #11411: trac_11411-q_analogues-review-fh.patch

File trac_11411-q_analogues-review-fh.patch, 4.4 KB (added by hivert, 9 years ago)
  • sage/combinat/q_analogues.py

    # HG changeset patch
    # User Florent Hivert <Florent.Hivert@univ-rouen.fr>
    # Date 1307033169 -7200
    # Node ID 6bba477a7ee07b745377a1e36550973e1af25642
    # Parent  277fdbc7153644352144183714aca01787d69e53
    #11411: Review patch for q-analogue fix.
    
    diff --git a/sage/combinat/q_analogues.py b/sage/combinat/q_analogues.py
    a b from dyck_word import DyckWords 
    2323def q_int(n, p=None):
    2424    """
    2525    Returns the ``q``-analogue of the integer ``n``.
    26    
     26
    2727    If ``p`` is unspecified, then it defaults to using the generator ``q`` for
    2828    a univariate polynomial ring over the integers.
    29    
     29
    3030    EXAMPLES::
    31    
     31
    3232        sage: import sage.combinat.q_analogues as q_analogues
    3333        sage: q_analogues.q_int(3)
    3434        q^2 + q + 1
    3535        sage: p = ZZ['p'].0
    3636        sage: q_analogues.q_int(3,p)
    3737        p^2 + p + 1
     38
     39    The ``q``-analogue of ``n`` is only defined for ``n`` a nonnegative
     40    integer (trac #11411)::
     41
     42        sage: q_analogues.q_int(-2)
     43        Traceback (most recent call last):
     44        ...
     45        ValueError: Argument (-2) must be a nonnegative integer.
    3846    """
    3947    if n in ZZ and n >= 0:
    4048        if p == None:
    def q_int(n, p=None): 
    4654def q_factorial(n, p=None):
    4755    """
    4856    Returns the ``q``-analogue of the factorial ``n!``.
    49    
     57
    5058    If ``p`` is unspecified, then it defaults to using the generator ``q`` for
    5159    a univariate polynomial ring over the integers.
    52    
     60
    5361    EXAMPLES::
    54    
     62
    5563        sage: import sage.combinat.q_analogues as q_analogues
    5664        sage: q_analogues.q_factorial(3)
    5765        q^3 + 2*q^2 + 2*q + 1
    5866        sage: p = ZZ['p'].0
    5967        sage: q_analogues.q_factorial(3, p)
    6068        p^3 + 2*p^2 + 2*p + 1
     69
     70    The ``q``-analogue of ``n!`` is only defined for ``n`` a nonnegative
     71    integer (trac #11411)::
     72
     73        sage: q_analogues.q_factorial(-2)
     74        Traceback (most recent call last):
     75        ...
     76        ValueError: Argument (-2) must be a nonnegative integer.
    6177    """
    6278    if n in ZZ and n >= 0:
    6379        return prod([q_int(i, p) for i in range(1, n+1)])
    def q_factorial(n, p=None): 
    6783def q_binomial(n,k,p=None):
    6884    """
    6985    Returns the ``q``-binomial coefficient.
    70    
     86
    7187    If ``p`` is unspecified, then it defaults to using the generator ``q`` for
    7288    a univariate polynomial ring over the integers.
    73    
     89
    7490    EXAMPLES::
    75    
     91
    7692        sage: import sage.combinat.q_analogues as q_analogues
    7793        sage: q_analogues.q_binomial(4,2)
    7894        q^4 + q^3 + 2*q^2 + q + 1
    7995        sage: p = ZZ['p'].0
    8096        sage: q_analogues.q_binomial(4,2,p)
    8197        p^4 + p^3 + 2*p^2 + p + 1
     98
     99    The ``q``-analogue of ``binomial(n,k)`` is currently only defined for
     100    ``n`` a nonnegative integer, it is zero for negative k  (trac #11411)::
     101
     102        sage: q_analogues.q_binomial(5, -1)
     103        0
    82104    """
    83     if n in ZZ and n >= 0 and k in ZZ:
    84         if 0 <= k and k <= n:
    85             k=min(k, n-k)
    86             return prod(q_int(j, p) for j in range(n-k+1, n+1)) / prod(q_int(j, p) for j in range(1, k+1))
    87         else:
    88             return 0
     105    if not (n in ZZ and k in ZZ):
     106        raise ValueError, "Argument (%s, %s) must be integers."%(n, k)
     107    if n < 0:
     108        raise NotImplementedError
     109    if 0 <= k and k <= n:
     110        k=min(k, n-k)
     111        return (prod(q_int(j, p) for j in range(n-k+1, n+1)) /
     112                prod(q_int(j, p) for j in range(1, k+1)))
    89113    else:
    90         raise NotImplementedError
     114        return 0
    91115
    92116def q_catalan_number(n,p=None):
    93117    """
    def q_catalan_number(n,p=None): 
    107131        sage: p = ZZ['p'].0
    108132        sage: q_analogues.q_catalan_number(4,p)
    109133        p^12 + p^10 + p^9 + 2*p^8 + p^7 + 2*p^6 + p^5 + 2*p^4 + p^3 + p^2 + 1
     134
     135    The ``q``-Catalan number of index ``n`` is only defined for ``n`` a
     136    nonnegative integer (trac #11411)::
     137
     138        sage: q_analogues.q_catalan_number(-2)
     139        Traceback (most recent call last):
     140        ...
     141        ValueError: Argument (-2) must be a nonnegative integer.
    110142    """
    111143    if n in ZZ and n >= 0:
    112144        return prod(q_int(j, p) for j in range(n+2, 2*n+1)) / prod(q_int(j, p) for j in range(2,n+1))
    def q_catalan_number(n,p=None): 
    116148def qt_catalan_number(n):
    117149    """
    118150    Returns the ``q,t``-Catalan number of index ``n``.
    119    
     151
    120152    EXAMPLES::
    121    
     153
    122154        sage: import sage.combinat.q_analogues as q_analogues
    123155        sage: q_analogues.qt_catalan_number(1)
    124156        1