Haskell程序计算给定数字的伽马对数


在Haskell中,我们将使用斯特灵近似和兰乔斯近似公式来计算给定数字的伽马对数。在第一个例子中,我们将使用斯特灵近似公式 (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)) 函数,在第二个例子中,我们将使用兰乔斯近似公式以及 (lanczos = log $ sqrt (2 * pi / x) * sum (zipWith (\c s -> c / (x + s)) cof [1,2..g]) in lanczos + log ser_sum) 函数。

方法一:使用斯特灵近似计算给定数字的伽马对数

在此方法中,计算伽马函数的斯特灵近似值,并使用Spouge近似值来计算伽马对数函数。logGamma函数接收一个双精度输入值x,并使用Spouge近似算法计算其伽马对数。

算法

  • 步骤1 - 定义logGamma函数,并设置一些初始值。

    该函数使用log和foldr函数定义为: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))。

  • 步骤2 - 程序执行将从main函数开始。main()函数控制整个程序。它被编写为main = do。

  • 步骤3 - 定义名为“x”的变量,该变量将保存需要计算伽马对数的数字。

  • 步骤4 - 调用logGamma函数,并将数字作为参数传递给它。

  • 步骤5 - 一旦函数被调用,结果伽马对数值将被打印到控制台。

示例

在此示例中,使用斯特灵近似计算给定数字的伽马对数。

Open Compiler
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

输出

[1 of 1] Compiling Main             ( main.hs, main.o )
Linking main ...
The logarithm gamma is:-2.6799871655768586

方法二:使用兰乔斯近似公式计算给定数字的伽马对数

在此方法中,我们使用兰乔斯近似公式计算伽马对数,该公式没有任何奇点。

算法

  • 步骤1 - 定义logGamma函数,并设置一些初始值。

    该函数使用log和foldr函数定义为:lanczos = log $ sqrt (2 * pi / x) * sum (zipWith (\c s -> c / (x + s)) cof [1,2..g]) in lanczos + log ser_sum。

  • 步骤2 - 程序执行将从main函数开始。main()函数控制整个程序。它被编写为main = do。

  • 步骤3 - 定义名为“input”的变量,该变量将保存需要计算伽马对数的数字。

  • 步骤4 - 调用logGamma函数,并将数字作为参数传递给它。

  • 步骤5 - 一旦函数被调用,结果伽马对数值将被打印到控制台。

示例

在此示例中,使用兰乔斯近似公式计算给定数字的伽马对数。

Open Compiler
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."

输出

[1 of 1] Compiling Main             ( main.hs, main.o )
Linking main ...
The logarithm gamma of 10.0 is 2.4920651434972863

Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.

结论

在Haskell中,数字的伽马对数是在该数字处计算的伽马函数的自然对数。伽马函数是一个将阶乘函数推广到非整数输入的数学函数。伽马函数的自然对数常用于许多数学、统计和物理领域。我们可以使用斯特灵近似和兰乔斯近似公式在Haskell中计算任何数字的伽马对数。

更新于:2023年3月28日

99 次浏览

开启您的职业生涯

完成课程获得认证

开始学习
广告