On this page:
5.1 Error Functions
5.1.1 Error Function
erf
unchecked-erf
5.1.2 Complementary Error Function
erfc
unchecked-erfc
log-erfc
unchecked-log-erfc
5.1.3 Hazard Function
hazard
unchecked-hazard
5.2 Exponential Integral Functions
5.2.1 First-Order Exponential Integral
expint-E1
unchecked-expint-E1
expint-E1-scaled
unchecked-expint-E1-scaled
5.2.2 Second-Order Exponential Integral
expint-E2
unchecked-expint-E2
expint-E2-scaled
unchecked-expint-E2-scaled
5.2.3 General Exponential Integral
expint-Ei
unchecked-expint-Ei
expint-Ei-scaled
unchecked-expint-Ei-scaled
5.3 Gamma Functions
5.3.1 Gamma Function
gamma
unchecked-gamma
lngamma
unchecked-lngamma
lngamma-sgn
unchecked-lngamma-sgn
gammainv
unchecked-gammainv
5.3.2 Regulated Gamma Function
gammastar
gamma*
unchecked-gammastar
unchecked-gamma*
5.3.3 Incomplete Gamma Function
gamma-inc-Q
unchecked-gamma-inc-Q
gamma-inc-P
unchecked-gamma-inc-P
gamma-inc
unchecked-gamma-inc
5.3.4 Factorial Function
fact
unchecked-fact
lnfact
unchecked-lnfact
5.3.5 Double Factorial Function
double-fact
unchecked-double-fact
lndouble-fact
unchecked-lndouble-fact
5.3.6 Binomial Coefficient Function
choose
unchecked-choose
lnchoose
unchecked-lnchoose
5.4 Psi Functions
5.4.1 Psi (Digamma) Functions
psi-int
unchecked-psi-int
psi
unchecked-psi
psi-1piy
unchecked-psi-1piy
5.4.2 Psi-1 (Trigamma) Functions
psi-1-int
unchecked-psi-1-int
psi-1
unchecked-psi-1
5.4.3 Psi-n (Polygamma) Functions
psi-n
unchecked-psi-n
5.5 Zeta Functions
5.5.1 Riemann Zeta Functions
zeta-int
unchecked-zeta-int
zeta
unchecked-zeta
5.5.2 Riemann Zeta Functions Minus One
zetam1-int
unchecked-zetam1-int
zetam1
unchecked-zetam1
5.5.3 Hutwitz Zeta Function
hzeta
unchecked-hzeta
5.5.4 Eta Functions
eta-int
unchecked-eta-int
eta
unchecked-eta
5.6 Beta Functions
beta
unchecked-beta
lnbeta
unchecked-lnbeta

5 Special Functions

    5.1 Error Functions

      5.1.1 Error Function

      5.1.2 Complementary Error Function

      5.1.3 Hazard Function

    5.2 Exponential Integral Functions

      5.2.1 First-Order Exponential Integral

      5.2.2 Second-Order Exponential Integral

      5.2.3 General Exponential Integral

    5.3 GammaFunctions

      5.3.1 Gamma Function

      5.3.2 Regulated Gamma Function

      5.3.3 Incomplete Gamma Function

      5.3.4 Factorial Function

      5.3.5 Double Factorial Function

      5.3.6 Binomial Coefficient Function

    5.4 Psi Functions

      5.4.1 Psi (Digamma) Functions

      5.4.2 Psi-1 (Trigamma) Functions

      5.4.3 Psi-n (Polygamma) Functions

    5.5 Zeta Functions

      5.5.1 Riemann Zeta Functions

      5.5.2 Riemann Zeta Functions Minus One

      5.5.3 Hutwitz Zeta Function

      5.5.4 Eta Functions

    5.6 Beta Functions

This chapter describes the special functions provided by the Science Collection.

The functions described in this chapter are defined in the special-functions sub-collection of the Science Collection. The entire special-functions sub-collection can be made available using the form:

 (require (planet williams/science/special-functions))

The individual modules in the special-functions sub-collection can also be made available as describes in the sections below.

5.1 Error Functions

The error function is described in Abramowitz and Stegun [Abramowitz64], Chapter 7. The functions are defined in the "error.rkt" file in the special-functions sub-collection of the science collection and are made available using the form:

 (require (planet williams/science/special-functions/error))

5.1.1 Error Function

+Erf from Wolfram MathWorld.

procedure

(erf x)  (real-in -1.0 1.0)

  x : real?
(unchecked-erf x)  (real-in -1.0 1.0)
  x : real?
Computes the error function:

erf equation.

Example: Plot of (erf x) on the interval [-4, 4].

#lang racket
(require (planet williams/science/special-functions/error)
         plot)
 
(plot (line erf)
      #:x-min -4.0 #:x-max 4.0
      #:y-min -1.0 #:y-max 1.0
      #:title "Error Function, erf(x)")

The following figure shows the resulting plot:

erf plot

5.1.2 Complementary Error Function

+Erfc from Wolfram MathWorld.

procedure

(erfc x)  (real-in 0.0 2.0)

  x : real?
(unchecked-erfc x)  (real-in 0.0 2.0)
  x : real?
Computes the complementary error function:

erfc equation.

procedure

(log-erfc x)  real?

  x : real?
(unchecked-log-erfc x)  real?
  x : real?
Computes the log of the complementary error function.

Example: Plot of (erfc x) on the interval [-4, 4].

#lang racket
(require (planet williams/science/special-functions/error)
         plot)
 
(plot (line erfc)
      #:x-min -4.0 #:x-max 4.0
      #:y-min 0.0 #:y-max 2.0
      #:title "Complementary Error Function, erfc(x)")

The following figure shows the resulting plot:

erfc plot

5.1.3 Hazard Function

+Hazard Function from Wolfram MathWorld.

The hazard function for the normal distribution, also known as the inverse Mill’s ratio, is the ratio of the probability function, P(x), to the survival function, S(x), and is defined as:

hazard equation

procedure

(hazard x)  (>=/c 0.0)

  x : real?
(unchecked-hazard x)  (>=/c 0.0)
  x : real?
Computes the hazard function for the normal distribution.

Example: Plot of (hazard x) on the interval [-5, 10].

#lang racket
(require (planet williams/science/special-functions/error)
         plot)
 
(plot (line hazard)
      #:x-min -5.0 #:x-max 10.0
      #:y-min 0.0 #:y-max 10.0
      #:title "Hazard Function, hazard(x)")

The following figure shows the resulting plot:

hazard plot

5.2 Exponential Integral Functions

+Exponential Intgral from Wolfram MathWorld.

Information on the exponential integral functions can be found in Abramowitz and Stegun [Abramowitz64], Chapter 5. The functions are defined in the "exponential-integral.rkt" file in the special-functions sub-collection of the science collection are made available using the form:

 (require (planet williams/science/special-functions/exponential-integral))

5.2.1 First-Order Exponential Integral

procedure

(expint-E1 x)  real?

  x : real?
(unchecked-expint-E1 x)  real?
  x : real?
(expint-E1-scaled x)  real?
  x : real?
(unchecked-expint-E1-scaled x)  real?
  x : real?
Computes the exponential integral E1(x):

.

Example: Plot of (expint-E1 x) on the interval [-4, 4].

#lang racket
(require (planet williams/science/special-functions/exponential-integral)
         plot)
 
(plot (line expint-E1)
      #:x-min -4.0 #:x-max 4.0
      #:y-min -10.0 #:y-max 10.0
      #:title "Exponential Integral, E1(x)")

The following figure shows the resulting plot:

5.2.2 Second-Order Exponential Integral

procedure

(expint-E2 x)  real?

  x : real?
(unchecked-expint-E2 x)  real?
  x : real?
(expint-E2-scaled x)  real?
  x : real?
(unchecked-expint-E2-scaled x)  real?
  x : real?
Computes the second-order exponential integral E2(x):

.

Example: Plot of (expint-E2 x) on the interval [-4, 4].

#lang racket
(require (planet williams/science/special-functions/exponential-integral)
         plot)
 
(plot (line expint-E2)
      #:x-min -4.0 #:x-max 4.0
      #:y-min -10.0 #:y-max 10.0
      #:title "Exponential Integral, E2(x)")

The following figure shows the resulting plot:

5.2.3 General Exponential Integral

procedure

(expint-Ei x)  real?

  x : real?
(unchecked-expint-Ei x)  real?
  x : real?
(expint-Ei-scaled x)  real?
  x : real?
(unchecked-expint-Ei-scaled x)  real?
  x : real?
Computes the exponential integral Ei(x):

.

Example: Plot of (expint-Ei x) on the interval [-4, 4].

#lang racket
(require (planet williams/science/special-functions/exponential-integral)
         plot)
 
(plot (line expint-Ei)
      #:x-min -4.0 #:x-max 4.0
      #:y-min -10.0 #:y-max 10.0
      #:title "Exponential Integral, Ei(x)")

The following figure shows the resulting plot:

5.3 GammaFunctions

The gamma functions are defined in the "gamma.rkt" file in the special-functions sub-collection of the Science Collection and are made available using the form:

 (require (planet williams/science/special-functions/gamma))

Note that the gamma functions (Section 5.3), psi functions (Section 5.4), and the zeta functions (Section 5.5) are defined in the same module, "gamma.rkt". This is because their definitions are interdependent and Racket does not allow circular module dependencies.

5.3.1 Gamma Function

+Gamma Function from Wolfram MathWorld.

The gamma function is defined by the integral:

It is related to the factorial function by Γ(n) = (n - 1)! for positive integer n. Further information on the gamma function can be found in Abramowitz and Stegun [Abramowitz64], Chapter 6.

procedure

(gamma x)  real?

  x : real?
(unchecked-gamma x)  real?
  x : real?
Computes the gamma function, Γ(x), subject to x not being a negative integer. This function is computed using the real Lanczos method. The maximum value of x such that Γ(x) is not considered an overflow is given by the constant gamma-xmax and is 171.0.

Example: Plot of (gamma x) on the interval (0, 6].

#lang racket
(require (planet williams/science/special-functions/gamma)
         plot)
 
(plot (line gamma)
      #:x-min 0.001 #:x-max 6.0
      #:y-min 0.0 #:y-max 120.0
      #:title "Gamma Function, Gamma(x)")

The following figure shows the resulting plot:

Example: Plot of (gamma x) on the interval (-1, 0).

#lang racket
(require (planet williams/science/special-functions/gamma)
         plot)
 
(plot (line gamma)
      #:x-min -0.999 #:x-max -0.001
      #:y-min -120.0 #:y-max 0.0
      #:title "Gamma Function, Gamma(x)")

The following figure shows the resulting plot:

+Log Gamma Function from Wolfram MathWorld.

procedure

(lngamma x)  real?

  x : real?
(unchecked-lngamma x)  real?
  x : real?
Computes the logarithm of the gamma function, log Γ(x), subject to x not being a negative integer. For x < 0, the real part of log Γ(x) is returned, which is equivalent to log |Γ(x)|. The function is computed using the real Lanczos method.

procedure

(lngamma-sgn x)  
real? (integer-in -1 1)
  x : real?
(unchecked-lngamma-sgn x)  
real? (integer-in -1 1)
  x : real?
Computes the logarithm of the magnitude of the gamma function and its sign, subject to x not being a negative integer, and returns them as multiple values. The function is computed using the real Lanczos method. The value of the gamma function can be reconstructed using the relation Γ(x) = sgn × exp(resultlg), where resultlg and sgn are the returned values.

Example: Plot of (lngamma x) on the interval (0, 6].

#lang racket
(require (planet williams/science/special-functions/gamma)
         plot)
 
(plot (line lngamma)
      #:x-min 0.001 #:x-max 6.0
      #:y-min 0.0 #:y-max 5.0
      #:title "Log Gamma Function, log Gamma(x)")

The following figure shows the resulting plot:

procedure

(gammainv x)  real?

  x : real?
(unchecked-gammainv x)  real?
  x : real?
Computes the reciprocal of the gamma function, 1(x), using the real Lanczos method.

5.3.2 Regulated Gamma Function

The regulated gamma function is given by

procedure

(gammastar x)  real?

  x : (>/c 0.0)
(gamma* x)  real?
  x : (>/c 0.0)
(unchecked-gammastar x)  real?
  x : (>/c 0.0)
(unchecked-gamma* x)  real?
  x : (>/c 0.0)
Computes the regulated gamma function, Γ*(x), for x > 0.

Example: Plot of (gammastar x) on the interval (0, 4].

#lang racket
(require (planet williams/science/special-functions/gamma)
         plot)
 
(plot (line gammastar)
      #:x-min 0.001 #:x-max 4.0
      #:y-min 0.0 #:y-max 10.0
      #:title "Regulated Gamma Function, Gamma*(x)")

The following figure shows the resulting plot:

5.3.3 Incomplete Gamma Function

+Incomplete Gamma Function from Wolfram MathWorld.

procedure

(gamma-inc-Q a x)  real?

  a : (>/c 0.0)
  x : (>=/c 0.0)
(unchecked-gamma-inc-Q a x)  real?
  a : (>/c 0.0)
  x : (>=/c 0.0)
Computes the normalized incomplete gamma function,

for a > 0 and x ≥ 0.

procedure

(gamma-inc-P a x)  real?

  a : (>/c 0.0)
  x : (>=/c 0.0)
(unchecked-gamma-inc-P a x)  real?
  a : (>/c 0.0)
  x : (>=/c 0.0)
Computes the complementary normalized incomplete gamma function,

for a > 0 and x ≥ 0.

procedure

(gamma-inc a x)  real?

  a : real?
  x : (>=/c 0.0)
(unchecked-gamma-inc a x)  real?
  a : real?
  x : (>=/c 0.0)
Computes the unnormalized incomplete gamma function,

for a real and x ≥ 0.

5.3.4 Factorial Function

The factorial of a positive integer n, n!, is defined as n! = n × (n - 1) × ... × 2 × 1. By definition, 0! = 1. The related function is related to the gamma function by Γ(n) = (n - 1)!.

procedure

(fact n)  (>=/c 1.0)

  n : natural-number/c
(unchecked-fact n)  (>=/c 1.0)
  n : natural-number/c
Computes the factorial of n, n!.

procedure

(lnfact n)  (>=/c 0.0)

  n : natural-number/c
(unchecked-lnfact n)  (>=/c 0.0)
  n : natural-number/c
Computes the logarithm of the factorial of n, log n!. The algorithm is faster than computing ln Γ(n + 1) via lngamma for n < 170, but defers for larger n.

5.3.5 Double Factorial Function

The double factorial of n, n!!, is defined as n! = n × (n - 2) × (n - 4) × .... By definition, -1!! = 0!! = 1.

procedure

(double-fact n)  (>=/c 1.0)

  n : natural-number/c
(unchecked-double-fact n)  (>=/c 1.0)
  n : natural-number/c
Computes the double factorial of n, n!!.

procedure

(lndouble-fact n)  (>=/c 0.0)

  n : natural-number/c
(unchecked-lndouble-fact n)  (>=/c 0.0)
  n : natural-number/c
Computes the logarithm of the double factorial of n, log n!!.

5.3.6 Binomial Coefficient Function

The binomial coefficient, n choose m, is defined as:

procedure

(choose n m)  (>=/c 1.0)

  n : natural-number/c
  m : natural-number/c
(unchecked-choose n m)  (>=/c 1.0)
  n : natural-number/c
  m : natural-number/c
Computes the binomial coefficient for n and m, n choose m.

procedure

(lnchoose n m)  (>=/c 0.0)

  n : natural-number/c
  m : natural-number/c
(unchecked-lnchoose n m)  (>=/c 0.0)
  n : natural-number/c
  m : natural-number/c
Computes the logarithm of the binomial coefficient for n and m, log (n choose m).

5.4 Psi Functions

The psi functions are defined in the "gamma.rkt" file in the special-functions sub-collection of the science collection and are made available using the form:

(require (planet williams/science/special-functions/gamma))

Note that the gamma functions (Section 5.3), psi functions (Section 5.4), and the zeta functions (Section 5.5) are defined in the same module, "gamma.rkt". This is because their definitions are interdependent and Racket does not allow circular module dependencies.

5.4.1 Psi (Digamma) Functions

+Digamma Function from Wolfram MathWorld.

procedure

(psi-int n)  real?

  n : (integer-in 1 +inf.0)
(unchecked-psi-int n)  real?
  n : (integer-in 1 +inf.0)
Computes the digamma function, Ψ(n), for positive integer n. The digamma function is also called the Psi function.

procedure

(psi x)  real?

  x : real?
(unchecked-psi x)  real?
  x : real?
Returns the digamma function, Ψ(x), for general x, x ≠ 0.

procedure

(psi-1piy y)  real?

  y : real?
(unchecked-psi-1piy y)  real?
  y : real?
Computes the real part of the digamma function on the line 1 + iy, Re Ψ(1 + iy).

Example: Plot of (psi x) on the interval (0, 5].

#lang racket
(require (planet williams/science/special-functions/gamma)
         plot)
 
(plot (line psi)
      #:x-min 0.001 #:x-max 5.0
      #:y-min -5.0 #:y-max 5.0
      #:title "Psi (Digamma) Function, Psi(x)")

The following figure shows the resulting plot:

5.4.2 Psi-1 (Trigamma) Functions

+Trigamma Function from Wolfram MathWorld.

procedure

(psi-1-int n)  real?

  n : (integer-in 1 +inf.0)
(unchecked-psi-1-int n)  real?
  n : (integer-in 1 +inf.0)
Computes the trigamma function, Ψ(n), for positive integer n.

procedure

(psi-1 x)  real?

  x : real?
(unchecked-psi-1 x)  real?
  x : real?
Computes the trigamma function, Ψ(x), for general x.

Example: Plot of (psi-1 x) on the interval (0, 5].

#lang racket
(require (planet williams/science/special-functions/gamma)
         plot)
 
(plot (line psi-1)
      #:x-min 0.001 #:x-max 5.0
      #:y-min 0.0 #:y-max 5.0
      #:title "Psi-1 (Trigamma) Function, Psi-1(x)")

The following figure shows the resulting plot:

5.4.3 Psi-n (Polygamma) Functions

+Polygamma Function from Wolfram MathWorld.

procedure

(psi-n n x)  real?

  n : natural-number/c
  x : (>/c 0.0)
(unchecked-psi-n n x)  real?
  n : natural-number/c
  x : (>/c 0.0)
Computes the polygamma function, Ψm(x), for m ≥ 0, x > 0.

Example: Plot of (psi-n n x) for n = 3 on the interval (0, 5].

#lang racket
(require (planet williams/science/special-functions/gamma)
         plot)
 
(plot (line (lambda (x) (psi-n 3 x)))
      #:x-min 0.001 #:x-max 5.0
      #:y-min 0.0 #:y-max 10.0
      #:title "Psi-n (Polygamma) Function, Psi-n(3, x)")

The following figure shows the resulting plot:

5.5 Zeta Functions

The Riemann zeta function is defined in Abramowitz and Stegun [Abramowitz64], Section 23.3. The zeta functions are defined in the "gamma.rkt" file in the special-functions subcollection of the science collection are are made available using the form:

(require (planet williams/science/special-functions/gamma))

Note that the gamma functions (Section 5.3), psi functions (Section 5.4), and the zeta functions (Section 5.5) are defined in the same module, "gamma.rkt". This is because their definitions are interdependent and Racket does not allow circular module dependencies.

5.5.1 Riemann Zeta Functions

+Riemann Zeta Function from Wolfram MathWorld.

The Riemann zeta function is defined by the infinite sum:

procedure

(zeta-int n)  real?

  n : integer?
(unchecked-zeta-int n)  real?
  n : integer?
Computes the Reimann zeta function, ζ(n), for integer n, n ≠ 1.

procedure

(zeta s)  real?

  s : real?
(unchecked-zeta s)  real?
  s : real?
Computes the Riemann zeta function, ζ(s), for arbitrary s, s ≠ 1.

Example: Plot of (zeta x) on the interval [-5, 5].

#lang racket
(require (planet williams/science/special-functions/gamma)
         plot)
 
(plot (line zeta)
      #:x-min -5.0 #:x-max 5.0
      #:y-min -5.0 #:y-max 5.0
      #:title "Riemann Zeta Function, zeta(x)")

The following figure shows the resulting plot:

5.5.2 Riemann Zeta Functions Minus One

For large positive arguments, the Riemann zeta function approached one. In this region the fractional part is interesting and, therefore, we need a function to evaluate it explicitly.

procedure

(zetam1-int n)  real?

  n : integer?
(unchecked-zetam1-int n)  real?
  n : integer?
Computes ζ(n) - 1 for integer n, n ≠ 1.

procedure

(zetam1 s)  real?

  s : real?
(unchecked-zetam1 s)  real?
  s : real?
Computes ζ(s) - 1 for argitrary s, s ≠ 1.

5.5.3 Hutwitz Zeta Function

+Hurwitz Zeta Function from Wolfram MathWorld.

The Hurwitz zeta function is defined by:

procedure

(hzeta s q)  real?

  s : (>/c 1.0)
  q : (>/c 0.0)
(unchecked-hzeta s q)  real?
  s : (>/c 1.0)
  q : (>/c 0.0)
Computes the Hurwitz zeta function, ζ(s, q), for s > 1, q > 0.

Example: Plot of (hzeta x 2.0) on the interval (1, 5].

#lang racket
(require (planet williams/science/special-functions/gamma)
         plot)
 
(plot (line (lambda (x) (hzeta x 2.0)))
      #:x-min 1.001 #:x-max 5.0
      #:y-min 0.0 #:y-max 5.0
      #:title "Hutwitz Zeta Function, hzeta(x, 2.0)")

The following figure shows the resulting plot:

5.5.4 Eta Functions

+Dirichlet Eta Function from Wolfram MathWorld.

The eta function is defined by:

procedure

(eta-int n)  real?

  n : integer?
(unchecked-eta-int n)  real?
  n : integer?
Computes the eta function, η(n), for integer n.

procedure

(eta s)  real?

  s : real?
(unchecked-eta s)  real?
  s : real?
Computes the eta function, η(s), for arbitrary s.

Example: Plot of (eta x) on the interval [-10, 10].

#lang racket
(require (planet williams/science/special-functions/gamma)
         plot)
 
(plot (line eta)
      #:x-min -10.0 #:x-max 10.0
      #:y-min -5.0 #:y-max 5.0
      #:title "Eta Function, eta(x)")

The following figure shows the resulting plot:

5.6 Beta Functions

The beta functions are defined in the "beta.rkt" file in the special-functions sub-collection of the Science Collection and are made available using the form:

 (require (planet williams/science/special-functions/beta))

+Beta Function from Wolfram MathWorld.

procedure

(beta a b)  real?

  a : (>/c 0.0)
  b : (>/c 0.0)
(unchecked-beta a b)  real?
  a : (>/c 0.0)
  b : (>/c 0.0)
Computes the beta function,

for a > 0 and b > 0.

procedure

(lnbeta a b)  real?

  a : (>/c 0.0)
  b : (>/c 0.0)
(unchecked-lnbeta a b)  real?
  a : (>/c 0.0)
  b : (>/c 0.0)
Computes the logarithm of the beta function, log Β(a, b), for a > 0 and b > 0.