Example #1
0
 /// <inheritdoc />
 public override double ProbabilityDensity(double x)
 {
     if (x < 0.0)
     {
         return(0.0);
     }
     else
     {
         double z = x / s;
         // use Gamma(a) or Ln(Gamma(a)) form depending on size of a
         if (ga > 0.0)
         {
             return(Math.Pow(z, a - 1.0) * Math.Exp(-z) / ga / s);
         }
         else
         {
             // The standard Gamma distribution p(\alpha, x) is the same as the
             // Poisson distribution P(\lambda, k) with k \leftarrow \alpha - 1
             // and \lambda \leftarrow x. Use this fact plus our Poisson
             // machinery to accurately compute probabilities for large \alpha.
             return(Stirling.PoissonProbability(z, a - 1.0) / s);
             //return (Math.Exp((a - 1.0) * Math.Log(z) - z + ga) / s);
         }
     }
 }
    public static void stirling1_values_test()
    //****************************************************************************80
    //
    //  Purpose:
    //
    //    STIRLING1_VALUES_TEST tests STIRLING1_VALUES.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    08 February 2007
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        int m  = 0;
        int n  = 0;
        int s1 = 0;

        Console.WriteLine("");
        Console.WriteLine("STIRLING1_VALUES_TEST:");
        Console.WriteLine("  STIRLING1_VALUES returns values of");
        Console.WriteLine("  the Stirling numbers of the first kind.");
        Console.WriteLine("");
        Console.WriteLine("     N     N        S1");
        Console.WriteLine("");
        int n_data = 0;

        for (;;)
        {
            Stirling.stirling1_values(ref n_data, ref n, ref m, ref s1);
            if (n_data == 0)
            {
                break;
            }

            Console.WriteLine("  "
                              + n.ToString().PadLeft(6) + "  "
                              + m.ToString().PadLeft(6) + "  "
                              + s1.ToString().PadLeft(12) + "");
        }
    }
    public static void stirling2_test( )

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    STIRLING2_TEST tests STIRLING2.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    02 June 2007
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        const int M = 8;
        const int N = 8;

        int i;

        Console.WriteLine("");
        Console.WriteLine("STIRLING2_TEST");
        Console.WriteLine("  STIRLING2: Stirling numbers of second kind.");
        Console.WriteLine("  Get rows 1 through " + M + "");
        Console.WriteLine("");

        int[] s2 = Stirling.stirling2(M, N);

        for (i = 0; i < M; i++)
        {
            string cout = (i + 1).ToString().PadLeft(6) + "  ";
            int    j;
            for (j = 0; j < N; j++)
            {
                cout += s2[i + j * M].ToString().PadLeft(6) + "  ";
            }
            Console.WriteLine(cout);
        }
    }
    public static void stirling1_test( )

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    STIRLING1_TEST tests STIRLING1.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    02 June 2007
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        int       i;
        const int m = 8;
        const int n = 8;

        Console.WriteLine("");
        Console.WriteLine("STIRLING1_TEST");
        Console.WriteLine("  STIRLING1: Stirling numbers of first kind.");
        Console.WriteLine("  Get rows 1 through " + m + "");
        Console.WriteLine("");

        int[] s1 = Stirling.stirling1(m, n);

        for (i = 0; i < m; i++)
        {
            string cout = (i + 1).ToString().PadLeft(6) + "  ";
            int    j;
            for (j = 0; j < n; j++)
            {
                cout += s1[i + j * m].ToString().PadLeft(6) + "  ";
            }
            Console.WriteLine(cout);
        }
    }
 /// <inheritdoc />
 public override double ProbabilityMass(int k)
 {
     if (k < 0)
     {
         return(0.0);
     }
     else
     {
         // These are the same expression, but the form for small arguments is faster,
         // while the form for large arguments avoids overflow and cancellation errors.
         if (k < 16)
         {
             return(Math.Exp(-mu) * MoreMath.Pow(mu, k) / AdvancedIntegerMath.Factorial(k));
         }
         else
         {
             return(Stirling.PoissonProbability(mu, k));
         }
     }
 }
 /// <inheritdoc />
 public override double ProbabilityMass(int k)
 {
     if ((k < 0) || (k > n))
     {
         return(0.0);
     }
     else
     {
         int nmk = n - k;
         if ((k > 16) && (nmk > 16))
         {
             // For large arguments, use cancellation-corrected Stirling series to
             // avoid overflow and preserve accuracy.
             return(Stirling.BinomialProbability(p, k, q, nmk));
         }
         else
         {
             // Otherwise, fall back to direct evaluation.
             return(AdvancedIntegerMath.BinomialCoefficient(n, k) * MoreMath.Pow(p, k) * MoreMath.Pow(q, n - k));
         }
     }
 }
Example #7
0
    public long howMany(int num, string[] facts)
    {
        int  n      = 0;
        long result = 0;

        bool[] flag = new bool[num];
        for (int i = 0; i < num; i++)
        {
            if (!flag[i])
            {
                Queue Q = new Queue(num);
                Q.Enqueue(i);
                n++;
                while (Q.Count > 0)
                {
                    int k = (int)Q.Dequeue();
                    for (int j = 0; j < facts.Length; j++)
                    {
                        string[] x = facts[j].Split('=');
                        int      a = int.Parse(x[0]);
                        int      b = int.Parse(x[1]);
                        if (a == k && !flag[b])
                        {
                            flag[b] = true; Q.Enqueue(b);
                        }
                        if (b == k && !flag[a])
                        {
                            flag[a] = true; Q.Enqueue(a);
                        }
                    }
                }
            }
        }
        for (int i = 1; i <= n; i++)
        {
            result += Stirling.calc(n, i) * Factorial.calc(i);
        }
        return(result);
    }
Example #8
0
    public static int poly_bernoulli(int n, int k)

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    POLY_BERNOULLI evaluates the poly-Bernolli numbers with negative index.
    //
    //  Discussion:
    //
    //    The poly-Bernoulli numbers B_n^k were defined by M Kaneko
    //    formally as the coefficients of X^n/n! in a particular power
    //    series.  He also showed that, when the super-index is negative,
    //    we have
    //
    //      B_n^(-k) = Sum ( 0 <= j <= min ( n, k ) )
    //        (j!)^2 * S(n+1,j+1) * S(k+1,j+1)
    //
    //    where S(n,k) is the Stirling number of the second kind, the number of
    //    ways to partition a set of size n into k nonempty subset.
    //
    //    B_n^(-k) is also the number of "lonesum matrices", that is, 0-1
    //    matrices of n rows and k columns which are uniquely reconstructable
    //    from their row and column sums.
    //
    //    The poly-Bernoulli numbers get large very quickly.
    //
    //  Table:
    //
    //    \ K 0  1    2     3      4       5        6
    //    N
    //    0   1  1    1     1      1       1        1
    //    1   1  2    4     8     16      32       64
    //    2   1  4   14    46    146     454     1394
    //    3   1  8   46   230   1066    4718    20266
    //    4   1 16  146  1066   6902   41506   237686
    //    5   1 32  454  4718  41506  329462  2441314
    //    6   1 64 1394 20266 237686 2441314 22934774
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    15 March 2006
    //
    //  Author:
    //
    //    John Burkardt
    //
    //  Reference:
    //
    //    Chad Brewbaker,
    //    Lonesum (0,1) Matrices and Poly-Bernoulli Numbers of Negative Index,
    //    MS Thesis,
    //    Iowa State University, 2005.
    //
    //    M Kaneko,
    //    Poly-Bernoulli Numbers,
    //    Journal Theorie des Nombres Bordeaux,
    //    Volume 9, 1997, pages 221-228.
    //
    //  Parameters:
    //
    //    Input, int N, K, the indices.  N and K should be nonnegative.
    //
    //    Output, int POLY_BERNOULLI, the value of B_N^(-K).
    //
    {
        int b;
        int j;

        switch (n)
        {
        case < 0:
            b = 0;
            return(b);

        case 0:
            b = 1;
            return(b);
        }

        switch (k)
        {
        case <= 0:
            b = 0;
            return(b);
        }

        switch (k)
        {
        case 0:
            b = 1;
            return(b);
        }

        int jhi = Math.Min(n, k);
        int m   = Math.Max(n, k) + 1;

        int[] s = Stirling.stirling2(m, m);

        int jfact = 1;

        b = 0;

        for (j = 0; j <= jhi; j++)
        {
            b += jfact * jfact * s[n + j * m] * s[k + j * m];

            jfact *= j + 1;
        }

        return(b);
    }