public static void InitHaltonSequence()
 {
     halton2 = new float[HaltonNum];
     halton3 = new float[HaltonNum];
     halton5 = new float[HaltonNum];
     {
         Halton halton = new Halton();
         halton.Number(0, 2);
         for (int i = 0; i < HaltonNum; i++)
         {
             halton2[i] = (float)halton.Get();
             halton.Next();
         }
     }
     {
         Halton halton = new Halton();
         halton.Number(0, 3);
         for (int i = 0; i < HaltonNum; i++)
         {
             halton3[i] = (float)halton.Get();
             halton.Next();
         }
     }
     {
         Halton halton = new Halton();
         halton.Number(0, 5);
         for (int i = 0; i < HaltonNum; i++)
         {
             halton5[i] = (float)halton.Get();
             halton.Next();
         }
     }
 }
Example #2
0
    private static void halton_test()

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    HALTON_TEST tests HALTON.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    19 August 2016
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        int m;

        Console.WriteLine("");
        Console.WriteLine("HALTON_TEST");
        Console.WriteLine("  HALTON returns the I-th element of an M-dimensional");
        Console.WriteLine("  Halton sequence.");
        Console.WriteLine("");
        Console.WriteLine("    I          HALTON(I)");

        for (m = 1; m <= 3; m++)
        {
            Console.WriteLine("");
            Console.WriteLine("  Use M = " + m + "");
            Console.WriteLine("");
            int i;
            for (i = 0; i <= 10; i++)
            {
                double[] r    = Halton.halton(i, m);
                string   cout = "  " + i.ToString(CultureInfo.InvariantCulture).PadLeft(3);
                int      j;
                for (j = 0; j < m; j++)
                {
                    cout += "  " + r[j].ToString(CultureInfo.InvariantCulture).PadLeft(14);
                }

                Console.WriteLine(cout);
            }
        }
    }
Example #3
0
    private static void halton_inverse_test()

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    HALTON_INVERSE_TEST tests HALTON_INVERSE.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    19 August 2016
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        int i;

        Console.WriteLine("");
        Console.WriteLine("HALTON_INVERSE_TEST");
        Console.WriteLine("  HALTON_INVERSE inverts an element of a Halton sequence.");
        Console.WriteLine("");
        Console.WriteLine("    I        R=HALTON(I,3)  HALTON_INVERSE(R,3)");
        Console.WriteLine("");

        int m = 3;

        for (i = 0; i <= 10; i++)
        {
            double[] r    = Halton.halton(i, m);
            int      i2   = Halton.halton_inverse(r, m);
            string   cout = "  " + i.ToString(CultureInfo.InvariantCulture).PadLeft(3);
            int      j;
            for (j = 0; j < m; j++)
            {
                cout += "  " + r[j].ToString(CultureInfo.InvariantCulture).PadLeft(14);
            }

            Console.WriteLine(cout + "  " + i2.ToString(CultureInfo.InvariantCulture).PadLeft(3) + "");
        }
    }
Example #4
0
    private static void halton_sequence_test()

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    HALTON_SEQUENCE_TEST tests HALTON_SEQUENCE.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    19 August 2016
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        int m;

        double[] r;

        Console.WriteLine("");
        Console.WriteLine("HALTON_SEQUENCE_TEST");
        Console.WriteLine("  HALTON_SEQUENCE returns the elements I1 through I2");
        Console.WriteLine("  of an M-dimensional Halton sequence.");

        for (m = 1; m <= 3; m++)
        {
            Console.WriteLine("");
            Console.WriteLine("  HALTON_SEQUENCE(0,10," + m + ",R):");
            r = Halton.halton_sequence(0, 10, m);
            typeMethods.r8mat_print(m, 11, r, "  R:");
        }

        m = 3;
        Console.WriteLine("");
        Console.WriteLine("  HALTON_SEQUENCE(10,0," + m + ",R):");
        r = Halton.halton_sequence(10, 0, m);
        typeMethods.r8mat_print(m, 11, r, "  R:");
    }
Example #5
0
    private static void halton_base_test()

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    HALTON_BASE_TEST tests HALTON_BASE.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    19 August 2016
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        int[] b1 =
        {
            2, 3, 5
        }

        ;
        int[] b2 =
        {
            3, 10, 2
        }

        ;
        int i;
        int j;

        double[] r;

        Console.WriteLine("");
        Console.WriteLine("HALTON_BASE_TEST");
        Console.WriteLine("  HALTON_BASE returns the I-th element of an M-dimensional");
        Console.WriteLine("  Halton sequence, using user-specified bases.");

        int m = 3;

        Console.WriteLine("");
        Console.WriteLine("  M = " + m + "");
        string cout = "  B:";

        for (j = 0; j < m; j++)
        {
            cout += "  " + b1[j].ToString(CultureInfo.InvariantCulture).PadLeft(14);
        }

        Console.WriteLine(cout);
        Console.WriteLine("");
        for (i = 0; i <= 10; i++)
        {
            r    = Halton.halton_base(i, m, b1);
            cout = "  " + i.ToString(CultureInfo.InvariantCulture).PadLeft(3);
            for (j = 0; j < m; j++)
            {
                cout += "  " + r[j].ToString(CultureInfo.InvariantCulture).PadLeft(14);
            }

            Console.WriteLine(cout);
        }

        m = 3;
        Console.WriteLine("");
        Console.WriteLine("  M = " + m + "");
        cout = "  B:";
        for (j = 0; j < m; j++)
        {
            cout += "  " + b2[j].ToString(CultureInfo.InvariantCulture).PadLeft(14);
        }

        Console.WriteLine(cout);
        Console.WriteLine("");
        for (i = 0; i <= 10; i++)
        {
            r    = Halton.halton_base(i, m, b2);
            cout = "  " + i.ToString(CultureInfo.InvariantCulture).PadLeft(3);
            for (j = 0; j < m; j++)
            {
                cout += "  " + r[j].ToString(CultureInfo.InvariantCulture).PadLeft(14);
            }

            Console.WriteLine(cout);
        }
    }
Example #6
0
    public static void cvt_sample(ref CVTHaltonData data, int dim_num, int n, int n_now, int sample, bool initialize,
                                  ref int seed, ref double[] r)

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    CVT_SAMPLE returns sample points.
    //
    //  Discussion:
    //
    //    N sample points are to be taken from the unit box of dimension DIM_NUM.
    //
    //    These sample points are usually created by a pseudorandom process
    //    for which the points are essentially indexed by a quantity called
    //    SEED.  To get N sample points, we generate values with indices
    //    SEED through SEED+N-1.
    //
    //    It may not be practical to generate all the sample points in a
    //    single call.  For that reason, the routine allows the user to
    //    request a total of N points, but to require that only N_NOW be
    //    generated now (on this call).
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    23 June 2005
    //
    //  Author:
    //
    //    John Burkardt
    //
    //  Parameters:
    //
    //    Input, int DIM_NUM, the spatial dimension.
    //
    //    Input, int N, the number of Voronoi cells.
    //
    //    Input, int N_NOW, the number of sample points to be generated
    //    on this call.  N_NOW must be at least 1.
    //
    //    Input, int SAMPLE, specifies how the sampling is done.
    //    -1, 'RANDOM', using C++ RANDOM function;
    //     0, 'UNIFORM', using a simple uniform RNG;
    //     1, 'HALTON', from a Halton sequence;
    //     2, 'GRID', points from a grid;
    //     3, 'USER', call "user" routine.
    //
    //    Input, bool INITIALIZE, is TRUE if the pseudorandom process should be
    //    reinitialized.
    //
    //    Input/output, int *SEED, the random number seed.
    //
    //    Output, double R[DIM_NUM*N_NOW], the sample points.
    //
    {
        switch (n_now)
        {
        case < 1:
            Console.WriteLine("");
            Console.WriteLine("CVT_SAMPLE - Fatal error!");
            Console.WriteLine("  N_NOW < 1.");
            return;

        default:
            int i;
            int j;
            switch (sample)
            {
            case -1:
            {
                /*
                 * if (initialize)
                 * {
                 * random_initialize(ref seed);
                 * }
                 */
                for (j = 0; j < n_now; j++)
                {
                    for (i = 0; i < dim_num; i++)
                    {
                        r[i + j * dim_num] = RNG.nextdouble();        //(double) random() / (double) RAND_MAX;
                    }
                }

                seed += n_now * dim_num;
                break;
            }

            case 0:
                r = UniformRNG.r8mat_uniform_01(dim_num, n_now, ref seed);
                break;

            case 1:
            {
                data.halton_seed = new int[dim_num];
                data.halton_leap = new int[dim_num];
                data.halton_base = new int[dim_num];

                int halton_step = seed;

                for (i = 0; i < dim_num; i++)
                {
                    data.halton_seed[i] = 0;
                }

                for (i = 0; i < dim_num; i++)
                {
                    data.halton_leap[i] = 1;
                }

                for (i = 0; i < dim_num; i++)
                {
                    data.halton_base[i] = Prime.prime(i + 1);
                }

                Halton.i4_to_halton_sequence(dim_num, n_now, halton_step, data.halton_seed,
                                             data.halton_leap, data.halton_base, ref r);

                seed += n_now;
                break;
            }

            case 2:
            {
                double exponent = 1.0 / dim_num;
                data.ngrid = (int)Math.Pow(n, exponent);
                int rank_max = (int)Math.Pow(data.ngrid, dim_num);
                data.tuple = new int[dim_num];

                if (rank_max < n)
                {
                    data.ngrid += 1;
                    rank_max    = (int)Math.Pow(data.ngrid, dim_num);
                }

                switch (initialize)
                {
                case true:
                    data.rank = -1;
                    BTuple.tuple_next_fast(ref data.tupledata, data.ngrid, dim_num, data.rank, ref data.tuple);
                    break;
                }

                data.rank = seed % rank_max;

                for (j = 0; j < n_now; j++)
                {
                    BTuple.tuple_next_fast(ref data.tupledata, data.ngrid, dim_num, data.rank, ref data.tuple);
                    data.rank += 1;
                    data.rank %= rank_max;
                    for (i = 0; i < dim_num; i++)
                    {
                        r[i + j * dim_num] = (2.0 * data.tuple[i] - 1) / (2.0 * data.ngrid);
                    }
                }

                seed += n_now;
                break;
            }

            case 3:
                user(dim_num, n_now, ref seed, ref r);
                break;

            default:
                Console.WriteLine("");
                Console.WriteLine("CVT_SAMPLE - Fatal error!");
                Console.WriteLine("  The value of SAMPLE = " + sample + " is illegal.");
                break;
            }

            break;
        }
    }
Example #7
0
    public static void region_sampler(ref RegionData data, int m, int n, int n_total, ref double[] x,
                                      int sample_function, bool reset, ref int seed)

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    REGION_SAMPLER returns a sample point in the physical region.
    //
    //  Discussion:
    //
    //    This routine original interfaced with a lower routine called
    //    TEST_REGION, which tested whether the points generated in the
    //    bounding box were actually inside a possibly smaller physical
    //    region of interest.  It's been a long time since that option
    //    was actually used, so it's been dropped.
    //
    //    A point is chosen in the bounding box, either by a uniform random
    //    number generator, or from a vector Halton sequence.
    //
    //    The entries of the local vector HALTON_BASE should be distinct primes.
    //    Right now, we're assuming M is no greater than 3.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    04 March 2004
    //
    //  Author:
    //
    //    John Burkardt
    //
    //  Parameters:
    //
    //    Input, int M, the spatial dimension.
    //
    //    Input, int N, the number of points to generate now.
    //
    //    Input, int N_TOTAL, the total number of points to generate.
    //
    //    Output, double X[M*N], the sample points.
    //
    //    Input, int SAMPLE_FUNCTION, region sampling function:
    //    -1, sampling function is RANDOM (C++ STDLIB library function);
    //    0, sampling function is UNIFORM;
    //    1, sampling function is HALTON;
    //    2, sampling function is GRID;
    //    3, sample points are generated elsewhere, and this routine is skipped.
    //
    //    Input, bool RESET, if true, then this is the first call for a particular
    //    calculation, and initialization should be taken care of.
    //
    //    Input/output, int *SEED, the random number seed.
    //
    {
        int i;
        int j;
        int k;

        switch (sample_function)
        {
        //
        case -1:
        {
            for (k = 0; k < m * n; k++)
            {
                x[k] = entropyRNG.RNG.nextdouble();
            }

            break;
        }

        case 0:
        {
            for (k = 0; k < m * n; k++)
            {
                x[k] = UniformRNG.r8_uniform_01(ref seed);
            }

            break;
        }

        case 1:
        {
            switch (reset)
            {
            case true:
            {
                data.halton_seed = 1;

                data.halton_base = new int[m];
                for (i = 0; i < m; i++)
                {
                    data.halton_base[i] = Prime.prime(i + 1);
                }

                break;
            }
            }

            //
            //  The unusual syntax X+J*M essentially means pass the address of the beginning
            //  of the J-th vector of length M in X.
            //
            for (j = 0; j < n; j++)
            {
                Halton.i4_to_halton(data.halton_seed, data.halton_base, m, ref x, rIndex: +j * m);
                data.halton_seed += 1;
            }

            break;
        }

        case 2:
        {
            switch (reset)
            {
            case true:
            {
                data.rank = 0;
                double exponent = 1.0 / m;

                data.ngrid = (int)Math.Pow(n_total, exponent);

                if (Math.Pow(data.ngrid, m) < n_total)
                {
                    data.ngrid += 1;
                }

                data.tuple = new int[m];
                break;
            }
            }

            for (j = 0; j < n; j++)
            {
                BTuple.tuple_next_fast(ref data.tdata, data.ngrid, m, data.rank, ref data.tuple);
                data.rank += 1;
                for (i = 0; i < m; i++)
                {
                    x[j * m + i] = (2 * data.tuple[i] - 1)
                                   / (double)(2 * data.ngrid);
                }
            }

            break;
        }

        case 3:
            break;

        default:
            Console.WriteLine("");
            Console.WriteLine("REGION_SAMPLER - Fatal error!");
            Console.WriteLine("  Illegal SAMPLE_FUNCTION = " + sample_function + "");
            break;
        }
    }