Blame doc/qrng.rst

Packit 67cb25
.. index::
Packit 67cb25
   single: quasi-random sequences
Packit 67cb25
   single: low discrepancy sequences
Packit 67cb25
   single: Sobol sequence
Packit 67cb25
   single: Niederreiter sequence
Packit 67cb25
Packit 67cb25
**********************
Packit 67cb25
Quasi-Random Sequences
Packit 67cb25
**********************
Packit 67cb25
Packit 67cb25
.. include:: include.rst
Packit 67cb25
Packit 67cb25
This chapter describes functions for generating quasi-random sequences
Packit 67cb25
in arbitrary dimensions.  A quasi-random sequence progressively covers a
Packit 67cb25
:math:`d`-dimensional space with a set of points that are uniformly
Packit 67cb25
distributed.  Quasi-random sequences are also known as low-discrepancy
Packit 67cb25
sequences.  The quasi-random sequence generators use an interface that
Packit 67cb25
is similar to the interface for random number generators, except that
Packit 67cb25
seeding is not required---each generator produces a single sequence.
Packit 67cb25
Packit 67cb25
The functions described in this section are declared in the header file
Packit 67cb25
:file:`gsl_qrng.h`.
Packit 67cb25
Packit 67cb25
Quasi-random number generator initialization
Packit 67cb25
============================================
Packit 67cb25
Packit 67cb25
.. type:: gsl_qrng
Packit 67cb25
Packit 67cb25
   This is a workspace for computing quasi-random sequences.
Packit 67cb25
Packit 67cb25
.. function:: gsl_qrng * gsl_qrng_alloc (const gsl_qrng_type * T, unsigned int d)
Packit 67cb25
Packit 67cb25
   This function returns a pointer to a newly-created instance of a
Packit 67cb25
   quasi-random sequence generator of type :data:`T` and dimension :data:`d`.
Packit 67cb25
   If there is insufficient memory to create the generator then the
Packit 67cb25
   function returns a null pointer and the error handler is invoked with an
Packit 67cb25
   error code of :macro:`GSL_ENOMEM`.
Packit 67cb25
Packit 67cb25
.. function:: void gsl_qrng_free (gsl_qrng * q)
Packit 67cb25
Packit 67cb25
   This function frees all the memory associated with the generator
Packit 67cb25
   :data:`q`.
Packit 67cb25
Packit 67cb25
.. function:: void gsl_qrng_init (gsl_qrng * q)
Packit 67cb25
Packit 67cb25
   This function reinitializes the generator :data:`q` to its starting point.
Packit 67cb25
   Note that quasi-random sequences do not use a seed and always produce
Packit 67cb25
   the same set of values.
Packit 67cb25
Packit 67cb25
Sampling from a quasi-random number generator
Packit 67cb25
=============================================
Packit 67cb25
Packit 67cb25
.. function:: int gsl_qrng_get (const gsl_qrng * q, double x[])
Packit 67cb25
Packit 67cb25
   This function stores the next point from the sequence generator :data:`q`
Packit 67cb25
   in the array :data:`x`.  The space available for :data:`x` must match the
Packit 67cb25
   dimension of the generator.  The point :data:`x` will lie in the range
Packit 67cb25
   :math:`0 < x_i < 1` for each :math:`x_i`. |inlinefn|
Packit 67cb25
Packit 67cb25
Auxiliary quasi-random number generator functions
Packit 67cb25
=================================================
Packit 67cb25
Packit 67cb25
.. function:: const char * gsl_qrng_name (const gsl_qrng * q)
Packit 67cb25
Packit 67cb25
   This function returns a pointer to the name of the generator.
Packit 67cb25
Packit 67cb25
.. function:: size_t gsl_qrng_size (const gsl_qrng * q)
Packit 67cb25
              void * gsl_qrng_state (const gsl_qrng * q)
Packit 67cb25
Packit 67cb25
   These functions return a pointer to the state of generator :data:`r` and
Packit 67cb25
   its size.  You can use this information to access the state directly.  For
Packit 67cb25
   example, the following code will write the state of a generator to a
Packit 67cb25
   stream::
Packit 67cb25
Packit 67cb25
      void * state = gsl_qrng_state (q);
Packit 67cb25
      size_t n = gsl_qrng_size (q);
Packit 67cb25
      fwrite (state, n, 1, stream);
Packit 67cb25
Packit 67cb25
Saving and restoring quasi-random number generator state
Packit 67cb25
========================================================
Packit 67cb25
Packit 67cb25
.. function:: int gsl_qrng_memcpy (gsl_qrng * dest, const gsl_qrng * src)
Packit 67cb25
Packit 67cb25
   This function copies the quasi-random sequence generator :data:`src` into the
Packit 67cb25
   pre-existing generator :data:`dest`, making :data:`dest` into an exact copy
Packit 67cb25
   of :data:`src`.  The two generators must be of the same type.
Packit 67cb25
Packit 67cb25
.. function:: gsl_qrng * gsl_qrng_clone (const gsl_qrng * q)
Packit 67cb25
Packit 67cb25
   This function returns a pointer to a newly created generator which is an
Packit 67cb25
   exact copy of the generator :data:`q`.
Packit 67cb25
Packit 67cb25
Quasi-random number generator algorithms
Packit 67cb25
========================================
Packit 67cb25
Packit 67cb25
The following quasi-random sequence algorithms are available,
Packit 67cb25
Packit 67cb25
.. type:: gsl_qrng_type
Packit 67cb25
Packit 67cb25
   .. var:: gsl_qrng_niederreiter_2
Packit 67cb25
Packit 67cb25
      This generator uses the algorithm described in Bratley, Fox,
Packit 67cb25
      Niederreiter, ACM Trans. Model. Comp. Sim. 2, 195 (1992). It is
Packit 67cb25
      valid up to 12 dimensions.
Packit 67cb25
Packit 67cb25
   .. var:: gsl_qrng_sobol
Packit 67cb25
Packit 67cb25
      This generator uses the Sobol sequence described in Antonov, Saleev,
Packit 67cb25
      USSR Comput. Maths. Math. Phys. 19, 252 (1980). It is valid up to
Packit 67cb25
      40 dimensions.
Packit 67cb25
Packit 67cb25
   .. var:: gsl_qrng_halton
Packit 67cb25
            gsl_qrng_reversehalton
Packit 67cb25
Packit 67cb25
      These generators use the Halton and reverse Halton sequences described
Packit 67cb25
      in J.H. Halton, Numerische Mathematik, 2, 84-90 (1960) and
Packit 67cb25
      B. Vandewoestyne and R. Cools Computational and Applied
Packit 67cb25
      Mathematics, 189, 1&2, 341-361 (2006).  They are valid up to 1229
Packit 67cb25
      dimensions.
Packit 67cb25
Packit 67cb25
Examples
Packit 67cb25
========
Packit 67cb25
Packit 67cb25
The following program prints the first 1024 points of the 2-dimensional
Packit 67cb25
Sobol sequence.
Packit 67cb25
Packit 67cb25
.. include:: examples/qrng.c
Packit 67cb25
   :code:
Packit 67cb25
Packit 67cb25
Here is the output from the program::
Packit 67cb25
Packit 67cb25
  $ ./a.out
Packit 67cb25
  0.50000 0.50000
Packit 67cb25
  0.75000 0.25000
Packit 67cb25
  0.25000 0.75000
Packit 67cb25
  0.37500 0.37500
Packit 67cb25
  0.87500 0.87500
Packit 67cb25
  0.62500 0.12500
Packit 67cb25
  0.12500 0.62500
Packit 67cb25
  ....
Packit 67cb25
Packit 67cb25
It can be seen that successive points progressively fill-in the spaces
Packit 67cb25
between previous points. 
Packit 67cb25
Packit 67cb25
:numref:`fig_qrng` shows the distribution in the x-y plane of the first
Packit 67cb25
1024 points from the Sobol sequence,
Packit 67cb25
Packit 67cb25
.. _fig_qrng:
Packit 67cb25
Packit 67cb25
.. figure:: /images/qrng.png
Packit 67cb25
   :scale: 60%
Packit 67cb25
Packit 67cb25
   Distribution of the first 1024 points 
Packit 67cb25
   from the quasi-random Sobol sequence
Packit 67cb25
Packit 67cb25
References
Packit 67cb25
==========
Packit 67cb25
Packit 67cb25
The implementations of the quasi-random sequence routines are based on
Packit 67cb25
the algorithms described in the following paper,
Packit 67cb25
Packit 67cb25
* P. Bratley and B.L. Fox and H. Niederreiter, "Algorithm 738: Programs
Packit 67cb25
  to Generate Niederreiter's Low-discrepancy Sequences", ACM
Packit 67cb25
  Transactions on Mathematical Software, Vol.: 20, No.: 4, December, 1994,
Packit 67cb25
  p.: 494--495.