source: trunk/misc/tools/pytools/combinatorics.py @ 8813

Last change on this file since 8813 was 7276, checked in by ole, 15 years ago

Merged numpy branch back into the trunk.

In ~/sandpit/anuga/anuga_core/source
svn merge -r 6246:HEAD ../../branches/numpy .

In ~/sandpit/anuga/anuga_validation
svn merge -r 6417:HEAD ../branches/numpy_anuga_validation .

In ~/sandpit/anuga/misc
svn merge -r 6809:HEAD ../branches/numpy_misc .

For all merges, I used numpy version where conflicts existed

The suites test_all.py (in source/anuga) and validate_all.py passed using Python2.5 with numpy on my Ubuntu Linux box.

File size: 769 bytes
Line 
1"""
2  Combinatorial function using NumPy
3  OMN, 2001
4""" 
5       
6import numpy as num
7
8def factorial(n):
9  v = num.arange(n)+1
10  return num.multiply.reduce(v)
11
12def binom (n,k):
13  """ Compute the binomial coefficient n!/(k! (n-k)!)
14  """
15 
16  if k > n or n < 0 or k < 0:
17    raise Exception
18  elif k==n or n==0 or k==0:
19    return(1)
20  else:
21    v = num.arange(k) + n-k+1 #[n-k+1, ..., n-1, n]
22    nom = num.multiply.reduce(v)
23    denom = factorial(k)
24   
25    return nom/denom
26
27def bitvector(i, n):
28    """Compute binary representation of positive integer i in a word of size n
29    """
30   
31    b=[0]*n
32    k=0
33    while i > 0:
34        d = i%2
35        b[k] = d
36        i = i/2
37        k = k+1
38        if k > n: break
39    return b
40
41
Note: See TracBrowser for help on using the repository browser.