Accord.Math.Gamma.Log C# (CSharp) Method

Log() public static method

Natural logarithm of the gamma function.
public static Log ( double x ) : double
x double
return double
        public static double Log(double x)
        {
            if (x == 0)
                return Double.PositiveInfinity;

            double p, q, w, z;

            if (x < -34.0)
            {
                q = -x;
                w = Log(q);
                p = Math.Floor(q);

                if (p == q)
                    throw new OverflowException();

                z = q - p;
                if (z > 0.5)
                {
                    p += 1.0;
                    z = p - q;
                }
                z = q * Math.Sin(System.Math.PI * z);

                if (z == 0.0)
                    throw new OverflowException();

                z = Constants.LogPI - Math.Log(z) - w;
                return z;
            }

            if (x < 13.0)
            {
                z = 1.0;
                while (x >= 3.0)
                {
                    x -= 1.0;
                    z *= x;
                }
                while (x < 2.0)
                {
                    if (x == 0.0)
                        throw new OverflowException();

                    z /= x;
                    x += 1.0;
                }

                if (z < 0.0)
                    z = -z;

                if (x == 2.0)
                    return System.Math.Log(z);

                x -= 2.0;

                p = x * Special.Polevl(x, log_B, 5) / Special.P1evl(x, log_C, 6);

                return (Math.Log(z) + p);
            }

            if (x > 2.556348e305)
                throw new OverflowException();

            q = (x - 0.5) * Math.Log(x) - x + 0.91893853320467274178;

            if (x > 1.0e8)
                return (q);

            p = 1.0 / (x * x);

            if (x >= 1000.0)
            {
                q += ((7.9365079365079365079365e-4 * p
                    - 2.7777777777777777777778e-3) * p
                    + 0.0833333333333333333333) / x;
            }
            else
            {
                q += Special.Polevl(p, log_A, 4) / x;
            }

            return q;
        }

Same methods

Gamma::Log ( double x, int p ) : double

Usage Example

コード例 #1
0
ファイル: Special.cs プロジェクト: mindnumb/Accord.Net
        /// <summary>
        ///   Computes the factorial of a number (n!)
        /// </summary>
        public static double Factorial(int n)
        {
            if (fcache == null)
            {
                // Initialize factorial cache
                fcache    = new double[33];
                fcache[0] = 1; fcache[1] = 1;
                fcache[2] = 2; fcache[3] = 6;
                fcache[4] = 24; ftop = 4;
            }

            if (n < 0)
            {
                // Factorial is not defined for negative numbers
                throw new ArgumentException("Argument can not be negative", "n");
            }
            if (n > 32)
            {
                // Return Gamma approximation using exp(gammaln(n+1)),
                //  which for some reason is faster than gamma(n+1).
                return(Math.Exp(Gamma.Log(n + 1.0)));
            }
            else
            {
                // Compute in the standard way, but use the
                //  factorial cache to speed up computations.
                while (ftop < n)
                {
                    int j = ftop++;
                    fcache[ftop] = fcache[j] * ftop;
                }
                return(fcache[n]);
            }
        }
All Usage Examples Of Accord.Math.Gamma::Log