""" Combinatorial function using NumPy OMN, 2001 """ import Numeric def factorial(n): v = Numeric.arange(n)+1 return Numeric.multiply.reduce(v) def binom (n,k): """ Compute the binomial coefficient n!/(k! (n-k)!) """ if k > n or n < 0 or k < 0: raise Exception elif k==n or n==0 or k==0: return(1) else: v = Numeric.arange(k) + n-k+1 #[n-k+1, ..., n-1, n] nom = Numeric.multiply.reduce(v) denom = factorial(k) return nom/denom def bitvector(i, n): """Compute binary representation of positive integer i in a word of size n """ b=[0]*n k=0 while i > 0: d = i%2 b[k] = d i = i/2 k = k+1 if k > n: break return b