HORIZON HASKELLDocslts/ghc-9.10.xc74966e2026-09-27Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27

Modulemath-functions-0.3.4.4Haskell2010

Numeric.SpecFunctions.Internal

Internal module with implementation of special functions.

  • 1 type
  • 48 values
valueerf :: Double -> Double
#

Error function.

\operatorname{erf}(x) = \frac{2}{\sqrt{\pi}} \int_{0}^{x} \exp(-t^2) dt

Function limits are:

\begin{aligned} &\operatorname{erf}(-\infty) &=& -1 \\ &\operatorname{erf}(0) &=& \phantom{-}\,0 \\ &\operatorname{erf}(+\infty) &=& \phantom{-}\,1 \\ \end{aligned}

valueerfc :: Double -> Double
#

Complementary error function.

\operatorname{erfc}(x) = 1 - \operatorname{erf}(x)

Function limits are:

\begin{aligned} &\operatorname{erf}(-\infty) &=&\, 2 \\ &\operatorname{erf}(0) &=&\, 1 \\ &\operatorname{erf}(+\infty) &=&\, 0 \\ \end{aligned}

valuelogGamma :: Double -> Double
#

Compute the logarithm of the gamma function, Γ(x).

\Gamma(x) = \int_0^{\infty}t^{x-1}e^{-t}\,dt = (x - 1)!

This implementation uses Lanczos approximation. It gives 14 or more significant decimal digits, except around x = 1 and x = 2, where the function goes to zero.

Returns ∞ if the input is outside of the range (0 < x ≤ 1e305).

valueincompleteGamma
  1. :: Double

    z ∈ (0,∞)

  2. -> Double

    x ∈ (0,∞)

  3. -> Double
#

Compute the normalized lower incomplete gamma function γ(z,x). Normalization means that γ(z,∞)=1

\gamma(z,x) = \frac{1}{\Gamma(z)}\int_0^{x}t^{z-1}e^{-t}\,dt

Uses Algorithm AS 239 by Shea.

valueinvIncompleteGamma
  1. :: Double

    z ∈ (0,∞)

  2. -> Double

    p ∈ [0,1]

  3. -> Double
#

Inverse incomplete gamma function. It's approximately inverse of incompleteGamma for the same z. So following equality approximately holds:

invIncompleteGamma z . incompleteGamma z ≈ id
valuedigamma :: Double -> Double
#

Compute ψ(x), the first logarithmic derivative of the gamma function.

\psi(x) = \frac{d}{dx} \ln \left(\Gamma(x)\right) = \frac{\Gamma'(x)}{\Gamma(x)}

Uses Algorithm AS 103 by Bernardo, based on Minka's C implementation.

valuelogBeta
  1. :: Double

    a > 0

  2. -> Double

    b > 0

  3. -> Double
#

Compute the natural logarithm of the beta function.

B(a,b) = \int_0^1 t^{a-1}(1-t)^{b-1}\,dt = \frac{\Gamma(a)\Gamma(b)}{\Gamma(a+b)}

valueincompleteBeta
  1. :: Double

    a > 0

  2. -> Double

    b > 0

  3. -> Double

    x, must lie in [0,1] range

  4. -> Double
#

Regularized incomplete beta function.

I(x;a,b) = \frac{1}{B(a,b)} \int_0^x t^{a-1}(1-t)^{b-1}\,dt

Uses algorithm AS63 by Majumder and Bhattachrjee and quadrature approximation for large p and q.

valueinvIncompleteBeta
  1. :: Double

    a > 0

  2. -> Double

    b > 0

  3. -> Double

    x ∈ [0,1]

  4. -> Double
#

Compute inverse of regularized incomplete beta function. Uses initial approximation from AS109, AS64 and Halley method to solve equation.

valuelog2 :: Int -> Int
#

O(log n) Compute the logarithm in base 2 of the given value.

valuefactorial :: Int -> Double
#

Compute the factorial function n!. Returns +∞ if the input is above 170 (above which the result cannot be represented by a 64-bit Double).

valuelogFactorial :: Integral a => a -> Double
#

Compute the natural logarithm of the factorial function. Gives 16 decimal digits of precision.

valuestirlingError :: Double -> Double
#

Calculate the error term of the Stirling approximation. This is only defined for non-negative values.

\operatorname{stirlingError}(n) = \log(n!) - \log(\sqrt{2\pi n}\frac{n}{e}^n)

valuechoose :: Int -> Int -> Double
#

Compute the binomial coefficient n `choose` k. For values of k > 50, this uses an approximation for performance reasons. The approximation is accurate to 12 decimal places in the worst case

Example:

7 `choose` 3 == 35
valuelogChooseFast :: Double -> Double -> Double
#

Quickly compute the natural logarithm of n choose k, with no checking.

Less numerically stable:

exp $ lg (n+1) - lg (k+1) - lg (n-k+1)
  where lg = logGamma . fromIntegral

Compute the log gamma correction factor for Stirling approximation for x ≥ 10. This correction factor is suitable for an alternate (but less numerically accurate) definition of logGamma:

\log\Gamma(x) = \frac{1}{2}\log(2\pi) + (x-\frac{1}{2})\log x - x + \operatorname{logGammaCorrection}(x)

methodlog1p :: a -> a
#

log1p x computes log (1 + x), but provides more precise results for small (absolute) values of x if possible.

methodexpm1 :: a -> a
#

expm1 x computes exp x - 1, but provides more precise results for small (absolute) values of x if possible.