Mathru
Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Back to homepage

Gamma functions

Gamma functions

Gamma

The gamma function is defined as $$ \Gamma(z) = \int_0^\infty t^{z-1} {\mathrm e}^{-t} \mathrm dt $$ for $ z > 0.0 $.

Example

Here is an example, how the gamma function can be used:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
use mathru::special::gamma;
use plotters::prelude::*;

fn main() {
    let x_start: f64 = -3.50;
    let x_end: f64 = 5.0;
    let length: usize = 2000;

    let mut graph_1: Vec<(f64, f64)> = Vec::with_capacity(length);
    for i in 0..length {
        let x: f64 = (x_end - x_start) / (length as f64) * (i as f64) + x_start;
        graph_1.push((x, gamma::gamma(x)));
    }

    let root_area = BitMapBackend::new("./figures/gamma.png", (600, 400)).into_drawing_area();
    root_area.fill(&WHITE).unwrap();

    let mut ctx = ChartBuilder::on(&root_area)
        .margin(20)
        .set_label_area_size(LabelAreaPosition::Left, 40)
        .set_label_area_size(LabelAreaPosition::Bottom, 40)
        .build_cartesian_2d(x_start..x_end, -10.0f64..25.0f64)
        .unwrap();

    ctx.configure_mesh()
        .x_desc("x")
        .y_desc("gamma(x)")
        .axis_desc_style(("sans-serif", 15).into_font())
        .draw()
        .unwrap();

    ctx.draw_series(LineSeries::new(graph_1, &BLUE)).unwrap();
}

This code snippet generates the following plot:

Gamma function

The vertical lines are caused by a bug in plotters(the used plotting library).

Upper incomplete gamma function

The upper incomplete gamma function is defined as: $$ \Gamma(s,x) = \int_x^{\infty} t^{s-1}\mathrm{e}^{-t}{\rm d}t $$

Example

use mathru::special::gamma;

let a: f64 = 0.5_f64;
let x: f64 = 0.3_f64;

let gamma_u: f64 = gamma::gamma_u(a, x);

Lower incomplete gamma function

The lower incomplete gamma function is defined as: $$ \gamma(s,x) = \int_0^x t^{s-1}\mathrm{e}^{-t}{\rm d}t $$

Example

use mathru::special::gamma;

let a: f64 = 0.5_f64;
let x: f64 = 0.3_f64;
let gamma_l: f64 = gamma::gamma_l(a, x);

Upper regularized incomplete gamma function

The upper regularized incomplete gamma function is defined as: $$ Q(s,x) = \frac{\Gamma(s,x)}{\Gamma(s)} = 1 - P(s,x) $$

Example

use mathru::special::gamma;

let a: f64 = 0.5_f64;
let x: f64 = 0.3_f64;
let gamma_ur: f64 = gamma::gamma_ur(a, x);

Lower regularized incomplete gamma function

The lower regularized incomplete gamma function is defined as: $$ P(s,x)=\frac{\gamma(s,x)}{\Gamma(s)} = 1 - Q(s, x) $$

Example

use mathru::special::gamma;

let a: f64 = 0.5_f64;
let x: f64 = 0.3_f64;
let gamma_lr: f64 = gamma::gamma_lr(a, x);