Fun end of day mental exercise...
Test function matches Excel to 5 decimal places (vs. built in functions GAMMALN & GAMMA, decimal factorial computed from X*Gamma(X))
and in range of 0.1 to 10.0 in 0.1 increments
(but maybe Excel is wrong too...)
(defun C:TestGamma
( / CNT MyVal LG G DF
) ; Test function for 'LnGamma', 'Gamma', and 'DecimalFact'
; KJM - April 2024
; Uses custom functions
; lngamma
; gamma
; decimalfact
(prompt "\nValue LnGamma Gamma DecFact")
; Test from 0.1 to 10.0
(setq LG
(lngamma MyVal
)) (setq DF
(DecimalFact MyVal
))
; Report
) ; close repeat
)
OutVal tol
)
; Decimal factorial function x! = x*gamma(x)
; KJM - April 2024
; Input
; X - (positive real)
; Returns
; Natural logarithm of gamma function
; Uses custom functions:
; Gamma
(if (> X tol
) ; Must by positive (setq OutVal
(* X
(gamma X
))) ) ; close if
OutVal
)
OutVal tol
)
; Gamma function
; KJM - April 2024
; Input
; X - (positive real)
; Returns
; gamma function of X
; Uses custom functions:
; LnGamma
(if (> X tol
) ; Must by positive ) ; close if
OutVal
)
OutVal tol Coeff B A CNT
)
; Natural Log of Gamma function by Lanczos approximation
; KJM - April 2024
; Input
; X - (positive real)
; Returns
; Natural logarithm of gamma function of X
; Uses custom functions:
; none!
; See:
; https://en.wikipedia.org/wiki/Lanczos_approximation
(if (> X tol
) ; Must by positive
(setq Coeff
(list ; list of 7 coefficients 1.000000000190015
76.180091729471463
-86.505320329416768
24.014098240830910
-1.231739572450155
0.001208650973866
-0.000005395239385
)) ; close setq list
; Reflection formula for small X
(LnGamma (- 1.0 X))
))
)
) ; close repeat
(* -1.0 B)
))
)
) ; close if
)
) ; close if
OutVal
)