ISO module for binomial coefficients, etc.

Peter Pearson ppearson at nowhere.invalid
Thu Jan 28 05:33:13 CET 2010


On Sun, 24 Jan 2010 16:11:03 -0500, Dave Angel <davea at ieee.org> wrote:

> I didn't think of simply summing the logs. 

A couple terms of Stirling's approximation work pretty well:

def log_fact_half( N ):
  """log_fact_half( n ) returns the natural logarithm of the factorial of n/2.
  n need not be an integer.
  Domain: n from 0 to infinity
  Range: from something around -0.12 to infinity
  """
  
  SmallFacts = (
		0.0000000000000000, 
		-0.1207822376352453, 
		0.0000000000000000, 
		0.2846828704729192, 
		0.6931471805599453, 
		1.2009736023470743,
		1.7917594692280550, 
		2.4537365708424423, 
		3.1780538303479458, 
		3.9578139676187165, 
		4.7874917427820458, 
		5.6625620598571418, 
		6.5792512120101012, 
		7.5343642367587327, 
		8.5251613610654147, 
		9.5492672573009969, 
		10.6046029027452509, 
		11.6893334207972686,
		12.8018274800814691, 
		13.9406252194037634  )

  if N < 0:
    raise RuntimeError, \
	  "Negative argument in LogHalfFact!"

  if N < len( SmallFacts ):
    RetVal = SmallFacts[ N ]
  else:
    n = 0.5 * N
    RetVal = (n+0.5)*math.log(n) - n + HALFLOG2PI + 1.0/(12*n) - \
	     1.0/(360*n*n*n)
  return RetVal



-- 
To email me, substitute nowhere->spamcop, invalid->net.



More information about the Python-list mailing list