コード例 #1
0
    public static void multiperm_enum_test()

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    MULTIPERM_ENUM_TEST tests MULTIPERM_ENUM.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    07 July 2007
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        const int N = 5;

        int[]     counts = new int[N];
        int       seed   = 123456789;
        int       test;
        const int test_num = 5;

        Console.WriteLine("");
        Console.WriteLine("MULTIPERM_ENUM_TEST");
        Console.WriteLine("  MULTIPERM_ENUM enumerates multipermutations.");
        Console.WriteLine("");
        Console.WriteLine("  N is the number of objects to be permuted.");
        Console.WriteLine("  K is the number of distinct types of objects.");
        Console.WriteLine("  COUNTS is the number of objects of each type.");
        Console.WriteLine("  NUMBER is the number of multipermutations.");
        Console.WriteLine("");
        Console.WriteLine("  Number       N       K       Counts(1:K)");
        Console.WriteLine("");

        for (test = 1; test <= test_num; test++)
        {
            int k = UniformRNG.i4_uniform_ab(1, N, ref seed);

            Comp.compnz_random(N, k, ref seed, ref counts);

            int number = Permutation.multiperm_enum(N, k, counts);

            string cout = "  " + number.ToString().PadLeft(6)
                          + "  " + N.ToString().PadLeft(6)
                          + "  " + k.ToString().PadLeft(6);
            int i;
            for (i = 0; i < k; i++)
            {
                cout += "  " + counts[i].ToString().PadLeft(4);
            }

            Console.WriteLine(cout);
        }
    }
コード例 #2
0
    public static void rat_to_dec_test()

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    RAT_TO_DEC_TEST tests RAT_TO_DEC.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    07 November 2012
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        int exponent = 0;
        int i;
        int mantissa = 0;
        int rat_bot2 = 0;
        int rat_top2 = 0;

        Console.WriteLine("");
        Console.WriteLine("RAT_TO_DEC_TEST");
        Console.WriteLine("  RAT_TO_DEC fraction => decimal,");
        Console.WriteLine("");
        Console.WriteLine("  In this test, choose the top and bottom");
        Console.WriteLine("  of a rational at random, and compute the");
        Console.WriteLine("  equivalent real number.");
        Console.WriteLine("");
        Console.WriteLine("  Then convert to decimal, and the equivalent real.");
        Console.WriteLine("");
        Console.WriteLine("  Then convert back to rational and the equivalent real.");

        int seed = 123456789;

        for (i = 1; i <= 10; i++)
        {
            int rat_top = UniformRNG.i4_uniform_ab(-1000, 1000, ref seed);
            int rat_bot = UniformRNG.i4_uniform_ab(1, 1000, ref seed);

            double r1 = rat_top / (double)rat_bot;

            Rational.rat_to_dec(rat_top, rat_bot, ref mantissa, ref exponent);
            double r2 = mantissa * Math.Pow(10.0, exponent);

            typeMethods.dec_to_rat(mantissa, exponent, ref rat_top2, ref rat_bot2);
            double r3 = rat_top2 / (double)rat_bot2;

            Console.WriteLine("");
            Console.WriteLine("  " + r1 + " = " + rat_top + "/" + rat_bot + "");
            Console.WriteLine("  " + r2 + " = " + mantissa + "*10^(" + exponent + ")");
            Console.WriteLine("  " + r3 + " = " + rat_top2 + "/" + rat_bot2 + "");
        }
    }
コード例 #3
0
    public static void dvec_add_test()

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    DVEC_ADD_TEST tests DVEC_ADD;
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    02 December 2006
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        const int N = 10;

        int[]     dvec1 = new int[N];
        int[]     dvec2 = new int[N];
        int[]     dvec3 = new int[N];
        int       seed  = 123456789;
        int       test;
        const int test_num = 10;

        Console.WriteLine("");
        Console.WriteLine("DVEC_ADD_TEST");
        Console.WriteLine("  DVEC_ADD adds decimal vectors representing integers;");
        Console.WriteLine("");
        Console.WriteLine("        I        J        I + J    DVEC_ADD");
        Console.WriteLine("");

        for (test = 1; test <= test_num; test++)
        {
            int i = UniformRNG.i4_uniform_ab(-100, 100, ref seed);
            int j = UniformRNG.i4_uniform_ab(-100, 100, ref seed);

            int k = i + j;

            typeMethods.i4_to_dvec(i, N, ref dvec1);
            typeMethods.i4_to_dvec(j, N, ref dvec2);
            typeMethods.dvec_add(N, dvec1, dvec2, ref dvec3);
            int l = typeMethods.dvec_to_i4(N, ref dvec3);

            Console.WriteLine("  " + i.ToString().PadLeft(8)
                              + "  " + j.ToString().PadLeft(8)
                              + "  " + k.ToString().PadLeft(8)
                              + "  " + l.ToString().PadLeft(8) + "");
        }
    }
コード例 #4
0
ファイル: perm.cs プロジェクト: philstopford/DesignLibs_GPL
    public static int[] perm1_uniform_new(int n, ref int seed)

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    PERM1_UNIFORM_NEW selects a random permutation of 1,...,N.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    23 May 2015
    //
    //  Author:
    //
    //    John Burkardt
    //
    //  Reference:
    //
    //    Albert Nijenhuis, Herbert Wilf,
    //    Combinatorial Algorithms,
    //    Academic Press, 1978, second edition,
    //    ISBN 0-12-519260-6.
    //
    //  Parameters:
    //
    //    Input, int N, the number of objects to be permuted.
    //
    //    Input/output, int &SEED, a seed for the random number generator.
    //
    //    Output, int PERM1_UNIFORM_NEW[N], a permutation of
    //    (1, ..., N).
    //
    {
        int i;

        int[] p = new int[n];

        for (i = 0; i < n; i++)
        {
            p[i] = i + 1;
        }

        for (i = 0; i < n; i++)
        {
            int j = UniformRNG.i4_uniform_ab(i, n - 1, ref seed);
            (p[i], p[j]) = (p[j], p[i]);
        }

        return(p);
    }
コード例 #5
0
ファイル: perm.cs プロジェクト: philstopford/DesignLibs_GPL
    public static int[] perm_uniform_new(int n, ref int seed)
    //****************************************************************************80
    //
    //  Purpose:
    //
    //    PERM_UNIFORM_NEW selects a random permutation of N objects.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    05 August 2012
    //
    //  Author:
    //
    //    Original FORTRAN77 version by Albert Nijenhuis, Herbert Wilf.
    //    C++ version by John Burkardt.
    //
    //  Reference:
    //
    //    Albert Nijenhuis, Herbert Wilf,
    //    Combinatorial Algorithms for Computers and Calculators,
    //    Second Edition,
    //    Academic Press, 1978,
    //    ISBN: 0-12-519260-6,
    //    LC: QA164.N54.
    //
    //  Parameters:
    //
    //    Input, int N, the number of objects to be permuted.
    //
    //    Input/output, int &SEED, a seed for the random number generator.
    //
    //    Output, int PERM_UNIFORM_NEW[N], a permutation of (1, 2, ..., N).
    //
    {
        int[] p = new int[n];

        for (int i = 0; i < n; i++)
        {
            p[i] = i + 1;
        }

        for (int i = 0; i < n; i++)
        {
            int j = UniformRNG.i4_uniform_ab(i, n - 1, ref seed);
            (p[i], p[j]) = (p[j], p[i]);
        }

        return(p);
    }
コード例 #6
0
    public static void dvec_complementx_test()

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    DVEC_COMPLEMENTX_TEST tests DVEC_COMPLEMENTX;
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    02 December 2006
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        const int N = 10;

        int[]     dvec1 = new int[N];
        int[]     dvec2 = new int[N];
        int       seed  = 123456789;
        int       test;
        const int test_num = 5;

        Console.WriteLine("");
        Console.WriteLine("DVEC_COMPLEMENTX_TEST");
        Console.WriteLine("  DVEC_COMPLEMENTX returns the ten's complement");
        Console.WriteLine("  of a (signed) decimal vector;");
        Console.WriteLine("");

        for (test = 1; test <= test_num; test++)
        {
            int i = UniformRNG.i4_uniform_ab(-100, 100, ref seed);

            typeMethods.i4_to_dvec(i, N, ref dvec1);

            typeMethods.dvec_complementx(N, dvec1, ref dvec2);

            int j = typeMethods.dvec_to_i4(N, ref dvec2);

            Console.WriteLine("");
            Console.WriteLine("  I = " + "  " + i + "");
            Console.WriteLine("  J = " + "  " + j + "");
            typeMethods.dvec_print(N, dvec1, "");
            typeMethods.dvec_print(N, dvec2, "");
        }
    }
コード例 #7
0
    public static void ubvec_xor_test()

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    UBVEC_XOR_TEST tests UBVEC_XOR;
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    26 May 2015
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        uint[]    bvec1 = new uint[10];
        uint[]    bvec2 = new uint[10];
        uint[]    bvec3 = new uint[10];
        const int n     = 10;
        int       seed  = 123456789;
        int       test;
        const int test_num = 10;

        Console.WriteLine("");
        Console.WriteLine("UBVEC_XOR_TEST");
        Console.WriteLine("  UBVEC_XOR exclusive-ors unsigned binary vectors ");
        Console.WriteLine("  representing uintegers;");
        Console.WriteLine("");
        Console.WriteLine("        I        J        K = I XOR J");
        Console.WriteLine("");

        for (test = 1; test <= test_num; test++)
        {
            uint ui = (uint )UniformRNG.i4_uniform_ab(0, 100, ref seed);
            uint uj = (uint )UniformRNG.i4_uniform_ab(0, 100, ref seed);
            typeMethods.ui4_to_ubvec(ui, n, ref bvec1);
            typeMethods.ui4_to_ubvec(uj, n, ref bvec2);
            typeMethods.ubvec_xor(n, bvec1, bvec2, bvec3);
            uint uk = typeMethods.ubvec_to_ui4(n, bvec3);

            Console.WriteLine("  " + ui.ToString().PadLeft(8)
                              + "  " + uj.ToString().PadLeft(8)
                              + "  " + uk.ToString().PadLeft(8) + "");
        }
    }
コード例 #8
0
ファイル: Runs.cs プロジェクト: philstopford/DesignLibs_GPL
    public static int[] runs_simulate(int m, int n, ref int seed)
    //****************************************************************************80
    //
    //  Purpose:
    //
    //    RUNS_SIMULATE simulates a case governed by the Runs PDF.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    27 January 2007
    //
    //  Author:
    //
    //    John Burkardt
    //
    //  Parameters:
    //
    //    Input, int M, N, the parameters of the PDF.
    //
    //    Input/output, int &SEED, a seed for the random number generator.
    //
    //    Output, int RUNS_SIMULATE[M+N], a sequence of M 0's and N 1's chosen
    //    uniformly at random.
    //
    {
        int[] a = new int[m + n];

        for (int i = 0; i < m; i++)
        {
            a[i] = 0;
        }

        for (int i = m; i < m + n; i++)
        {
            a[i] = 1;
        }

        for (int i = 1; i <= m + n - 1; i++)
        {
            int j = UniformRNG.i4_uniform_ab(i, m + n, ref seed);

            (a[i - 1], a[j - 1]) = (a[j - 1], a[i - 1]);
        }

        return(a);
    }
コード例 #9
0
    public static void dvec_to_i4_test()

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    DVEC_TO_I4_TEST tests DVEC_TO_I4;
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    28 May 2015
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        int[] dvec = new int[6];
        int   i;

        Console.WriteLine("");
        Console.WriteLine("DVEC_TO_I4_TEST");
        Console.WriteLine("  DVEC_TO_I4 converts a DVEC to an I4;");
        Console.WriteLine("");
        Console.WriteLine("         I4 => DVEC => I4");
        Console.WriteLine("");

        int seed = 123456789;
        int i1   = UniformRNG.i4_uniform_ab(-10000, 10000, ref seed);

        const int n = 6;

        typeMethods.i4_to_dvec(i1, n, ref dvec);

        int i2 = typeMethods.dvec_to_i4(n, ref dvec);

        string cout = "  " + i1.ToString().PadLeft(6) + "  ";

        for (i = n - 1; 0 <= i; i--)
        {
            cout += dvec[i].ToString().PadLeft(2);
        }

        Console.WriteLine(cout + "  " + i2.ToString().PadLeft(6) + "");
    }
コード例 #10
0
ファイル: r8Test.cs プロジェクト: philstopford/DesignLibs_GPL
    public static void r8_agm_test()

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    R8_AGM_TEST tests R8_AGM;
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    02 December 2006
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        int i;

        Console.WriteLine("");
        Console.WriteLine("R8_AGM_TEST");
        Console.WriteLine("  R8_AGM computes the arithmetic-geometric mean (AGM)");
        Console.WriteLine("  of two nonnegative real numbers.");

        Console.WriteLine("");
        Console.WriteLine("    X        Y    R8_AGM(X,Y)");
        Console.WriteLine("");

        int seed = 123456789;

        for (i = 1; i <= 10; i++)
        {
            double x = UniformRNG.i4_uniform_ab(1, 10, ref seed);

            double y = UniformRNG.i4_uniform_ab(1, 10, ref seed);

            double z = typeMethods.r8_agm(x, y);

            Console.WriteLine(x.ToString(CultureInfo.InvariantCulture).PadLeft(10) + "  "
                              + y.ToString(CultureInfo.InvariantCulture).PadLeft(10) + "  "
                              + z.ToString(CultureInfo.InvariantCulture).PadLeft(10) + "");
        }
    }
コード例 #11
0
ファイル: i4.cs プロジェクト: philstopford/DesignLibs_GPL
    private static void i4_uniform_ab_test()

//****************************************************************************80
//
//  Purpose:
//
//    I4_UNIFORM_AB_TEST tests I4_UNIFORM_AB.
//
//  Licensing:
//
//    This code is distributed under the GNU LGPL license.
//
//  Modified:
//
//    27 October 2014
//
//  Author:
//
//    John Burkardt
//
    {
        const int a = -100;
        const int b = 200;
        int       i;
        int       seed = 123456789;

        Console.WriteLine("");
        Console.WriteLine("I4_UNIFORM_AB_TEST");
        Console.WriteLine("  I4_UNIFORM_AB computes pseudorandom values");
        Console.WriteLine("  in an interval [A,B].");

        Console.WriteLine("");
        Console.WriteLine("  The lower endpoint A = " + a + "");
        Console.WriteLine("  The upper endpoint B = " + b + "");
        Console.WriteLine("  The initial seed is " + seed + "");
        Console.WriteLine("");

        for (i = 1; i <= 20; i++)
        {
            int j = UniformRNG.i4_uniform_ab(a, b, ref seed);

            Console.WriteLine("  " + i.ToString(CultureInfo.InvariantCulture).PadLeft(8)
                              + "  " + j.ToString(CultureInfo.InvariantCulture).PadLeft(8) + "");
        }
    }
コード例 #12
0
ファイル: Npart.cs プロジェクト: philstopford/DesignLibs_GPL
    public static int[] npart_rsf_lex_random(int n, int npart, ref int seed)

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    NPART_RSF_LEX_RANDOM returns a random RSF NPART partition.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    25 July 2011
    //
    //  Author:
    //
    //    John Burkardt
    //
    //  Parameters:
    //
    //    Input, int N, the integer to be partitioned.
    //    N must be positive.
    //
    //    Input, int NPART, the number of parts of the partition.
    //    1 <= NPART <= N.
    //
    //    Input/output, int &SEED, a seed for the random number
    //    generator.
    //
    //    Output, int NPART_RSF_LEX_RANDOM[NPART], contains the partition.
    //    A(1) through A(NPART) contain the nonzero integers which
    //    sum to N.
    //
    {
        int npartitions = npart_enum(n, npart);

        int rank = UniformRNG.i4_uniform_ab(1, npartitions, ref seed);

        int[] a = npart_rsf_lex_unrank(rank, n, npart);

        return(a);
    }
コード例 #13
0
    private static void r8_mop_test()

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    R8_MOP_TEST tests R8_MOP.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    06 December 2014
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        int seed = 123456789;
        int test;

        Console.WriteLine("");
        Console.WriteLine("R8_MOP_TEST");
        Console.WriteLine("  R8_MOP evaluates (-1.0)^I4 as an R8.");
        Console.WriteLine("");
        Console.WriteLine("    I4  R8_MOP(I4)");
        Console.WriteLine("");

        const int i4_min = -100;
        const int i4_max = +100;

        for (test = 1; test <= 10; test++)
        {
            int    i4 = UniformRNG.i4_uniform_ab(i4_min, i4_max, ref seed);
            double r8 = typeMethods.r8_mop(i4);
            Console.WriteLine("  "
                              + i4.ToString(CultureInfo.InvariantCulture).PadLeft(4) + "  "
                              + r8.ToString(CultureInfo.InvariantCulture).PadLeft(4) + "");
        }
    }
コード例 #14
0
    public static int[] mono_upto_random(int m, int n, ref int seed, ref int rank)

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    MONO_UPTO_RANDOM: random monomial with total degree less than or equal to N.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    21 November 2013
    //
    //  Author:
    //
    //    John Burkardt
    //
    //  Parameters:
    //
    //    Input, int M, the spatial dimension.
    //
    //    Input, int N, the degree.
    //    0 <= N.
    //
    //    Input/output, int &SEED, the random number seed.
    //
    //    Output, int &RANK, the rank of the monomial.
    //
    //    Output, int MONO_UPTO_RANDOM[M], the random monomial.
    //
    {
        const int rank_min = 1;
        int       rank_max = mono_upto_enum(m, n);

        rank = UniformRNG.i4_uniform_ab(rank_min, rank_max, ref seed);
        int[] x = mono_unrank_grlex(m, rank);

        return(x);
    }
コード例 #15
0
    public static void mono_unrank_grlex_test()

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    MONO_UNRANK_GRLEX_TEST tests MONO_UNRANK_GRLEX.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    12 December 2013
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        const int m = 3;
        int       i;
        int       j;
        int       test;

        Console.WriteLine("");
        Console.WriteLine("MONO_UNRANK_GRLEX_TEST");
        Console.WriteLine("  MONO_UNRANK_GRLEX is given a rank, and returns the corresponding");
        Console.WriteLine("  monomial in the sequence of all monomials in M dimensions");
        Console.WriteLine("  in grlex order.");

        Console.WriteLine("");
        Console.WriteLine("  For reference, print a monomial sequence with ranks.");

        const int n        = 4;
        int       rank_max = Monomial.mono_upto_enum(m, n);

        Console.WriteLine("");
        Console.WriteLine("  Let M = " + m + "");
        Console.WriteLine("      N = " + n + "");
        Console.WriteLine("");

        int[] x = new int[n];
        for (i = 0; i < m; i++)
        {
            x[i] = 0;
        }

        i = 1;

        for (;;)
        {
            string cout = "  " + i.ToString().PadLeft(3) + "    ";
            for (j = 0; j < m; j++)
            {
                cout += x[j].ToString().PadLeft(2);
            }

            Console.WriteLine(cout);

            if (x[0] == n)
            {
                break;
            }

            Monomial.mono_upto_next_grlex(m, n, ref x);
            i += 1;
        }

        Console.WriteLine("");
        Console.WriteLine("  Now choose random ranks between 1 and " + rank_max + "");
        Console.WriteLine("");

        int       seed     = 123456789;
        const int test_num = 5;

        for (test = 1; test <= test_num; test++)
        {
            int rank = UniformRNG.i4_uniform_ab(1, rank_max, ref seed);
            x = Monomial.mono_unrank_grlex(m, rank);
            string cout = "  " + rank.ToString().PadLeft(3) + "    ";
            for (j = 0; j < m; j++)
            {
                cout += x[j].ToString().PadLeft(2);
            }

            Console.WriteLine(cout);
        }
    }
コード例 #16
0
    public static void coupon_sample(int type_num, ref int seed, ref int[] coupon, ref int box_num)
    //****************************************************************************80
    //
    //  Purpose:
    //
    //    COUPON_SAMPLE simulates the coupon collector's problem.
    //
    //  Discussion:
    //
    //    The coupon collector needs to collect one of each of TYPE_NUM
    //    coupons.  The collector may draw one coupon (or, in some settings,
    //    open one box) on each trial, and takes as many trials as necessary
    //    to complete the task.  On each trial, the probability of picking
    //    any particular type of coupon is always 1 / TYPE_NUM.
    //
    //    Interesting questions include;
    //
    //    * what is the expected number of drawings necessary to complete
    //      the collection?
    //
    //    * How does the expected number of drawings necessary to complete
    //      the collection vary as TYPE_NUM increases?
    //
    //    * What is the distribution of the numbers of each type of coupon
    //      in a typical collection when it is just completed?
    //
    //    As TYPE_NUM increases, the number of coupons necessary to be
    //    collected in order to get a complete set in any simulation
    //    strongly tends to the value TYPE_NUM * LOG ( TYPE_NUM ).
    //
    //    If TYPE_NUM is 1, the simulation ends with a single drawing.
    //
    //    If TYPE_NUM is 2, then we may call the coupon taken on the first drawing
    //    a "Head", say, and the process then is similar to the question of the
    //    length, plus one, of a run of Heads or Tails in coin flipping.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    14 October 2004
    //
    //  Author:
    //
    //    John Burkardt
    //
    //  Parameters:
    //
    //    Input, int TYPE_NUM, the number of types of coupons.
    //
    //    Input/output, int &SEED, a seed for the random number generator.
    //
    //    Output, int COUPON[TYPE_NUM], the number of coupons of each type
    //    that were collected during the simulation.
    //
    //    Output, int *BOX_NUM, the total number of boxes opened.
    //
    {
        int       i;
        const int box_max = 2000;

        for (i = 0; i < type_num; i++)
        {
            coupon[i] = 0;
        }

        int straight = 0;

        box_num = 0;
        //
        //  Draw another coupon.
        //
        while (box_num < box_max)
        {
            i = UniformRNG.i4_uniform_ab(1, type_num, ref seed);
            //
            //  Increment the number of I coupons.
            //
            coupon[i - 1] += 1;
            box_num       += 1;
            //
            //  If I is the next one we needed, increase STRAIGHT by 1.
            //
            if (i == straight + 1)
            {
                for (;;)
                {
                    straight += 1;
                    //
                    //  If STRAIGHT = TYPE_NUM, we have all of them.
                    //
                    if (type_num <= straight)
                    {
                        return;
                    }

                    //
                    //  If the next coupon has not been collected, our straight is over.
                    //
                    if (coupon[straight] <= 0)
                    {
                        break;
                    }
                }
            }
        }

        Console.WriteLine(" ");
        Console.WriteLine("COUPON_SAMPLE - Fatal error!");
        Console.WriteLine("  Maximum number of coupons drawn without success.");
    }
コード例 #17
0
    public static void dvec_mul_test()

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    DVEC_MUL_TEST tests DVEC_MUL;
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    02 December 2006
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        const int N = 10;

        int[]     dvec1    = new int[N];
        int[]     dvec2    = new int[N];
        int[]     dvec3    = new int[N];
        int       n2       = 0;
        int       seed     = 123456789;
        const int test_num = 10;
        int       test2;
        const int test2_num = 2;

        Console.WriteLine("");
        Console.WriteLine("DVEC_MUL_TEST");
        Console.WriteLine("  DVEC_MUL multiplies decimal vectors");
        Console.WriteLine("  representing integers;");

        for (test2 = 1; test2 <= test2_num; test2++)
        {
            switch (test2)
            {
            case 1:
                n2 = N;
                break;

            case 2:
                n2 = 6;

                Console.WriteLine("");
                Console.WriteLine("  NOW REPEAT THE TEST...");
                Console.WriteLine("");
                Console.WriteLine("  but use too few digits to represent big products.");
                Console.WriteLine("  This corresponds to an \"overflow\".");
                Console.WriteLine("  The result here should get the final decimal");
                Console.WriteLine("  digits correctly, though.");
                break;
            }

            Console.WriteLine("");
            Console.WriteLine("        I        J        I * J  DVEC_MUL");
            Console.WriteLine("");

            int test;
            for (test = 1; test <= test_num; test++)
            {
                int i = UniformRNG.i4_uniform_ab(-1000, 1000, ref seed);
                int j = UniformRNG.i4_uniform_ab(-1000, 1000, ref seed);

                int k = i * j;

                typeMethods.i4_to_dvec(i, n2, ref dvec1);
                typeMethods.i4_to_dvec(j, n2, ref dvec2);
                typeMethods.dvec_mul(n2, dvec1, dvec2, ref dvec3);
                int l = typeMethods.dvec_to_i4(n2, ref dvec3);

                Console.WriteLine("  " + i.ToString().PadLeft(8)
                                  + "  " + j.ToString().PadLeft(8)
                                  + "  " + k.ToString().PadLeft(8)
                                  + "  " + l.ToString().PadLeft(8) + "");
            }
        }
    }
コード例 #18
0
    private static void test05()

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    TEST05 tests COMB by generating 10 random 10-subsets of a 100 set.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    01 April 2016
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        int       j;
        const int k = 10;
        const int n = 100;

        int lmax = FullertonLib.i4_binom(n, k);

        Console.WriteLine("");
        Console.WriteLine("TEST05");
        Console.WriteLine("  Generate 10 random K-subsets of an N set.");
        Console.WriteLine("  K = " + k + "");
        Console.WriteLine("  N = " + n + "");
        Console.WriteLine("  LMAX =" + lmax + "");
        Console.WriteLine("");
        Console.WriteLine("  Note that this function is already");
        Console.WriteLine("  failing because LMAX is negative.");
        Console.WriteLine("  The combinatorial coefficient C(100,10)");
        Console.WriteLine("  is too large to store in an integer.");
        Console.WriteLine("");
        Console.WriteLine("  Although the program continues to give");
        Console.WriteLine("  results, they cannot be relied on.");

        if (!typeMethods.i4_choose_check(n, k))
        {
            Console.WriteLine("");
            Console.WriteLine("TEST05 - Warning!");
            Console.WriteLine("  The binomial coefficient cannot be");
            Console.WriteLine("  computed in integer arithmetic for");
            Console.WriteLine("  this choice of parameters.");
            return;
        }

        Console.WriteLine("");

        int seed = 123456789;

        for (j = 1; j <= 10; j++)
        {
            int    l    = UniformRNG.i4_uniform_ab(1, lmax, ref seed);
            int[]  c    = Comb.comb(n, k, l);
            string cout = "  " + l.ToString().PadLeft(4) + ":  ";
            int    i;
            for (i = 0; i < k; i++)
            {
                cout += "  " + c[i].ToString().PadLeft(4);
            }

            Console.WriteLine(cout);
        }
    }
コード例 #19
0
    private static void test007()

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    TEST007 tests TET_MESH_SEARCH_NAIVE.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    19 August 2009
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        int face     = 0;
        int node_num = 0;

        double[]  p        = new double[3];
        int       step_num = 0;
        int       test;
        const int test_num  = 5;
        int       tet_num   = 0;
        const int tet_order = 4;

        double[] tet_xyz = new double[3 * 4];

        int seed = 123456789;

        Console.WriteLine("");
        Console.WriteLine("TEST007");
        Console.WriteLine("  TET_MESH_SEARCH_NAIVE uses a naive algorithm");
        Console.WriteLine("  to search a tetrahedral mesh for the tetrahedron");
        Console.WriteLine("  containing a point.");
        //
        //  Set up the example tetrahedron mesh.
        //
        TetMesh.tet_mesh_order4_example_size(ref node_num, ref tet_num);

        Console.WriteLine("");
        Console.WriteLine("  This mesh has tetrahedron order " + tet_order + "");
        Console.WriteLine("  The number of tetrahedrons is   " + tet_num + "");

        double[] node_xyz = new double[3 * node_num];
        int[]    tet_node = new int[tet_order * tet_num];

        TetMesh.tet_mesh_order4_example_set(node_num, tet_num, ref node_xyz, ref tet_node);
        //
        //  The TET_NEIGHBOR array is needed for the Delaunay search.
        //
        int[] tet_neighbor = TetMesh.tet_mesh_neighbor_tets(tet_order, tet_num, tet_node);

        for (test = 1; test <= test_num; test++)
        {
            //
            //  Choose a tetrahedron at random.
            //
            int tet1 = UniformRNG.i4_uniform_ab(0, tet_num - 1, ref seed);

            Console.WriteLine("");
            Console.WriteLine("  Point was chosen from tetrahedron    " + tet1.ToString(CultureInfo.InvariantCulture).PadLeft(8) + "");

            int j;
            for (j = 0; j < 4; j++)
            {
                int k = tet_node[j + tet1 * 4];
                int i;
                for (i = 0; i < 3; i++)
                {
                    tet_xyz[i + j * 3] = node_xyz[i + k * 3];
                }
            }

            //
            //  Choose a point in the tetrahedron at random.
            //
            Tetrahedron.tetrahedron_sample(tet_xyz, 1, ref seed, ref p);
            //
            //  Naive search.
            //
            int tet2 = TetMesh.tet_mesh_search_naive(node_num, node_xyz, tet_order, tet_num,
                                                     tet_node, p, ref step_num);

            Console.WriteLine("  Naive search ended in tetrahedron    " + tet2.ToString(CultureInfo.InvariantCulture).PadLeft(8)
                              + ", number of steps = " + step_num + "");
            //
            //  Delaunay search.
            //
            int tet3 = TetMesh.tet_mesh_search_delaunay(node_num, node_xyz, tet_order,
                                                        tet_num, tet_node, tet_neighbor, p, ref face, ref step_num);

            Console.WriteLine("  Delaunay search ended in tetrahedron " + tet3.ToString(CultureInfo.InvariantCulture).PadLeft(8)
                              + ", number of steps = " + step_num + "");
        }
    }
コード例 #20
0
    public static void ubvec_add_test()

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    UBVEC_ADD_TEST tests UBVEC_ADD;
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    02 December 2006
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        const int N = 10;

        uint[]    bvec1 = new uint[N];
        uint[]    bvec2 = new uint[N];
        uint[]    bvec3 = new uint[N];
        int       seed  = 123456789;
        int       test;
        const int test_num = 10;

        Console.WriteLine("");
        Console.WriteLine("UBVEC_ADD_TEST");
        Console.WriteLine("  UBVEC_ADD adds unsigned binary vectors ");
        Console.WriteLine("  representing uintegers;");
        Console.WriteLine("");
        Console.WriteLine("        I        J        K = I + J");
        Console.WriteLine("");

        for (test = 1; test <= test_num; test++)
        {
            uint ui = (uint )UniformRNG.i4_uniform_ab(0, 100, ref seed);
            uint uj = (uint )UniformRNG.i4_uniform_ab(0, 100, ref seed);

            Console.WriteLine("");
            Console.WriteLine("  " + ui.ToString().PadLeft(8)
                              + "  " + uj.ToString().PadLeft(8) + "");

            uint uk = ui + uj;

            Console.WriteLine("  Directly:         "
                              + "  " + uk.ToString().PadLeft(8) + "");

            typeMethods.ui4_to_ubvec(ui, N, ref bvec1);
            typeMethods.ui4_to_ubvec(uj, N, ref bvec2);

            typeMethods.ubvec_add(N, bvec1, bvec2, ref bvec3);
            uk = typeMethods.ubvec_to_ui4(N, bvec3);

            Console.WriteLine(" UBVEC_ADD          "
                              + "  " + uk.ToString().PadLeft(8) + "");
        }
    }
コード例 #21
0
    private static void test04()

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    TEST04 tests COMB by generating 10 random 3-subsets of a 100 set.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    01 April 2016
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        int       j;
        const int k = 3;
        const int n = 100;

        int lmax = FullertonLib.i4_binom(n, k);

        Console.WriteLine("");
        Console.WriteLine("TEST04");
        Console.WriteLine("  Generate 10 random K-subsets of an N set.");
        Console.WriteLine("  K = " + k + "");
        Console.WriteLine("  N = " + n + "");
        Console.WriteLine("  LMAX =" + lmax + "");

        if (!typeMethods.i4_choose_check(n, k))
        {
            Console.WriteLine("");
            Console.WriteLine("TEST04 - Warning!");
            Console.WriteLine("  The binomial coefficient cannot be");
            Console.WriteLine("  computed in integer arithmetic for");
            Console.WriteLine("  this choice of parameters.");
            return;
        }

        Console.WriteLine("");

        int seed = 123456789;

        for (j = 1; j <= 10; j++)
        {
            int    l    = UniformRNG.i4_uniform_ab(1, lmax, ref seed);
            int[]  c    = Comb.comb(n, k, l);
            string cout = "  " + l.ToString().PadLeft(4) + ":  ";
            int    i;
            for (i = 0; i < k; i++)
            {
                cout += "  " + c[i].ToString().PadLeft(4);
            }

            Console.WriteLine(cout);
        }
    }
コード例 #22
0
    public static void r8ss_random(int n, ref int na, ref int[] diag, ref double[] a, ref int seed)

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    R8SS_RANDOM randomizes an R8SS matrix.
    //
    //  Discussion:
    //
    //    The R8SS storage format is used for real symmetric skyline matrices.
    //    This storage is appropriate when the nonzero entries of the
    //    matrix are generally close to the diagonal, but the number
    //    of nonzeroes above each diagonal varies in an irregular fashion.
    //
    //    In this case, the strategy is essentially to assign column J
    //    its own bandwidth, and store the strips of nonzeros one after
    //    another.   Note that what's important is the location of the
    //    furthest nonzero from the diagonal.  A slot will be set up for
    //    every entry between that and the diagonal, whether or not
    //    those entries are zero.
    //
    //    A skyline matrix can be Gauss-eliminated without disrupting
    //    the storage format, as long as no pivoting is required.
    //
    //    The user must set aside ( N * ( N + 1 ) ) / 2 entries for the array,
    //    although the actual storage needed will generally be about half of
    //    that.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    01 July 2016
    //
    //  Author:
    //
    //    John Burkardt
    //
    //  Parameters:
    //
    //    Input, int N, the order of the matrix.
    //    N must be positive.
    //
    //    Output, int *NA, the dimension of the array A.
    //    NA will be at least N and no greater than ( N * ( N + 1 ) ) / 2.
    //
    //    Output, int DIAG[N], the indices in A of the N diagonal elements.
    //
    //    Output, double A[(N*(N+1))/2], the R8SS matrix.
    //
    //    Input/output, int &SEED, a seed for the random number generator.
    //
    {
        int j;
        int k;

        na = 0;
        //
        //  Set the values of DIAG.
        //
        diag[0] = 0;
        na      = 1;

        for (j = 1; j < n; j++)
        {
            k       = UniformRNG.i4_uniform_ab(1, j + 1, ref seed);
            diag[j] = diag[j - 1] + k;
            na     += k;
        }

        //
        //  Now set the values of A.
        //
        int diagold = -1;

        k = 0;

        for (j = 0; j < n; j++)
        {
            int ilo = j + 1 - (diag[j] - diagold);

            int i;
            for (i = ilo; i <= j; i++)
            {
                a[k] = UniformRNG.r8_uniform_01(ref seed);
                k   += 1;
            }

            diagold = diag[j];
        }
    }
コード例 #23
0
    public static void nim_sum_test()

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    NIM_SUM_TEST tests NIM_SUM.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    01 December 2006
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        const int N = 32;

        int i;

        uint[]    i1vec = new uint[N];
        uint[]    i2vec = new uint[N];
        uint[]    i3vec = new uint[N];
        const int ihi   = 1000;
        const int ilo   = 0;
        const int ntest = 5;

        Console.WriteLine("");
        Console.WriteLine("NIM_SUM_TEST");
        Console.WriteLine("  NIM_SUM computes the Nim sum of two integers.");
        Console.WriteLine("");
        Console.WriteLine("    I    J    Nim(I+J)");
        Console.WriteLine("");

        int seed = 123456789;

        for (i = 1; i <= ntest; i++)
        {
            uint ui1 = (uint)UniformRNG.i4_uniform_ab(ilo, ihi, ref seed);
            typeMethods.ui4_to_ubvec(ui1, N, ref i1vec);

            uint ui2 = (uint)UniformRNG.i4_uniform_ab(ilo, ihi, ref seed);
            typeMethods.ui4_to_ubvec(ui2, N, ref i2vec);

            uint ui3 = typeMethods.nim_sum(ui1, ui2);
            typeMethods.ui4_to_ubvec(ui3, N, ref i3vec);

            Console.WriteLine("");
            Console.WriteLine("  I1, I2, I3 in decimal:");
            Console.WriteLine("");

            Console.WriteLine("  "
                              + ui1.ToString().PadLeft(5) + "");
            Console.WriteLine("  "
                              + ui2.ToString().PadLeft(5) + "");
            Console.WriteLine("  "
                              + ui3.ToString().PadLeft(5) + "");

            Console.WriteLine("");
            Console.WriteLine("  I1, I2, I3 in binary:");
            Console.WriteLine("");

            typeMethods.ubvec_print(N, i1vec, " ");
            typeMethods.ubvec_print(N, i2vec, " ");
            typeMethods.ubvec_print(N, i3vec, " ");
        }
    }
コード例 #24
0
ファイル: Ksub.cs プロジェクト: philstopford/DesignLibs_GPL
    public static int[] ksub_random5(int n, int k, ref int seed)

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    KSUB_RANDOM5 selects a random subset of size K from a set of size N.
    //
    //  Discussion:
    //
    //    Consider the set A = 1, 2, 3, ... N.
    //    Choose a random index I1 between 1 and N, and swap items A(1) and A(I1).
    //    Choose a random index I2 between 2 and N, and swap items A(2) and A(I2).
    //    repeat K times.
    //    A(1:K) is your random K-subset.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    09 June 2011
    //
    //  Author:
    //
    //    John Burkardt
    //
    //  Parameters:
    //
    //    Input, int N, the size of the set from which subsets
    //    are drawn.
    //
    //    Input, int K, number of elements in desired subsets.
    //    1 <= K <= N.
    //
    //    Input/output, int &SEED, a seed for the random
    //    number generator.
    //
    //    Output, int KSUB_RANDOM5[K], the indices of the randomly
    //    chosen elements.  These are 1-based indices.
    //
    {
        const int base_ = 1;
        int       i;

        //
        //  Let B index the set.
        //
        int[] b = new int[n];

        for (i = 0; i < n; i++)
        {
            b[i] = i + base_;
        }

        //
        //  Choose item 1 from N things,
        //  choose item 2 from N-1 things,
        //  choose item K from N-K+1 things.
        //
        for (i = 0; i < k; i++)
        {
            int j = UniformRNG.i4_uniform_ab(i, n - 1, ref seed);
            (b[i], b[j]) = (b[j], b[i]);
        }

        //
        //  Copy the first K elements.
        //
        int[] a = new int[k];

        for (i = 0; i < k; i++)
        {
            a[i] = b[i];
        }

        //
        //  Put the elements in ascending order.
        //
        typeMethods.i4vec_sort_heap_a(k, ref a);

        return(a);
    }
コード例 #25
0
    public static void test180( )

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    TEST180 tests SORT_HEAP_EXTERNAL.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    17 January 2007
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        const int N = 20;

        int[] a = new int[N];
        SortHeapExternalData data = new();

        Console.WriteLine("");
        Console.WriteLine("TEST180");
        Console.WriteLine("  SORT_HEAP_EXTERNAL sorts objects externally.");

        int indx = 0;
        int i;
        int j    = 0;
        int isgn = 0;
        int seed = 123456789;

        for (i = 0; i < N; i++)
        {
            a[i] = UniformRNG.i4_uniform_ab(1, N, ref seed);
        }

        typeMethods.i4vec_print(N, a, "  Unsorted array:");
        //
        //void the sort routine over and over.
        //
        for ( ;;)
        {
            Sort.sort_heap_external(ref data, N, ref indx, ref i, ref j, isgn);
            //
            //  If the return value of INDX is negative, we're asked to compare
            //  array elements I and J;
            //
            if (indx < 0)
            {
                if (a[i - 1] <= a[j - 1])
                {
                    isgn = -1;
                }
                else
                {
                    isgn = 1;
                }
            }
            //
            //  ...and if the return value of INDX is positive, we're asked to switch
            //  array elements I and J;
            //
            else if (0 < indx)
            {
                (a[i - 1], a[j - 1]) = (a[j - 1], a[i - 1]);
                //
                //  ...and if the return value of INDX is 0, we're done.
                //
            }
            else
            {
                break;
            }
        }

        typeMethods.i4vec_print(N, a, "  Sorted array:");
    }
コード例 #26
0
ファイル: Ksub.cs プロジェクト: philstopford/DesignLibs_GPL
    public static void ksub_random(int n, int k, ref int seed, ref int[] a)

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    KSUB_RANDOM selects a random subset of size K from a set of size N.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    30 April 2003
    //
    //  Author:
    //
    //    Original FORTRAN77 version by Albert Nijenhuis, Herbert Wilf.
    //    C++ version by John Burkardt.
    //
    //  Reference:
    //
    //    Albert Nijenhuis, Herbert Wilf,
    //    Combinatorial Algorithms for Computers and Calculators,
    //    Second Edition,
    //    Academic Press, 1978,
    //    ISBN: 0-12-519260-6,
    //    LC: QA164.N54.
    //
    //  Parameters:
    //
    //    Input, int N, the size of the set from which subsets are drawn.
    //
    //    Input, int K, number of elements in desired subsets.  K must
    //    be between 0 and N.
    //
    //    Input/output, int &SEED, a seed for the random number generator.
    //
    //    Output, int A[K].  A(I) is the I-th element of the
    //    output set.  The elements of A are in order.
    //
    {
        int i;
        int ir = 0;
        int ix;
        int l;
        int ll;
        int m  = 0;
        int m0 = 0;

        switch (k)
        {
        case < 0:
            Console.WriteLine("");
            Console.WriteLine("KSUB_RANDOM - Fatal error!");
            Console.WriteLine("  K = " + k + "");
            Console.WriteLine("  but 0 <= K is required!");
            return;
        }

        if (n < k)
        {
            Console.WriteLine("");
            Console.WriteLine("KSUB_RANDOM - Fatal error!");
            Console.WriteLine("  N = " + n + "");
            Console.WriteLine("  K = " + k + "");
            Console.WriteLine("  K <= N is required!");
            return;
        }

        switch (k)
        {
        case 0:
            return;
        }

        for (i = 1; i <= k; i++)
        {
            a[i - 1] = (i - 1) * n / k;
        }

        for (i = 1; i <= k; i++)
        {
            for (;;)
            {
                ix = UniformRNG.i4_uniform_ab(1, n, ref seed);

                l = 1 + (ix * k - 1) / n;

                if (a[l - 1] < ix)
                {
                    break;
                }
            }

            a[l - 1] += 1;
        }

        int ip  = 0;
        int is_ = k;

        for (i = 1; i <= k; i++)
        {
            m        = a[i - 1];
            a[i - 1] = 0;

            if (m == (i - 1) * n / k)
            {
                continue;
            }

            ip       += 1;
            a[ip - 1] = m;
        }

        int ihi = ip;

        for (i = 1; i <= ihi; i++)
        {
            ip = ihi + 1 - i;
            l  = 1 + (a[ip - 1] * k - 1) / n;
            int ids = a[ip - 1] - (l - 1) * n / k;
            a[ip - 1]  = 0;
            a[is_ - 1] = l;
            is_       -= ids;
        }

        for (ll = 1; ll <= k; ll++)
        {
            l = k + 1 - ll;

            if (a[l - 1] != 0)
            {
                ir = l;
                m0 = 1 + (a[l - 1] - 1) * n / k;
                m  = a[l - 1] * n / k - m0 + 1;
            }

            //
            //  There is something wrong with this algorithm!
            //  If A[L-1] is zero, then the values of IR, M0, and M are not defined
            //  on this loop iteration, and hence are either STALE values from the
            //  previous iteration, or UNDEFINED if this is the first pass.
            //  JVB, 21 December 2014.
            //
            ix = UniformRNG.i4_uniform_ab(m0, m0 + m - 1, ref seed);

            i = l + 1;

            while (i <= ir)
            {
                if (ix < a[i - 1])
                {
                    break;
                }

                ix      += 1;
                a[i - 2] = a[i - 1];
                i       += 1;
            }

            a[i - 2] = ix;
            m       -= 1;
        }
    }
コード例 #27
0
    private static void pruefer_to_tree_test()

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    PRUEFER_TO_TREE_TEST tests PRUEFER_TO_TREE.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    03 December 2015
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        const int n    = 5;
        int       seed = 123456789;
        int       test;
        const int test_num = 5;

        Console.WriteLine("");
        Console.WriteLine("PRUEFER_TO_TREE_TEST");
        Console.WriteLine("  PRUEFER_TO_TREE converts a Pruefer code to a tree;");

        int pruefer_num = Ranking.pruefer_enum(n);

        const int i4_lo = 0;
        int       i4_hi = pruefer_num - 1;

        for (test = 1; test <= test_num; test++)
        {
            //
            //  Pick a "random" Pruefer code.
            //
            int rank = UniformRNG.i4_uniform_ab(i4_lo, i4_hi, ref seed);

            int[] p = Ranking.pruefer_unrank(rank, n);

            Console.WriteLine("");
            Console.WriteLine("  Random Pruefer code of rank " + rank + "");
            typeMethods.i4vec_transpose_print(n - 2, p, "");
            //
            //  Convert the Pruefer code to a tree.
            //
            int[] t = Ranking.pruefer_to_tree_new(n, p);

            Console.WriteLine("");
            Console.WriteLine("  Edge list for the corresponding tree:");
            Console.WriteLine("");
            int j;
            for (j = 0; j < n - 1; j++)
            {
                Console.WriteLine("  " + j.ToString().PadLeft(2)
                                  + "  " + t[0 + j * 2].ToString().PadLeft(4)
                                  + "  " + t[1 + j * 2].ToString().PadLeft(4) + "");
            }

            //
            //  Convert the tree to a Pruefer code.
            //

            p = Ranking.tree_to_pruefer(n, t);

            Console.WriteLine("");
            typeMethods.i4vec_transpose_print(n - 2, p, "  Pruefer code:");
        }
    }
コード例 #28
0
ファイル: Ksub.cs プロジェクト: philstopford/DesignLibs_GPL
    public static void ksub_random3(int n, int k, ref int seed, ref int[] a)

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    KSUB_RANDOM3 selects a random subset of size K from a set of size N.
    //
    //  Discussion:
    //
    //    This routine uses Floyd's algorithm.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    29 May 2003
    //
    //  Author:
    //
    //    Original FORTRAN77 version by Albert Nijenhuis, Herbert Wilf.
    //    C++ version by John Burkardt.
    //
    //  Reference:
    //
    //    Albert Nijenhuis, Herbert Wilf,
    //    Combinatorial Algorithms for Computers and Calculators,
    //    Second Edition,
    //    Academic Press, 1978,
    //    ISBN: 0-12-519260-6,
    //    LC: QA164.N54.
    //
    //  Parameters:
    //
    //    Input, int N, the size of the set from which subsets are drawn.
    //
    //    Input, int K, number of elements in desired subsets.  K must
    //    be between 0 and N.
    //
    //    Input/output, int &SEED, a seed for the random number generator.
    //
    //    Output, int A[N].  I is an element of the subset
    //    if A(I) = 1, and I is not an element if A(I)=0.
    //
    {
        int i;

        if (k < 0 || n < k)
        {
            Console.WriteLine("");
            Console.WriteLine("KSUB_RANDOM3 - Fatal error!");
            Console.WriteLine("  N = " + n + "");
            Console.WriteLine("  K = " + k + "");
            Console.WriteLine("  but 0 <= K <= N is required!");
            return;
        }

        for (i = 0; i < n; i++)
        {
            a[i] = 0;
        }

        switch (k)
        {
        case 0:
            return;
        }

        for (i = n - k + 1; i <= n; i++)
        {
            int j = UniformRNG.i4_uniform_ab(1, i, ref seed);

            switch (a[j - 1])
            {
            case 0:
                a[j - 1] = 1;
                break;

            default:
                a[i - 1] = 1;
                break;
            }
        }
    }
コード例 #29
0
ファイル: Ksub.cs プロジェクト: philstopford/DesignLibs_GPL
    public static void ksub_random4(int n, int k, ref int seed, ref int[] a)

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    KSUB_RANDOM4 selects a random subset of size K from a set of size N.
    //
    //  Discussion:
    //
    //    This routine is somewhat impractical for the given problem, but
    //    it is included for comparison, because it is an interesting
    //    approach that is superior for certain applications.
    //
    //    The approach is mainly interesting because it is "incremental";
    //    it proceeds by considering every element of the set, and does not
    //    need to know how many elements there are.
    //
    //    This makes this approach ideal for certain cases, such as the
    //    need to pick 5 lines at random from a text file of unknown length,
    //    or to choose 6 people who call a certain telephone number on a
    //    given day.  Using this technique, it is possible to make the
    //    selection so that, whenever the input stops, a valid uniformly
    //    random subset has been chosen.
    //
    //    Obviously, if the number of items is known in advance, and
    //    it is easy to extract K items directly, there is no need for
    //    this approach, and it is less efficient since, among other costs,
    //    it has to generate a random number for each item, and make an
    //    acceptance/rejection test.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    04 July 2016
    //
    //  Author:
    //
    //    John Burkardt
    //
    //  Reference:
    //
    //    Tom Christiansen, Nathan Torkington,
    //    "8.6: Picking a Random Line from a File",
    //    Perl Cookbook, pages 284-285,
    //    O'Reilly, 1999.
    //
    //  Parameters:
    //
    //    Input, int N, the size of the set from which subsets are drawn.
    //
    //    Input, int K, number of elements in desired subsets.  K must
    //    be between 0 and N.
    //
    //    Input/output, int &SEED, a seed for the random number generator.
    //
    //    Output, int A[K], contains the indices of the selected items.
    //
    {
        int next = 0;

        //
        //  Here, we use a WHILE to suggest that the algorithm
        //  proceeds to the next item, without knowing how many items
        //  there are in total.
        //
        //  Note that this is really the only place where N occurs,
        //  so other termination criteria could be used, and we really
        //  don't need to know the value of N!
        //
        while (next < n)
        {
            next += 1;

            int i;
            if (next <= k)
            {
                i        = next;
                a[i - 1] = next;
            }
            else
            {
                double r = UniformRNG.r8_uniform_01(ref seed);

                if (!(r * next <= k))
                {
                    continue;
                }

                i = UniformRNG.i4_uniform_ab(1, k, ref seed);
                //
                //  If we slide the current items down, and insert at the end, we preserve order.
                //
                int j;
                for (j = i; j < k; j++)
                {
                    a[j - 1] = a[j];
                }

                a[k - 1] = next;
                //      a[i-1] = next;
            }
        }
    }
コード例 #30
0
    public static void vibonacci(int n, ref int seed, ref int[] v)

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    VIBONACCI computes the first N Vibonacci numbers.
    //
    //  Discussion:
    //
    //    The "Vibonacci numbers" are a generalization of the Fibonacci numbers:
    //      V(N+1) = +/- V(N) +/- V(N-1)
    //    where the signs are chosen randomly.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    12 May 2003
    //
    //  Author:
    //
    //    John Burkardt
    //
    //  Reference:
    //
    //    Brian Hayes,
    //    The Vibonacci Numbers,
    //    American Scientist,
    //    July-August 1999, Volume 87, Number 4.
    //
    //    Divakar Viswanath,
    //    Random Fibonacci sequences and the number 1.13198824,
    //    Mathematics of Computation, 1998.
    //
    //  Parameters:
    //
    //    Input, int N, the highest number to compute.
    //
    //    Input/output, int &SEED, a seed for the random number generator.
    //
    //    Output, int V(N), the first N Vibonacci numbers.  By convention,
    //    V(1) and V(2) are taken to be 1.
    //
    {
        int i;

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

        v[0] = 1;

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

        v[1] = 1;

        for (i = 2; i < n; i++)
        {
            int j = UniformRNG.i4_uniform_ab(0, 1, ref seed);

            int s1 = j switch
            {
                0 => - 1,
                _ => + 1
            };

            j = UniformRNG.i4_uniform_ab(0, 1, ref seed);

            int s2 = j switch
            {
                0 => - 1,
                _ => + 1
            };

            v[i] = s1 * v[i - 1] + s2 * v[i - 2];
        }
    }
}