Пример #1
0
    public static void ksub_next4_test()

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    KSUB_NEXT4_TEST tests KSUB_NEXT4.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    02 December 2006
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        const int K = 3;

        int[]     a = new int[K];
        const int n = 5;

        Console.WriteLine("");
        Console.WriteLine("KSUB_NEXT4_TEST");
        Console.WriteLine("  KSUB_NEXT4 generates K subsets of an N set.");
        Console.WriteLine("  N = " + n + "");
        Console.WriteLine("  K = " + K + "");
        Console.WriteLine("");
        Console.WriteLine("Rank    Subset");
        Console.WriteLine("");

        bool done = true;
        int  rank = 0;

        for (;;)
        {
            Ksub.ksub_next4(n, K, ref a, ref done);

            if (done)
            {
                break;
            }

            rank += 1;
            string cout = rank.ToString().PadLeft(4) + "  ";
            cout += "  ";
            int i;
            for (i = 0; i < K; i++)
            {
                cout += a[i].ToString().PadLeft(4) + "  ";
            }

            Console.WriteLine(cout);
        }
    }
Пример #2
0
    public static void ksub_unrank_test()

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    KSUB_UNRANK_TEST tests KSUB_UNRANK.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    07 March 2007
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        const int K = 3;

        int i;

        int[]     a = new int[K];
        const int n = 5;

        const int rank = 8;

        Console.WriteLine("");
        Console.WriteLine("KSUB_UNRANK_TEST");
        Console.WriteLine("  KSUB_UNRANK: find the K-subset of an N set");
        Console.WriteLine("  of a given rank.");
        Console.WriteLine("");
        Console.WriteLine("  For N = " + n + "");
        Console.WriteLine("  and K = " + K + "");
        Console.WriteLine("  and the desired rank is " + rank + "");

        Ksub.ksub_unrank(K, rank, ref a);

        Console.WriteLine("");
        Console.WriteLine("  The subset of the given rank is:");
        Console.WriteLine("");

        string cout = "";

        for (i = 0; i < K; i++)
        {
            cout += a[i].ToString().PadLeft(4);
        }

        Console.WriteLine(cout);
    }
Пример #3
0
    public static void ksub_rank_test()

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    KSUB_RANK_TEST tests KSUB_RANK.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    07 March 2007
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        const int N = 6;
        const int K = 3;

        int[] a = { 1, 3, 5 };
        int   i;
        int   rank = 0;

        Console.WriteLine("");
        Console.WriteLine("KSUB_RANK_TEST");
        Console.WriteLine("  KSUB_RANK: determine the rank of a K subset of an N set.");
        Console.WriteLine("");
        Console.WriteLine("  For N = " + N + "");
        Console.WriteLine("  and K = " + K + "");
        Console.WriteLine("  the subset is:");
        Console.WriteLine("");

        string cout = "";

        for (i = 0; i < K; i++)
        {
            cout += a[i].ToString().PadLeft(4);
        }

        Console.WriteLine(cout);

        Ksub.ksub_rank(K, a, ref rank);

        Console.WriteLine("");
        Console.WriteLine("  The computed rank is " + rank + "");
    }
Пример #4
0
    public static void ksub_random4_test()

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    KSUB_RANDOM4_TEST tests KSUB_RANDOM4.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    07 March 2007
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        const int K = 3;
        const int N = 5;

        int[] a = new int[N];
        int   i;

        Console.WriteLine("");
        Console.WriteLine("KSUB_RANDOM4_TEST");
        Console.WriteLine("  KSUB_RANDOM4 generates a random K subset of an N set.");
        Console.WriteLine("  Set size is N =    " + N + "");
        Console.WriteLine("  Subset size is K = " + K + "");
        Console.WriteLine("");

        int seed = 123456789;

        for (i = 1; i <= 10; i++)
        {
            string cout = "";
            Ksub.ksub_random4(N, K, ref seed, ref a);
            int j;
            for (j = 0; j < K; j++)
            {
                cout += "  " + a[j].ToString().PadLeft(3);
            }

            Console.WriteLine(cout);
        }
    }
Пример #5
0
    public static void ksub_random5_test()

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    KSUB_RANDOM5_TEST tests KSUB_RANDOM5.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    09 June 2011
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        int       i;
        const int k = 5;
        const int n = 52;

        Console.WriteLine("");
        Console.WriteLine("KSUB_RANDOM5_TEST");
        Console.WriteLine("  KSUB_RANDOM5 generates a random K subset of an N set.");
        Console.WriteLine("  Set size is N =    " + n + "");
        Console.WriteLine("  Subset size is K = " + k + "");
        Console.WriteLine("");

        int seed = 123456789;

        for (i = 1; i <= 5; i++)
        {
            string cout = "";
            int[]  a    = Ksub.ksub_random5(n, k, ref seed);
            int    j;
            for (j = 0; j < k; j++)
            {
                cout += "  " + a[j].ToString().PadLeft(3);
            }

            Console.WriteLine(cout);
        }
    }
Пример #6
0
    public static double[] grid_in_cube01(int dim_num, int n, int center, ref int seed)

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    GRID_IN_CUBE01 generates a grid dataset in the unit hypercube.
    //
    //  Discussion:
    //
    //    N points are needed in a DIM_NUM dimensional space.
    //
    //    The points are to lie on a uniform grid of side N_SIDE.
    //
    //    Unless the N = N_SIDE^DIM_NUM for some N_SIDE, we can't use all the
    //    points on a grid.  What we do is find the smallest N_SIDE
    //    that's big enough, and randomly omit some points.
    //
    //    If N_SIDE is 4, then the choices in 1D are:
    //
    //    A: 0,   1/3, 2/3, 1
    //    B: 1/5, 2/5, 3/5, 4/5
    //    C: 0,   1/4, 2/4, 3/4
    //    D: 1/4, 2/4, 3/4, 1
    //    E: 1/8, 3/8, 5/8, 7/8
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    17 August 2004
    //
    //  Author:
    //
    //    John Burkardt
    //
    //  Parameters:
    //
    //    Input, int DIM_NUM, the spatial dimension.
    //
    //    Input, int N, the number of points.
    //
    //    Input, int CENTER, specifies the 1D grid centering:
    //    1: first point is 0.0, last point is 1.0;
    //    2: first point is 1/(N+1), last point is N/(N+1);
    //    3: first point is 0, last point is (N-1)/N;
    //    4: first point is 1/N, last point is 1;
    //    5: first point is 1/(2*N), last point is (2*N-1)/(2*N);
    //
    //    Input/output, int &SEED, a seed for the random number generator.
    //
    //    Output, double GRID_IN_CUBE01[DIM_NUM*N], the points.
    //
    {
        int j;
        //
        //  Find the dimension of the smallest grid with N points.
        //
        int n_side = grid_side(dim_num, n);
        //
        //  We need to select N points out of N_SIDE^DIM_NUM set.
        //
        int n_grid = (int)Math.Pow(n_side, dim_num);

        //
        //  Generate a random subset of N items from a set of size N_GRID.
        //
        int[] rank_list = new int[n];

        Ksub.ksub_random2(n_grid, n, ref seed, ref rank_list);
        //
        //  Must make one dummy call to TUPLE_NEXT_FAST with RANK = -1.
        //
        int rank = -1;

        int[]      tuple = new int[dim_num];
        BTupleData data  = new() { base_ = new int[dim_num] };

        BTuple.tuple_next_fast(ref data, n_side, dim_num, rank, ref tuple);
        //
        //  Now generate the appropriate indices, and "center" them.
        //
        double[] r = new double[dim_num * n];

        for (j = 0; j < n; j++)
        {
            rank = rank_list[j] - 1;

            BTuple.tuple_next_fast(ref data, n_side, dim_num, rank, ref tuple);

            int i;
            switch (center)
            {
            case 1:
            {
                for (i = 0; i < dim_num; i++)
                {
                    r[i + j * dim_num] = (tuple[i] - 1)
                                         / (double)(n_side - 1);
                }

                break;
            }

            case 2:
            {
                for (i = 0; i < dim_num; i++)
                {
                    r[i + j * dim_num] = tuple[i]
                                         / (double)(n_side + 1);
                }

                break;
            }

            case 3:
            {
                for (i = 0; i < dim_num; i++)
                {
                    r[i + j * dim_num] = (tuple[i] - 1)
                                         / (double)n_side;
                }

                break;
            }

            case 4:
            {
                for (i = 0; i < dim_num; i++)
                {
                    r[i + j * dim_num] = tuple[i]
                                         / (double)n_side;
                }

                break;
            }

            case 5:
            {
                for (i = 0; i < dim_num; i++)
                {
                    r[i + j * dim_num] = (2 * tuple[i] - 1)
                                         / (double)(2 * n_side);
                }

                break;
            }
            }
        }

        return(r);
    }
Пример #7
0
    public static void subset_by_size_next(int n, ref int[] a, ref int subsize, ref bool more,
                                           ref bool more2, ref int m, ref int m2)

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    SUBSET_BY_SIZE_NEXT returns all subsets of an N set, in order of size.
    //
    //  Example:
    //
    //    N = 4:
    //
    //    1 2 3 4
    //    1 2 3
    //    1 2 4
    //    1 3 4
    //    1 3
    //    1 4
    //    2 3
    //    1
    //    2
    //    3
    //    (the empty set)
    //
    //  Discussion:
    //
    //    The subsets are returned in decreasing order of size, with the
    //    empty set last.
    //
    //    For a given size K, the K subsets are returned in lexicographic order.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    09 June 2015
    //
    //  Author:
    //
    //    John Burkardt
    //
    //  Parameters:
    //
    //    Input, int N, the size of the set.
    //
    //    Input/output, int A[N].  The entries A(1:SUBSIZE) contain
    //    the elements of the subset.  The elements are given in ascending
    //    order.
    //
    //    Output, int &SUBSIZE, the number of elements in the subset.
    //
    //    Input/output, bool &MORE.  Set MORE = FALSE before first call
    //    for a new sequence of subsets.  It then is set and remains
    //    TRUE as long as the subset computed on this call is not the
    //    final one.  When the final subset is computed, MORE is set to
    //    FALSE as a signal that the computation is done.
    //
    //    Input/output, bool &MORE2, a variable for bookkeeping.
    //    The user should declare this variable, but need not initialize it.
    //    The output value from one call must be the input value for the next.
    //
    //    Input/output, int &M, &M2, variables for bookkeeping.
    //    The user should declare this variable, but need not initialize it.
    //    The output value from one call must be the input value for the next.
    //
    {
        switch (more)
        {
        case false:
            subsize = n;
            more    = true;
            more2   = false;
            m       = 0;
            m2      = 0;
            break;

        default:
        {
            switch (more2)
            {
            case false:
                subsize -= 1;
                break;
            }

            break;
        }
        }

        switch (subsize)
        {
        //
        //  Compute the next subset of size SUBSIZE.
        //
        case > 0:
            Ksub.ksub_next(n, subsize, ref a, ref more2, ref m, ref m2);
            break;

        case 0:
            more = false;
            break;
        }
    }
Пример #8
0
    public static void ksub_next_test()

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

        Console.WriteLine("");
        Console.WriteLine("KSUB_NEXT_TEST");
        Console.WriteLine("  KSUB_NEXT generates all K subsets of an N set");
        Console.WriteLine("  in lexicographic order.");
        Console.WriteLine("");

        int rank = 0;

        const int n = 5;
        const int k = 3;

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

        bool more = false;
        int  m    = 0;
        int  m2   = 0;

        for (;;)
        {
            Ksub.ksub_next(n, k, ref a, ref more, ref m, ref m2);

            rank += 1;

            string cout = rank.ToString().PadLeft(4) + "    ";
            for (i = 0; i < k; i++)
            {
                cout += a[i].ToString().PadLeft(4);
            }

            Console.WriteLine(cout);

            if (!more)
            {
                break;
            }
        }
    }
Пример #9
0
    public static void ksub_next2_test()

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    KSUB_NEXT2_TEST tests KSUB_NEXT2.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    02 December 2006
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        const int K = 3;

        int[]     a = new int[K];
        const int n = 5;

        Console.WriteLine("");
        Console.WriteLine("KSUB_NEXT2_TEST");
        Console.WriteLine("  KSUB_NEXT2 generates the next K subset of an");
        Console.WriteLine("  N set by the revolving door method.");
        Console.WriteLine("");
        Console.WriteLine("Rank  Subset  Added  Removed");
        Console.WriteLine("");
        //
        //  KSUB_NEXT2 does not have a good way of stopping.
        //  We will save the starting subset, and stop when the
        //  new subset is the same as the starting one.
        //
        int i_in  = 0;
        int i_out = 0;
        int rank  = 0;

        typeMethods.i4vec_indicator1(K, ref a);

        for (;;)
        {
            rank += 1;
            string cout = rank.ToString().PadLeft(2) + "  ";
            int    i;
            for (i = 0; i < K; i++)
            {
                cout += a[i].ToString().PadLeft(2) + "  ";
            }

            cout += "   ";
            cout += i_in.ToString().PadLeft(2) + "  ";
            Console.WriteLine(cout + i_out.ToString().PadLeft(2) + "");

            Ksub.ksub_next2(n, K, ref a, ref i_in, ref i_out);

            bool more = false;

            for (i = 1; i <= K; i++)
            {
                if (a[i - 1] != i)
                {
                    more = true;
                }
            }

            if (!more)
            {
                break;
            }
        }
    }
Пример #10
0
    public static void ksub_to_compnz_test()

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    KSUB_TO_COMPNZ_TEST tests KSUB_TO_COMPNZ.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    05 December 2013
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        int[] ac  = new int[5];
        int[] as_ = new int[4];
        int   i;
        int   ks = 0;
        int   ns = 0;

        Console.WriteLine("");
        Console.WriteLine("KSUB_TO_COMPNZ_TEST");
        Console.WriteLine("  KSUB_TO_COMPNZ returns the nonzero composition ");
        Console.WriteLine("  corresponding to a K subset.");

        int nc   = 10;
        int kc   = 5;
        int seed = 123456789;

        for (i = 1; i <= 5; i++)
        {
            Console.WriteLine("");

            Comp.compnz_random(nc, kc, ref seed, ref ac);
            string cout = "  COMPNZ:";
            int    j;
            for (j = 0; j < kc; j++)
            {
                cout += ac[j].ToString().PadLeft(4);
            }

            Console.WriteLine(cout);

            Comp.compnz_to_ksub(nc, kc, ac, ref ns, ref ks, ref as_);
            cout = "  KSUB:  ";
            for (j = 0; j < ks; j++)
            {
                cout += as_[j].ToString().PadLeft(4);
            }

            Console.WriteLine(cout);

            Ksub.ksub_to_compnz(ns, ks, as_, ref nc, ref kc, ref ac);
            cout = "  COMPNZ:";
            for (j = 0; j < kc; j++)
            {
                cout += ac[j].ToString().PadLeft(4);
            }

            Console.WriteLine(cout);
        }
    }
Пример #11
0
    public static void ksub_next3_test()

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    KSUB_NEXT3_TEST tests KSUB_NEXT3.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    02 December 2006
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        const int K = 3;

        int[]     a     = new int[K];
        int       i_in  = 0;
        int       i_out = 0;
        const int n     = 5;

        Console.WriteLine("");
        Console.WriteLine("KSUB_NEXT3_TEST");
        Console.WriteLine("  KSUB_NEXT3 generates all K subsets of an N set");
        Console.WriteLine("  using the revolving door method.");
        Console.WriteLine("");
        Console.WriteLine("Rank    Subset  Added Removed");
        Console.WriteLine("");

        int  rank = 0;
        bool more = false;

        for (;;)
        {
            Ksub.ksub_next3(n, K, ref a, ref more, ref i_in, ref i_out);

            rank += 1;
            string cout = rank.ToString().PadLeft(4) + "  ";
            int    i;
            for (i = 0; i < K; i++)
            {
                cout += a[i].ToString().PadLeft(2) + "  ";
            }

            cout += "   ";
            cout += i_in.ToString().PadLeft(2) + "  ";
            Console.WriteLine(cout + i_out.ToString().PadLeft(2) + "");

            if (!more)
            {
                break;
            }
        }
    }