Esempio n. 1
0
    public static void comb_row_next_test()

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    COMB_ROW_NEXT_TEST tests COMB_ROW_TEST.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    23 December 2014
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        int       n;
        const int N_MAX = 10;

        Console.WriteLine("");
        Console.WriteLine("COMB_ROW_NEXT_TEST");
        Console.WriteLine("  COMB_ROW_NEXT computes the next row of the Pascal triangle.");
        Console.WriteLine("");

        int[] c = new int[N_MAX + 1];

        for (n = 0; n <= N_MAX; n++)
        {
            Comb.comb_row_next(n, ref c);
            string cout = "  " + n.ToString().PadLeft(2) + "  ";
            int    i;
            for (i = 0; i <= n; i++)
            {
                cout += c[i].ToString().PadLeft(5);
            }

            Console.WriteLine(cout);
        }
    }
Esempio n. 2
0
    public static double bernoulli_poly(int n, double x)

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    BERNOULLI_POLY evaluates the Bernoulli polynomial of order N at X.
    //
    //  Discussion:
    //
    //    Thanks to Bart Vandewoestyne for pointing out an error in the previous
    //    documentation, 31 January 2008.
    //
    //    Special values of the Bernoulli polynomial include:
    //
    //      B(N,0) = B(N,1) = B(N), the N-th Bernoulli number.
    //
    //      B'(N,X) = N * B(N-1,X)
    //
    //      B(N,X+1) - B(N,X) = N * X^(N-1)
    //      B(N,X) = (-1)^N * B(N,1-X)
    //
    //    A formula for the Bernoulli polynomial in terms of the Bernoulli
    //    numbers is:
    //
    //      B(N,X) = sum ( 0 <= K <= N ) B(K) * C(N,K) * X^(N-K)
    //
    //    The first few polynomials include:
    //
    //      B(0,X) = 1
    //      B(1,X) = X    - 1/2
    //      B(2,X) = X^2 -   X      +  1/6
    //      B(3,X) = X^3 - 3/2*X^2 +  1/2*X
    //      B(4,X) = X^4 - 2*X^3   +      X^2 - 1/30
    //      B(5,X) = X^5 - 5/2*X^4 +  5/3*X^3 - 1/6*X
    //      B(6,X) = X^6 - 3*X^5   +  5/2*X^4 - 1/2*X^2 + 1/42
    //      B(7,X) = X^7 - 7/2*X^6 +  7/2*X^5 - 7/6*X^3 + 1/6*X
    //      B(8,X) = X^8 - 4*X^7   + 14/3*X^6 - 7/3*X^4 + 2/3*X^2 - 1/30
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    31 January 2008
    //
    //  Author:
    //
    //    John Burkardt
    //
    //  Parameters:
    //
    //    Input, int N, the order of the Bernoulli polynomial to
    //    be evaluated.  N must be 0 or greater.
    //
    //    Input, double X, the value of X at which the polynomial is to
    //    be evaluated.
    //
    //    Output, double BERNOULLI_POLY, the value of B(N,X).
    //
    {
        int i;

        double[] work = new double[n + 1];
        Sequence.Bernoulli.bernoulli_number(n, ref work);
        //
        //  Get row N of Pascal's triangle.
        //
        int[] c = new int[n + 1];
        for (i = 0; i <= n; i++)
        {
            Comb.comb_row_next(n, ref c);
        }

        double value = 1.0;

        for (i = 1; i <= n; i++)
        {
            value = value * x + work[i] * c[i];
        }

        return(value);
    }
Esempio n. 3
0
    public static void bernoulli_number(int n, ref double[] b)

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    BERNOULLI_NUMBER computes the value of the Bernoulli numbers B(0) through B(N).
    //
    //  Discussion:
    //
    //    The Bernoulli numbers are rational.
    //
    //    If we define the sum of the M-th powers of the first N integers as:
    //
    //      SIGMA(M,N) = sum ( 0 <= I <= N ) I**M
    //
    //    and let C(I,J) be the combinatorial coefficient:
    //
    //      C(I,J) = I! / ( ( I - J )! * J! )
    //
    //    then the Bernoulli numbers B(J) satisfy:
    //
    //      SIGMA(M,N) = 1/(M+1) * sum ( 0 <= J <= M ) C(M+1,J) B(J) * (N+1)^(M+1-J)
    //
    //  First values:
    //
    //   B0  1                   =         1.00000000000
    //   B1 -1/2                 =        -0.50000000000
    //   B2  1/6                 =         1.66666666666
    //   B3  0                   =         0
    //   B4 -1/30                =        -0.03333333333
    //   B5  0                   =         0
    //   B6  1/42                =         0.02380952380
    //   B7  0                   =         0
    //   B8 -1/30                =        -0.03333333333
    //   B9  0                   =         0
    //  B10  5/66                =         0.07575757575
    //  B11  0                   =         0
    //  B12 -691/2730            =        -0.25311355311
    //  B13  0                   =         0
    //  B14  7/6                 =         1.16666666666
    //  B15  0                   =         0
    //  B16 -3617/510            =        -7.09215686274
    //  B17  0                   =         0
    //  B18  43867/798           =        54.97117794486
    //  B19  0                   =         0
    //  B20 -174611/330          =      -529.12424242424
    //  B21  0                   =         0
    //  B22  854,513/138         =      6192.123
    //  B23  0                   =         0
    //  B24 -236364091/2730      =    -86580.257
    //  B25  0                   =         0
    //  B26  8553103/6           =   1425517.16666
    //  B27  0                   =         0
    //  B28 -23749461029/870     = -27298231.0678
    //  B29  0                   =         0
    //  B30  8615841276005/14322 = 601580873.901
    //
    //  Recursion:
    //
    //    With C(N+1,K) denoting the standard binomial coefficient,
    //
    //    B(0) = 1.0
    //    B(N) = - ( sum ( 0 <= K < N ) C(N+1,K) * B(K) ) / C(N+1,N)
    //
    //  Warning:
    //
    //    This recursion, which is used in this routine, rapidly results
    //    in significant errors.
    //
    //  Special Values:
    //
    //    Except for B(1), all Bernoulli numbers of odd index are 0.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    09 May 2003
    //
    //  Author:
    //
    //    John Burkardt
    //
    //  Parameters:
    //
    //    Input, int N, the order of the highest Bernoulli number to compute.
    //
    //    Output, double B[N+1], B(I) contains the I-th Bernoulli number.
    //
    {
        int i;

        switch (n)
        {
        case < 0:
            return;
        }

        b[0] = 1.0;

        switch (n)
        {
        case < 1:
            return;
        }

        b[1] = -0.5;

        int[] c = new int[n + 2];
        c[0] = 1;
        c[1] = 2;
        c[2] = 1;

        for (i = 2; i <= n; i++)
        {
            Comb.comb_row_next(i + 1, ref c);
            switch (i % 2)
            {
            case 1:
                b[i] = 0.0;
                break;

            default:
            {
                double b_sum = 0.0;
                int    j;
                for (j = 0; j <= i - 1; j++)
                {
                    b_sum += b[j] * c[j];
                }

                b[i] = -b_sum / c[i];
                break;
            }
            }
        }
    }