Accord.Math.Beta.Incomplete C# (CSharp) Method

Incomplete() public static method

Incomplete (regularized) Beta function Ix(a, b).
public static Incomplete ( double a, double b, double x ) : double
a double
b double
x double
return double
        public static double Incomplete(double a, double b, double x)
        {
            double aa, bb, t, xx, xc, w, y;
            bool flag;

            if (a <= 0.0)
                throw new ArgumentOutOfRangeException("a", "Lower limit must be greater than zero.");
            if (b <= 0.0)
                throw new ArgumentOutOfRangeException("b", "Upper limit must be greater than zero.");

            if ((x <= 0.0) || (x >= 1.0))
            {
                if (x == 0.0) 
                    return 0.0;
                if (x == 1.0) 
                    return 1.0;
                throw new ArgumentOutOfRangeException("x", "Value must be between 0 and 1.");
            }

            flag = false;
            if ((b * x) <= 1.0 && x <= 0.95)
            {
                t = PowerSeries(a, b, x);
                return t;
            }

            w = 1.0 - x;

            if (x > (a / (a + b)))
            {
                flag = true;
                aa = b;
                bb = a;
                xc = x;
                xx = w;
            }
            else
            {
                aa = a;
                bb = b;
                xc = w;
                xx = x;
            }

            if (flag && (bb * xx) <= 1.0 && xx <= 0.95)
            {
                t = PowerSeries(aa, bb, xx);
                if (t <= Constants.DoubleEpsilon) t = 1.0 - Constants.DoubleEpsilon;
                else t = 1.0 - t;
                return t;
            }

            y = xx * (aa + bb - 2.0) - (aa - 1.0);
            if (y < 0.0)
                w = Incbcf(aa, bb, xx);
            else
                w = Incbd(aa, bb, xx) / xc;


            y = aa * System.Math.Log(xx);
            t = bb * System.Math.Log(xc);
            if ((aa + bb) < Gamma.GammaMax && System.Math.Abs(y) < Constants.LogMax && System.Math.Abs(t) < Constants.LogMax)
            {
                t = System.Math.Pow(xc, bb);
                t *= System.Math.Pow(xx, aa);
                t /= aa;
                t *= w;
                t *= Gamma.Function(aa + bb) / (Gamma.Function(aa) * Gamma.Function(bb));
                if (flag)
                {
                    if (t <= Constants.DoubleEpsilon) t = 1.0 - Constants.DoubleEpsilon;
                    else t = 1.0 - t;
                }
                return t;
            }

            y += t + Gamma.Log(aa + bb) - Gamma.Log(aa) - Gamma.Log(bb);
            y += System.Math.Log(w / aa);
            if (y < Constants.LogMin)
                t = 0.0;
            else
                t = System.Math.Exp(y);

            if (flag)
            {
                if (t <= Constants.DoubleEpsilon) t = 1.0 - Constants.DoubleEpsilon;
                else t = 1.0 - t;
            }
            return t;
        }