1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
|
#----------------------------------------------------------------------
#
# math/combinatorics.tcl --
#
# This file contains definitions of mathematical functions
# useful in combinatorial problems.
#
# Copyright (c) 2001, by Kevin B. Kenny. All rights reserved.
#
# See the file "license.terms" for information on usage and redistribution
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
#
# RCS: @(#) $Id: combinatorics.tcl,v 1.5 2004/02/09 19:31:54 hobbs Exp $
#
#----------------------------------------------------------------------
package require Tcl 8.0
namespace eval ::math {
# Commonly used combinatorial functions
# ln_Gamma is spelt thus because it's a capital gamma (\u0393)
namespace export ln_Gamma; # Logarithm of the Gamma function
namespace export factorial; # Factorial
namespace export choose; # Binomial coefficient
# Note that Beta is spelt thus because it's conventionally a
# capital beta (\u0392). It is exported from the package even
# though its name is capitalized.
namespace export Beta; # Beta function
}
#----------------------------------------------------------------------
#
# ::math::InitializeFactorial --
#
# Initialize a table of factorials for small integer arguments.
#
# Parameters:
# None.
#
# Results:
# None.
#
# Side effects:
# The variable, ::math::factorialList, is initialized to hold
# a table of factorial n for 0 <= n <= 170.
#
# This procedure is called once when the 'factorial' procedure is
# being loaded.
#
#----------------------------------------------------------------------
proc ::math::InitializeFactorial {} {
variable factorialList
set factorialList [list 1]
set f 1
for { set i 1 } { $i < 171 } { incr i } {
if { $i > 12. } {
set f [expr { $f * double($i)}]
} else {
set f [expr { $f * $i }]
}
lappend factorialList $f
}
}
#----------------------------------------------------------------------
#
# ::math::InitializePascal --
#
# Precompute the first few rows of Pascal's triangle and store
# them in the variable ::math::pascal
#
# Parameters:
# None.
#
# Results:
# None.
#
# Side effects:
# ::math::pascal is initialized to a flat list containing
# the first 34 rows of Pascal's triangle. C(n,k) is to be found
# at [lindex $pascal $i] where i = n * ( n + 1 ) + k. No attempt
# is made to exploit symmetry.
#
#----------------------------------------------------------------------
proc ::math::InitializePascal {} {
variable pascal
set pascal [list 1]
for { set n 1 } { $n < 34 } { incr n } {
lappend pascal 1
set l2 [list 1]
for { set k 1 } { $k < $n } { incr k } {
set km1 [expr { $k - 1 }]
set c [expr { [lindex $l $km1] + [lindex $l $k] }]
lappend pascal $c
lappend l2 $c
}
lappend pascal 1
lappend l2 1
set l $l2
}
}
#----------------------------------------------------------------------
#
# ::math::ln_Gamma --
#
# Returns ln(Gamma(x)), where x >= 0
#
# Parameters:
# x - Argument to the Gamma function.
#
# Results:
# Returns the natural logarithm of Gamma(x).
#
# Side effects:
# None.
#
# Gamma(x) is defined as:
#
# +inf
# _
# | x-1 -t
# Gamma(x)= _| t e dt
#
# 0
#
# The approximation used here is from Lanczos, SIAM J. Numerical Analysis,
# series B, volume 1, p. 86. For x > 1, the absolute error of the
# result is claimed to be smaller than 5.5 * 10**-10 -- that is, the
# resulting value of Gamma when exp( ln_Gamma( x ) ) is computed is
# expected to be precise to better than nine significant figures.
#
#----------------------------------------------------------------------
proc ::math::ln_Gamma { x } {
# Handle the common case of a real argument that's within the
# permissible range.
if { [string is double -strict $x]
&& ( $x > 0 )
&& ( $x <= 2.5563481638716906e+305 )
} {
set x [expr { $x - 1.0 }]
set tmp [expr { $x + 5.5 }]
set tmp [ expr { ( $x + 0.5 ) * log( $tmp ) - $tmp }]
set ser 1.0
foreach cof {
76.18009173 -86.50532033 24.01409822
-1.231739516 .00120858003 -5.36382e-6
} {
set x [expr { $x + 1.0 }]
set ser [expr { $ser + $cof / $x }]
}
return [expr { $tmp + log( 2.50662827465 * $ser ) }]
}
# Handle the error cases.
if { ![string is double -strict $x] } {
return -code error [expectDouble $x]
}
if { $x <= 0.0 } {
set proc [lindex [info level 0] 0]
return -code error \
-errorcode [list ARITH DOMAIN \
"argument to $proc must be positive"] \
"argument to $proc must be positive"
}
return -code error \
-errorcode [list ARITH OVERFLOW \
"floating-point value too large to represent"] \
"floating-point value too large to represent"
}
#----------------------------------------------------------------------
#
# math::factorial --
#
# Returns the factorial of the argument x.
#
# Parameters:
# x -- Number whose factorial is to be computed.
#
# Results:
# Returns x!, the factorial of x.
#
# Side effects:
# None.
#
# For integer x, 0 <= x <= 12, an exact integer result is returned.
#
# For integer x, 13 <= x <= 21, an exact floating-point result is returned
# on machines with IEEE floating point.
#
# For integer x, 22 <= x <= 170, the result is exact to 1 ULP.
#
# For real x, x >= 0, the result is approximated by computing
# Gamma(x+1) using the ::math::ln_Gamma function, and the result is
# expected to be precise to better than nine significant figures.
#
# It is an error to present x <= -1 or x > 170, or a value of x that
# is not numeric.
#
#----------------------------------------------------------------------
proc ::math::factorial { x } {
variable factorialList
# Common case: factorial of a small integer
if { [string is integer -strict $x]
&& $x >= 0
&& $x < [llength $factorialList] } {
return [lindex $factorialList $x]
}
# Error case: not a number
if { ![string is double -strict $x] } {
return -code error [expectDouble $x]
}
# Error case: gamma in the left half plane
if { $x <= -1.0 } {
set proc [lindex [info level 0] 0]
set message "argument to $proc must be greater than -1.0"
return -code error -errorcode [list ARITH DOMAIN $message] $message
}
# Error case - gamma fails
if { [catch { expr {exp( [ln_Gamma [expr { $x + 1 }]] )} } result] } {
return -code error -errorcode $::errorCode $result
}
# Success - computed factorial n as Gamma(n+1)
return $result
}
#----------------------------------------------------------------------
#
# ::math::choose --
#
# Returns the binomial coefficient C(n,k) = n!/k!(n-k)!
#
# Parameters:
# n -- Number of objects in the sampling pool
# k -- Number of objects to be chosen.
#
# Results:
# Returns C(n,k).
#
# Side effects:
# None.
#
# Results are expected to be accurate to ten significant figures.
# If both parameters are integers and the result fits in 32 bits,
# the result is rounded to an integer.
#
# Integer results are exact up to at least n = 34.
# Floating point results are precise to better than nine significant
# figures.
#
#----------------------------------------------------------------------
proc ::math::choose { n k } {
variable pascal
# Use a precomputed table for small integer args
if { [string is integer -strict $n]
&& $n >= 0 && $n < 34
&& [string is integer -strict $k]
&& $k >= 0 && $k <= $n } {
set i [expr { ( ( $n * ($n + 1) ) / 2 ) + $k }]
return [lindex $pascal $i]
}
# Test bogus arguments
if { ![string is double -strict $n] } {
return -code error [expectDouble $n]
}
if { ![string is double -strict $k] } {
return -code error [expectDouble $k]
}
# Forbid negative n
if { $n < 0. } {
set proc [lindex [info level 0] 0]
set msg "first argument to $proc must be non-negative"
return -code error -errorcode [list ARITH DOMAIN $msg] $msg
}
# Handle k out of range
if { [string is integer -strict $k] && [string is integer -strict $n]
&& ( $k < 0 || $k > $n ) } {
return 0
}
if { $k < 0. } {
set proc [lindex [info level 0] 0]
set msg "second argument to $proc must be non-negative,\
or both must be integers"
return -code error -errorcode [list ARITH DOMAIN $msg] $msg
}
# Compute the logarithm of the desired binomial coefficient.
if { [catch { expr { [ln_Gamma [expr { $n + 1 }]]
- [ln_Gamma [expr { $k + 1 }]]
- [ln_Gamma [expr { $n - $k + 1 }]] } } r] } {
return -code error -errorcode $::errorCode $r
}
# Compute the binomial coefficient itself
if { [catch { expr { exp( $r ) } } r] } {
return -code error -errorcode $::errorCode $r
}
# Round to integer if both args are integers and the result fits
if { $r <= 2147483647.5
&& [string is integer -strict $n]
&& [string is integer -strict $k] } {
return [expr { round( $r ) }]
}
return $r
}
#----------------------------------------------------------------------
#
# ::math::Beta --
#
# Return the value of the Beta function of parameters z and w.
#
# Parameters:
# z, w : Two real parameters to the Beta function
#
# Results:
# Returns the value of the Beta function.
#
# Side effects:
# None.
#
# Beta( w, z ) is defined as:
#
# 1_
# | (z-1) (w-1)
# Beta( w, z ) = Beta( z, w ) = | t (1-t) dt
# _|
# 0
#
# = Gamma( z ) Gamma( w ) / Gamma( z + w )
#
# Results are returned as a floating point number precise to better
# than nine significant figures for w, z > 1.
#
#----------------------------------------------------------------------
proc ::math::Beta { z w } {
# Check form of both args so that domain check can be made
if { ![string is double -strict $z] } {
return -code error [expectDouble $z]
}
if { ![string is double -strict $w] } {
return -code error [expectDouble $w]
}
# Check sign of both args
if { $z <= 0.0 } {
set proc [lindex [info level 0] 0]
set msg "first argument to $proc must be positive"
return -code error -errorcode [list ARITH DOMAIN $msg] $msg
}
if { $w <= 0.0 } {
set proc [lindex [info level 0] 0]
set msg "second argument to $proc must be positive"
return -code error -errorcode [list ARITH DOMAIN $msg] $msg
}
# Compute beta using gamma function, keeping stack trace clean.
if { [catch { expr { exp( [ln_Gamma $z] + [ln_Gamma $w]
- [ln_Gamma [ expr { $z + $w }]] ) } } beta] } {
return -code error -errorcode $::errorCode $beta
}
return $beta
}
#----------------------------------------------------------------------
#
# Initialization of this file:
#
# Initialize the precomputed tables of factorials and binomial
# coefficients.
#
#----------------------------------------------------------------------
namespace eval ::math {
InitializeFactorial
InitializePascal
}
|