Blame doc/multiset.rst

Packit 67cb25
.. index:: multisets
Packit 67cb25
Packit 67cb25
*********
Packit 67cb25
Multisets
Packit 67cb25
*********
Packit 67cb25
Packit 67cb25
.. include:: include.rst
Packit 67cb25
Packit 67cb25
This chapter describes functions for creating and manipulating multisets. A
Packit 67cb25
multiset :math:`c` is represented by an array of :math:`k` integers in the range
Packit 67cb25
0 to :math:`n - 1`, where each value :math:`c_i` may occur more than once.  The
Packit 67cb25
multiset :math:`c` corresponds to indices of :math:`k` elements chosen from an
Packit 67cb25
:math:`n` element vector with replacement.  In mathematical terms, :math:`n` is
Packit 67cb25
the cardinality of the multiset while :math:`k` is the maximum multiplicity of
Packit 67cb25
any value.  Multisets are useful, for example, when iterating over the indices
Packit 67cb25
of a :math:`k`-th order symmetric tensor in :math:`n`-space.
Packit 67cb25
Packit 67cb25
The functions described in this chapter are defined in the header file
Packit 67cb25
:file:`gsl_multiset.h`.
Packit 67cb25
Packit 67cb25
The Multiset struct
Packit 67cb25
===================
Packit 67cb25
Packit 67cb25
.. type:: gsl_multiset
Packit 67cb25
Packit 67cb25
   A multiset is defined by a structure containing three components, the
Packit 67cb25
   values of :math:`n` and :math:`k`, and a pointer to the multiset array.
Packit 67cb25
   The elements of the multiset array are all of type :code:`size_t`, and
Packit 67cb25
   are stored in increasing order.  The :type:`gsl_multiset` structure
Packit 67cb25
   looks like this::
Packit 67cb25
Packit 67cb25
      typedef struct
Packit 67cb25
      {
Packit 67cb25
        size_t n;
Packit 67cb25
        size_t k;
Packit 67cb25
        size_t *data;
Packit 67cb25
      } gsl_multiset;
Packit 67cb25
Packit 67cb25
Multiset allocation
Packit 67cb25
===================
Packit 67cb25
Packit 67cb25
.. function:: gsl_multiset * gsl_multiset_alloc (size_t n, size_t k)
Packit 67cb25
Packit 67cb25
   This function allocates memory for a new multiset with parameters :data:`n`,
Packit 67cb25
   :data:`k`.  The multiset is not initialized and its elements are undefined.  Use
Packit 67cb25
   the function :func:`gsl_multiset_calloc` if you want to create a multiset which
Packit 67cb25
   is initialized to the lexicographically first multiset element. A null pointer
Packit 67cb25
   is returned if insufficient memory is available to create the multiset.
Packit 67cb25
Packit 67cb25
.. function:: gsl_multiset * gsl_multiset_calloc (size_t n, size_t k)
Packit 67cb25
Packit 67cb25
   This function allocates memory for a new multiset with parameters :data:`n`,
Packit 67cb25
   :data:`k` and initializes it to the lexicographically first multiset element. A
Packit 67cb25
   null pointer is returned if insufficient memory is available to create the
Packit 67cb25
   multiset.
Packit 67cb25
Packit 67cb25
.. function:: void gsl_multiset_init_first (gsl_multiset * c)
Packit 67cb25
Packit 67cb25
   This function initializes the multiset :data:`c` to the lexicographically first
Packit 67cb25
   multiset element, i.e. :math:`0` repeated :math:`k` times.
Packit 67cb25
Packit 67cb25
.. function:: void gsl_multiset_init_last (gsl_multiset * c)
Packit 67cb25
Packit 67cb25
   This function initializes the multiset :data:`c` to the lexicographically last
Packit 67cb25
   multiset element, i.e. :math:`n-1` repeated :math:`k` times.
Packit 67cb25
Packit 67cb25
.. function:: void gsl_multiset_free (gsl_multiset * c)
Packit 67cb25
Packit 67cb25
   This function frees all the memory used by the multiset :data:`c`.
Packit 67cb25
Packit 67cb25
.. function:: int gsl_multiset_memcpy (gsl_multiset * dest, const gsl_multiset * src)
Packit 67cb25
Packit 67cb25
   This function copies the elements of the multiset :data:`src` into the
Packit 67cb25
   multiset :data:`dest`.  The two multisets must have the same size.
Packit 67cb25
Packit 67cb25
Accessing multiset elements
Packit 67cb25
===========================
Packit 67cb25
Packit 67cb25
The following function can be used to access the elements of a multiset.
Packit 67cb25
Packit 67cb25
.. function:: size_t gsl_multiset_get (const gsl_multiset * c, const size_t i)
Packit 67cb25
Packit 67cb25
   This function returns the value of the :data:`i`-th element of the
Packit 67cb25
   multiset :data:`c`.  If :data:`i` lies outside the allowed range of 0 to
Packit 67cb25
   :math:`k - 1` then the error handler is invoked and 0 is returned. |inlinefn|
Packit 67cb25
Packit 67cb25
Multiset properties
Packit 67cb25
===================
Packit 67cb25
Packit 67cb25
.. function:: size_t gsl_multiset_n (const gsl_multiset * c)
Packit 67cb25
Packit 67cb25
   This function returns the range (:math:`n`) of the multiset :data:`c`.
Packit 67cb25
Packit 67cb25
.. function:: size_t gsl_multiset_k (const gsl_multiset * c)
Packit 67cb25
Packit 67cb25
   This function returns the number of elements (:math:`k`) in the multiset :data:`c`.
Packit 67cb25
Packit 67cb25
.. function:: size_t * gsl_multiset_data (const gsl_multiset * c)
Packit 67cb25
Packit 67cb25
   This function returns a pointer to the array of elements in the
Packit 67cb25
   multiset :data:`c`.
Packit 67cb25
Packit 67cb25
.. index::
Packit 67cb25
   single: checking multiset for validity
Packit 67cb25
   single: testing multiset for validity
Packit 67cb25
Packit 67cb25
.. function:: int gsl_multiset_valid (gsl_multiset * c)
Packit 67cb25
Packit 67cb25
   This function checks that the multiset :data:`c` is valid.  The :data:`k`
Packit 67cb25
   elements should lie in the range 0 to :math:`n - 1`, with each
Packit 67cb25
   value occurring in nondecreasing order.
Packit 67cb25
Packit 67cb25
Multiset functions
Packit 67cb25
==================
Packit 67cb25
Packit 67cb25
.. index:: iterating through multisets
Packit 67cb25
Packit 67cb25
.. function:: int gsl_multiset_next (gsl_multiset * c)
Packit 67cb25
Packit 67cb25
   This function advances the multiset :data:`c` to the next multiset element in
Packit 67cb25
   lexicographic order and returns :macro:`GSL_SUCCESS`.  If no further multisets
Packit 67cb25
   elements are available it returns :macro:`GSL_FAILURE` and leaves :data:`c`
Packit 67cb25
   unmodified.  Starting with the first multiset and repeatedly applying this
Packit 67cb25
   function will iterate through all possible multisets of a given order.
Packit 67cb25
Packit 67cb25
.. function:: int gsl_multiset_prev (gsl_multiset * c)
Packit 67cb25
Packit 67cb25
   This function steps backwards from the multiset :data:`c` to the previous
Packit 67cb25
   multiset element in lexicographic order, returning :macro:`GSL_SUCCESS`.  If no
Packit 67cb25
   previous multiset is available it returns :macro:`GSL_FAILURE` and leaves :data:`c`
Packit 67cb25
   unmodified.
Packit 67cb25
Packit 67cb25
Reading and writing multisets
Packit 67cb25
=============================
Packit 67cb25
Packit 67cb25
The library provides functions for reading and writing multisets to a
Packit 67cb25
file as binary data or formatted text.
Packit 67cb25
Packit 67cb25
.. function:: int gsl_multiset_fwrite (FILE * stream, const gsl_multiset * c)
Packit 67cb25
Packit 67cb25
   This function writes the elements of the multiset :data:`c` to the
Packit 67cb25
   stream :data:`stream` in binary format.  The function returns
Packit 67cb25
   :macro:`GSL_EFAILED` if there was a problem writing to the file.  Since the
Packit 67cb25
   data is written in the native binary format it may not be portable
Packit 67cb25
   between different architectures.
Packit 67cb25
Packit 67cb25
.. function:: int gsl_multiset_fread (FILE * stream, gsl_multiset * c)
Packit 67cb25
Packit 67cb25
   This function reads elements from the open stream :data:`stream` into the
Packit 67cb25
   multiset :data:`c` in binary format.  The multiset :data:`c` must be
Packit 67cb25
   preallocated with correct values of :math:`n` and :math:`k` since the
Packit 67cb25
   function uses the size of :data:`c` to determine how many bytes to read.
Packit 67cb25
   The function returns :macro:`GSL_EFAILED` if there was a problem reading
Packit 67cb25
   from the file.  The data is assumed to have been written in the native
Packit 67cb25
   binary format on the same architecture.
Packit 67cb25
Packit 67cb25
.. function:: int gsl_multiset_fprintf (FILE * stream, const gsl_multiset * c, const char * format)
Packit 67cb25
Packit 67cb25
   This function writes the elements of the multiset :data:`c`
Packit 67cb25
   line-by-line to the stream :data:`stream` using the format specifier
Packit 67cb25
   :data:`format`, which should be suitable for a type of :code:`size_t`.
Packit 67cb25
   In ISO C99 the type modifier :code:`z` represents :code:`size_t`, so
Packit 67cb25
   :code:`"%zu\n"` is a suitable format [#f1]_.
Packit 67cb25
   The function returns :macro:`GSL_EFAILED` if there was a problem writing to the file.
Packit 67cb25
Packit 67cb25
.. function:: int gsl_multiset_fscanf (FILE * stream, gsl_multiset * c)
Packit 67cb25
Packit 67cb25
   This function reads formatted data from the stream :data:`stream` into the
Packit 67cb25
   multiset :data:`c`.  The multiset :data:`c` must be preallocated with
Packit 67cb25
   correct values of :math:`n` and :math:`k` since the function uses the size of :data:`c` to
Packit 67cb25
   determine how many numbers to read.  The function returns
Packit 67cb25
   :macro:`GSL_EFAILED` if there was a problem reading from the file.
Packit 67cb25
Packit 67cb25
Examples
Packit 67cb25
========
Packit 67cb25
Packit 67cb25
The example program below prints all multisets elements containing the values
Packit 67cb25
:math:`{0,1,2,3}` ordered by size.  Multiset elements of the same size are
Packit 67cb25
ordered lexicographically.
Packit 67cb25
Packit 67cb25
.. include:: examples/multiset.c
Packit 67cb25
   :code:
Packit 67cb25
Packit 67cb25
Here is the output from the program,
Packit 67cb25
Packit 67cb25
.. include:: examples/multiset.txt
Packit 67cb25
   :code:
Packit 67cb25
Packit 67cb25
All 70 multisets are generated and sorted lexicographically.
Packit 67cb25
Packit 67cb25
.. rubric:: Footnotes
Packit 67cb25
Packit 67cb25
.. [#f1] In versions of the GNU C library prior to the ISO C99 standard,
Packit 67cb25
         the type modifier :code:`Z` was used instead.