Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
""" Fast computation of combinatorial functions (Cython + mpz).
Currently implemented: - Stirling numbers of the second kind
AUTHORS: - Fredrik Johansson (2010-10): Stirling numbers of second kind
"""
from cysignals.memory cimport check_allocarray, sig_free
from sage.libs.gmp.all cimport * from sage.rings.integer cimport Integer
cdef void mpz_addmul_alt(mpz_t s, mpz_t t, mpz_t u, unsigned long parity): """ Set s = s + t*u * (-1)^parity """ else:
cdef mpz_stirling_s2(mpz_t s, unsigned long n, unsigned long k): """ Set s = S(n,k) where S(n,k) denotes a Stirling number of the second kind.
Algorithm: S(n,k) = (sum_{j=0}^k (-1)^(k-j) C(k,j) j^n) / k!
TODO: compute S(n,k) efficiently for large n when n-k is small (e.g. when k > 20 and n-k < 20) """ cdef mpz_t t, u cdef mpz_t *bc cdef unsigned long j, max_bc # Some important special cases # Upper triangle of n\k table # S(n,n-1) = C(n,2) # Leftmost three columns of n\k table elif k == 1: elif k == 2: # 2^(n-1)-1 # Direct sequential evaluation of the sum # Use the fact that C(k,j) = C(k,k-j) # Last term not included because loop starts from 1 # Only compute odd powers, saving about half of the time for large n. # We need to precompute binomial coefficients since they will be accessed # out of order, adding overhead that makes this slower for small n. else: # Process each 2^p * j, where j is odd else:
def _stirling_number2(n, k): """ Python wrapper of mpz_stirling_s2.
sage: from sage.combinat.combinat_cython import _stirling_number2 sage: _stirling_number2(3, 2) 3
This is wrapped again by stirling_number2 in combinat.py. """ |