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 - 一旦函式被呼叫,結果伽馬對數值將被列印到控制檯。

示例

在此示例中,使用斯特靈近似計算給定數字的伽馬對數。

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 - 一旦函式被呼叫,結果伽馬對數值將被列印到控制檯。

示例

在此示例中,使用蘭喬斯近似公式計算給定數字的伽馬對數。

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

結論

在Haskell中,數字的伽馬對數是在該數字處計算的伽馬函式的自然對數。伽馬函式是一個將階乘函式推廣到非整數輸入的數學函式。伽馬函式的自然對數常用於許多數學、統計和物理領域。我們可以使用斯特靈近似和蘭喬斯近似公式在Haskell中計算任何數字的伽馬對數。

更新於:2023年3月28日

99 次瀏覽

開啟您的職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.