Changeset 5505:c1f9b9974b73
- Timestamp:
- 07/25/07 16:01:29 (6 years ago)
- Branch:
- default
- File:
-
- 1 edited
-
sage/combinat/combinat.py (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
sage/combinat/combinat.py
r5438 r5505 204 204 from sage.libs.all import pari 205 205 206 import expnums 207 206 208 ######### combinatorial sequences 207 209 208 def bell_number(n ):210 def bell_number(n, algorithm='sage'): 209 211 r""" 210 212 Returns the n-th Bell number (the number of ways to partition a … … 213 215 INPUT: 214 216 n -- an integer 217 algorithm -- 'sage': use N. Alexander's custom implementation in SAGE 218 'gap': use Gap's Bell command (slow) 215 219 216 220 If $n \leq 0$, returns $1$. 217 218 Wraps GAP's Bell. 221 219 222 220 223 EXAMPLES: 221 224 sage: bell_number(10) 225 115975 226 sage: bell_number(10, algorithm='gap') 222 227 115975 223 228 sage: bell_number(2) … … 231 236 ... 232 237 TypeError: no coercion of this rational to integer 233 """ 234 ans=gap.eval("Bell(%s)"%ZZ(n)) 235 return eval(ans) 238 239 To compute all Bell numbers up to n, use expnums: 240 sage: expnums(10,1) 241 [1, 1, 2, 5, 15, 52, 203, 877, 4140, 21147] 242 sage: [bell_number(n) for n in range(10)] 243 [1, 1, 2, 5, 15, 52, 203, 877, 4140, 21147] 244 """ 245 n = ZZ(n) 246 if n <= 0: 247 return ZZ(1) 248 if algorithm == 'sage': 249 return expnums.expnums(n+1,1)[-1] 250 elif algorithm == 'gap': 251 return ZZ(gap.eval("Bell(%s)"%ZZ(n))) 252 else: 253 raise ValueError, "unknown algorithm '%s'"%algorithm 236 254 237 255 ## def bernoulli_number(n):
Note: See TracChangeset
for help on using the changeset viewer.
