
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Calculate Logarithm Gamma of a Given Number in Haskell
In Haskell, we will calculate the logarithm gamma of the given number by using Stirling's approximation and Lanczos appromixation formula. In the first example, we are going to use Stirling's approximation with (s = foldr (\(c, q) acc -> c + (q / (x + acc))) 0 (zip (tail p) q) in (log s) - t + log (sqrt (2 * pi) / x) + (c * log (1 + c / 12.0 - (c * c) / 360.0)) function and in the second example, we are going to use Lanczos approximation formula along with (lanczos = log $ sqrt (2 * pi / x) * sum (zipWith (\c s -> c / (x + s)) cof [1,2..g]) in lanczos + log ser_sum) function.
Method 1: Calculating the logarithmic gamma of the given number using Stirlings's approximation
In this method, the Stirling's approximation of the gamma function is calculated and the Spouge approximation to calculate the logarithm gamma function. The logGamma function takes a Double input value x and calculates its logarithm gamma using the Spouge approximation algorithm.
Algorithm
-
Step 1 ? The logGamma function is defined with some initial values.
The function is defined using log and foldr function as, s = foldr (\(c, q) acc -> c + (q / (x + acc))) 0 (zip (tail p) q) in (log s) - t + log (sqrt (2 * pi) / x) + (c * log (1 + c / 12.0 - (c * c) / 360.0)).
Step 2 ? The program execution will be started from main function. The main() function has whole control of the program. It is written as main = do.
Step 3 ? The variable named, ?x' is defined that will hold the number whose logarithmic gamma is to be computed.
Step 4 ? The logGamma function is called and the number is passed as argument to it.
Step 5 ? The resultant logarithmic gamma value is printed to the console, once the function is being called.
Example
In this example, the logarithmic gamma of the given number is calculated using Stirling's approximation.
logGamma :: Double -> Double logGamma x = let g = 4.7421875 p = [0.99999999999999709182, 57.156235665862923517, -59.597960355475491248, 14.136097974741747174, -0.49191381609762019978, 0.33994649984811888699e-4, 0.46523628927048575665e-4, -0.98374475304879564677e-4, 0.15808870322491248884e-3, -0.21026444172410488319e-3, 0.21743961811521264320e-3, -0.16431810653676389022e-3, 0.84418223983852743293e-4, -0.26190838401581408670e-4, 0.36899182659531622704e-5] q = [1.00000000000000000000, 0.57721566490153286061, -0.65587807152025388108, -0.42002635034095235529, 0.16653861138229148950, -0.42197734555544336749e-1, -0.96219715278769735639e-2, 0.72189432466630995429e-2, -0.11651675918590651105e-2, -0.21524167411495097281e-3, 0.12805028238811618634e-3, -0.20134854780788238622e-4, -0.12504934821426706587e-5, 0.11330272319816958824e-5, -0.20563384169776071063e-6] a = head p b = last p ag = a + g t = x + g - 0.5 c = (t - ag) / sqrt ag s = foldr (\(c, q) acc -> c + (q / (x + acc))) 0 (zip (tail p) q) in (log s) - t + log (sqrt (2 * pi) / x) + (c * log (1 + c / 12.0 - (c * c) / 360.0)) main :: IO () main = do let x = 2.5 let result = logGamma x putStrLn $ "The logarithm gamma is:" ++ show result
Output:
[1 of 1] Compiling Main ( main.hs, main.o ) Linking main ... The logarithm gamma is:-2.6799871655768586
Method 2: Calculating the logarithmic gamma of the given number using Lanczos approximation formula
In this method, we compute the logarithm gamma using the Lanczos approximation formula, which does not have any singularity.
Algorithm
-
Step 1 ? The logGamma function is defined with some initial values.
The function is defined using log and foldr function as, lanczos = log $ sqrt (2 * pi / x) * sum (zipWith (\c s -> c / (x + s)) cof [1,2..g])in lanczos + log ser_sum.
Step 2 ? The program execution will be started from main function. The main() function has whole control of the program. It is written as main = do.
Step 3 ? The variable named, ?input' is defined that will hold the number whose logarithmic gamma is to be computed.
Step 4 ? The logGamma function is called and the number is passed as argument to it.
Step 5 ? The resultant logarithmic gamma value is printed to the console, once the function is being called.
Example
In this example, the logarithmic gamma of the given number is calculated using Lanczos approximation formula.
logGamma :: Double -> Double logGamma x = let cof = [0.99999999999980993, 676.5203681218851,-1259.1392167224028,771.32342877765313,-176.61502916214059,12.507343278686905,-0.13857109526572012,9.9843695780195716e-6,1.5056327351493116e-7] g = 7 ser_sum = foldl (\acc (c,s) -> acc + (c / (x + s))) 0 (zip (tail cof) [1..]) lanczos = log $ sqrt (2 * pi / x) * sum (zipWith (\c s -> c / (x + s)) cof [1,2..g]) in lanczos + log ser_sum main :: IO () main = do let input = "10" let maybeX = case reads input of [(x,"")] -> Just x _ -> Nothing case maybeX of Just x | x > 0 -> do let result = logGamma x putStrLn $ "The logarithm gamma of " ++ show x ++ " is " ++ show result _ -> putStrLn "Error: Input value must be a positive number."
Output
[1 of 1] Compiling Main ( main.hs, main.o ) Linking main ... The logarithm gamma of 10.0 is 2.4920651434972863
Conclusion
The logarithm gamma of a number in Haskell is the natural logarithm of the gamma function evaluated at that number. The gamma function is a mathematical function that generalizes the factorial function to non-integer inputs. The natural logarithm of the gamma function is commonly used in many areas of mathematics, statistics, and physics. We can calculate the logarithm gamma of any number in Haskell using Stirling's Approximation and Lanczos Approximation Formula.