# HG changeset patch
# User Florent Hivert <Florent.Hivert@univrouen.fr>
# Date 1307033169 7200
# Node ID 6bba477a7ee07b745377a1e36550973e1af25642
# Parent 277fdbc7153644352144183714aca01787d69e53
#11411: Review patch for qanalogue fix.
diff git a/sage/combinat/q_analogues.py b/sage/combinat/q_analogues.py
a

b

from dyck_word import DyckWords 
23  23  def q_int(n, p=None): 
24  24  """ 
25  25  Returns the ``q``analogue of the integer ``n``. 
26   
 26  
27  27  If ``p`` is unspecified, then it defaults to using the generator ``q`` for 
28  28  a univariate polynomial ring over the integers. 
29   
 29  
30  30  EXAMPLES:: 
31   
 31  
32  32  sage: import sage.combinat.q_analogues as q_analogues 
33  33  sage: q_analogues.q_int(3) 
34  34  q^2 + q + 1 
35  35  sage: p = ZZ['p'].0 
36  36  sage: q_analogues.q_int(3,p) 
37  37  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. 
38  46  """ 
39  47  if n in ZZ and n >= 0: 
40  48  if p == None: 
… 
… 
def q_int(n, p=None): 
46  54  def q_factorial(n, p=None): 
47  55  """ 
48  56  Returns the ``q``analogue of the factorial ``n!``. 
49   
 57  
50  58  If ``p`` is unspecified, then it defaults to using the generator ``q`` for 
51  59  a univariate polynomial ring over the integers. 
52   
 60  
53  61  EXAMPLES:: 
54   
 62  
55  63  sage: import sage.combinat.q_analogues as q_analogues 
56  64  sage: q_analogues.q_factorial(3) 
57  65  q^3 + 2*q^2 + 2*q + 1 
58  66  sage: p = ZZ['p'].0 
59  67  sage: q_analogues.q_factorial(3, p) 
60  68  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. 
61  77  """ 
62  78  if n in ZZ and n >= 0: 
63  79  return prod([q_int(i, p) for i in range(1, n+1)]) 
… 
… 
def q_factorial(n, p=None): 
67  83  def q_binomial(n,k,p=None): 
68  84  """ 
69  85  Returns the ``q``binomial coefficient. 
70   
 86  
71  87  If ``p`` is unspecified, then it defaults to using the generator ``q`` for 
72  88  a univariate polynomial ring over the integers. 
73   
 89  
74  90  EXAMPLES:: 
75   
 91  
76  92  sage: import sage.combinat.q_analogues as q_analogues 
77  93  sage: q_analogues.q_binomial(4,2) 
78  94  q^4 + q^3 + 2*q^2 + q + 1 
79  95  sage: p = ZZ['p'].0 
80  96  sage: q_analogues.q_binomial(4,2,p) 
81  97  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 
82  104  """ 
83   if n in ZZ and n >= 0 and k in ZZ: 
84   if 0 <= k and k <= n: 
85   k=min(k, nk) 
86   return prod(q_int(j, p) for j in range(nk+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, nk) 
 111  return (prod(q_int(j, p) for j in range(nk+1, n+1)) / 
 112  prod(q_int(j, p) for j in range(1, k+1))) 
89  113  else: 
90   raise NotImplementedError 
 114  return 0 
91  115  
92  116  def q_catalan_number(n,p=None): 
93  117  """ 
… 
… 
def q_catalan_number(n,p=None): 
107  131  sage: p = ZZ['p'].0 
108  132  sage: q_analogues.q_catalan_number(4,p) 
109  133  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. 
110  142  """ 
111  143  if n in ZZ and n >= 0: 
112  144  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): 
116  148  def qt_catalan_number(n): 
117  149  """ 
118  150  Returns the ``q,t``Catalan number of index ``n``. 
119   
 151  
120  152  EXAMPLES:: 
121   
 153  
122  154  sage: import sage.combinat.q_analogues as q_analogues 
123  155  sage: q_analogues.qt_catalan_number(1) 
124  156  1 