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
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
//! Beta functions
use crate::special::gamma::gamma;
use crate::algebra::abstr::Real;

/// Beta function
///
/// B(x,y) = &int; t<sup>x-1</sup>(1-t)<sup>y-1</sup> dt
///
/// Fore more information:
/// <a href="https://en.wikipedia.org/wiki/Beta_function">Wikipedia Beta function</a>
///
/// # Arguments
///
/// * `x` > 0.0
/// * `y` > 0.0
///
/// # Panics
///
/// Panics if the parameter conditions are not fulfilled.
///
/// # Example
///
/// ```
/// use mathru::special::beta;
///
/// let x: f64 = 0.3_f64;
/// let y: f64 = 0.6_f64;
///
/// let beta: f64 = beta::beta(x, y);
/// ```
pub fn beta<T>(x: T, y: T) -> T
    where T: Real
{
	gamma(x) * gamma(y) / gamma(x + y)
}




/// Incomplete regularized beta function
///
/// B(x; a,b) = &int;<sub>0</sub> <sup>x</sup> t<sup>a-1</sup>(1-t)<sup>b-1</sup> dt
///
/// Fore more information:
/// <a href="https://en.wikipedia.org/wiki/Beta_function#Incomplete_beta_function">Wikipedia Beta function</a>
///
/// # Arguments
///
/// * `x` > 0.0
/// * `y` > 0.0
///
/// # Panics
///
/// Panics if the parameter conditions are not fulfilled.
///
/// # Example
///
/// ```
/// use mathru::special::beta;
///
/// let a: f64 = 0.3_f64;
/// let b: f64 = 0.6_f64;
/// let x: f64 = 0.2_f64;
///
/// let beta: f64 = beta::beta_inc_reg(x, a, b);
/// ```
/// The code from the following C code was ported to Rust
/// <a href="http://people.sc.fsu.edu/~jburkardt/c_src/asa109/asa109.c">C implementation</a>
pub fn beta_inc_reg<T>(x: T, a: T, b: T) -> T
    where T: Real
{
    let acu: T = T::from_f64(0.1E-14);

    /*
    Check the input arguments.
    */
    if a <= T::zero() || b <= T::zero()
    {
        panic!();
    }

    if T::zero() > x || x > T::one()
    {
        panic!();
    }

    /*
    Special cases.
    */
    if x == T::zero() || x == T::one()
    {
        return x
    }

    /*
      Change tail if necessary and determine S.
    */
    let mut psq: T = a + b;
    let mut cx: T = T::one() - x;
    let xx: T;
    let pp: T;
    let qq: T;
    let indx: u32;

    if a < psq * x
    {
        xx = cx;
        cx = x;
        pp = b;
        qq = a;
        indx = 1;
    }
    else
    {
        xx = x;
        pp = a;
        qq = b;
        indx = 0;
    }

    let mut term: T = T::one();
    let mut ai: T = T::one();
    let mut value: T = T::one();

    let mut ns: i32 = ( qq + cx * psq ).to_i32();

    /*
      Use the Soper reduction formula.
    */
    let mut rx: T = xx / cx;
    let mut temp: T = qq - ai;
    if ns == 0
    {
        rx = xx;
    }

    loop
    {
        term = term * temp * rx / ( pp + ai );
        value = value + term;
        temp = term.abs();

        if temp <= acu && temp <= acu * value
        {
            value = value * (pp * xx.ln() + ( qq - T::one()) * cx.ln() - (beta(a, b)).ln() ).exp() / pp;

            if indx == 0
            {
                value = T::one() - value;
            }
            break;
        }

        ai = ai + T::one();
        ns = ns - 1;

        if 0 <= ns
        {
            temp = qq - ai;
            if ns == 0
            {
                rx = xx;
            }
        }
        else
        {
            temp = psq;
            psq = psq + T::one();
        }
    }

    if indx == 1
    {
        return T::one() - value
    }
    else
    {
        return value
    }
}