Added script front-end for primer-design code
[htsworkflow.git] / htswanalysis / MACS / lib / gsl / gsl-1.11 / doc / permutation.texi
1 @cindex permutations
2
3 This chapter describes functions for creating and manipulating
4 permutations. A permutation @math{p} is represented by an array of
5 @math{n} integers in the range 0 to @math{n-1}, where each value
6 @math{p_i} occurs once and only once.  The application of a permutation
7 @math{p} to a vector @math{v} yields a new vector @math{v'} where
8 @c{$v'_i = v_{p_i}$}
9 @math{v'_i = v_@{p_i@}}. 
10 For example, the array @math{(0,1,3,2)} represents a permutation
11 which exchanges the last two elements of a four element vector.
12 The corresponding identity permutation is @math{(0,1,2,3)}.   
13
14 Note that the permutations produced by the linear algebra routines
15 correspond to the exchange of matrix columns, and so should be considered
16 as applying to row-vectors in the form @math{v' = v P} rather than
17 column-vectors, when permuting the elements of a vector.
18
19 The functions described in this chapter are defined in the header file
20 @file{gsl_permutation.h}.
21
22 @menu
23 * The Permutation struct::      
24 * Permutation allocation::      
25 * Accessing permutation elements::  
26 * Permutation properties::      
27 * Permutation functions::       
28 * Applying Permutations::       
29 * Reading and writing permutations::  
30 * Permutations in cyclic form::  
31 * Permutation Examples::        
32 * Permutation References and Further Reading::  
33 @end menu
34
35 @node The Permutation struct
36 @section The Permutation struct
37
38 A permutation is defined by a structure containing two components, the size
39 of the permutation and a pointer to the permutation array.  The elements
40 of the permutation array are all of type @code{size_t}.  The
41 @code{gsl_permutation} structure looks like this,
42
43 @example
44 typedef struct
45 @{
46   size_t size;
47   size_t * data;
48 @} gsl_permutation;
49 @end example
50 @comment
51
52 @noindent
53
54 @node Permutation allocation
55 @section Permutation allocation
56
57 @deftypefun {gsl_permutation *} gsl_permutation_alloc (size_t @var{n})
58 This function allocates memory for a new permutation of size @var{n}.
59 The permutation is not initialized and its elements are undefined.  Use
60 the function @code{gsl_permutation_calloc} if you want to create a
61 permutation which is initialized to the identity. A null pointer is
62 returned if insufficient memory is available to create the permutation.
63 @end deftypefun
64
65 @deftypefun {gsl_permutation *} gsl_permutation_calloc (size_t @var{n})
66 This function allocates memory for a new permutation of size @var{n} and
67 initializes it to the identity. A null pointer is returned if
68 insufficient memory is available to create the permutation.
69 @end deftypefun
70
71 @deftypefun void gsl_permutation_init (gsl_permutation * @var{p})
72 @cindex identity permutation
73 This function initializes the permutation @var{p} to the identity, i.e.
74 @math{(0,1,2,@dots{},n-1)}.
75 @end deftypefun
76
77 @deftypefun void gsl_permutation_free (gsl_permutation * @var{p})
78 This function frees all the memory used by the permutation @var{p}.
79 @end deftypefun
80
81 @deftypefun int gsl_permutation_memcpy (gsl_permutation * @var{dest}, const gsl_permutation * @var{src})
82 This function copies the elements of the permutation @var{src} into the
83 permutation @var{dest}.  The two permutations must have the same size.
84 @end deftypefun
85
86 @node Accessing permutation elements
87 @section Accessing permutation elements
88
89 The following functions can be used to access and manipulate
90 permutations.
91
92 @deftypefun size_t gsl_permutation_get (const gsl_permutation * @var{p}, const size_t @var{i})
93 This function returns the value of the @var{i}-th element of the
94 permutation @var{p}.  If @var{i} lies outside the allowed range of 0 to
95 @math{@var{n}-1} then the error handler is invoked and 0 is returned.  @inlinefn{}
96 @end deftypefun
97
98 @deftypefun int gsl_permutation_swap (gsl_permutation * @var{p}, const size_t @var{i}, const size_t @var{j})
99 @cindex exchanging permutation elements
100 @cindex swapping permutation elements
101 This function exchanges the @var{i}-th and @var{j}-th elements of the
102 permutation @var{p}.
103 @end deftypefun
104
105 @node Permutation properties
106 @section Permutation properties
107
108 @deftypefun size_t gsl_permutation_size (const gsl_permutation * @var{p})
109 This function returns the size of the permutation @var{p}.
110 @end deftypefun
111
112 @deftypefun {size_t *} gsl_permutation_data (const gsl_permutation * @var{p})
113 This function returns a pointer to the array of elements in the
114 permutation @var{p}.
115 @end deftypefun
116
117 @deftypefun int gsl_permutation_valid (const gsl_permutation * @var{p})
118 @cindex checking permutation for validity
119 @cindex testing permutation for validity
120 This function checks that the permutation @var{p} is valid.  The @var{n}
121 elements should contain each of the numbers 0 to @math{@var{n}-1} once and only
122 once.
123 @end deftypefun
124
125 @node Permutation functions
126 @section Permutation functions
127
128 @deftypefun void gsl_permutation_reverse (gsl_permutation * @var{p})
129 @cindex reversing a permutation
130 This function reverses the elements of the permutation @var{p}.
131 @end deftypefun
132
133 @deftypefun int gsl_permutation_inverse (gsl_permutation * @var{inv}, const gsl_permutation * @var{p})
134 @cindex inverting a permutation
135 This function computes the inverse of the permutation @var{p}, storing
136 the result in @var{inv}.
137 @end deftypefun
138
139 @deftypefun int gsl_permutation_next (gsl_permutation * @var{p})
140 @cindex iterating through permutations
141 This function advances the permutation @var{p} to the next permutation
142 in lexicographic order and returns @code{GSL_SUCCESS}.  If no further
143 permutations are available it returns @code{GSL_FAILURE} and leaves
144 @var{p} unmodified.  Starting with the identity permutation and
145 repeatedly applying this function will iterate through all possible
146 permutations of a given order.
147 @end deftypefun
148
149 @deftypefun int gsl_permutation_prev (gsl_permutation * @var{p})
150 This function steps backwards from the permutation @var{p} to the
151 previous permutation in lexicographic order, returning
152 @code{GSL_SUCCESS}.  If no previous permutation is available it returns
153 @code{GSL_FAILURE} and leaves @var{p} unmodified.
154 @end deftypefun
155
156 @node Applying Permutations
157 @section Applying Permutations
158
159 @deftypefun int gsl_permute (const size_t * @var{p}, double * @var{data}, size_t @var{stride}, size_t @var{n})
160 This function applies the permutation @var{p} to the array @var{data} of
161 size @var{n} with stride @var{stride}.
162 @end deftypefun
163
164 @deftypefun int gsl_permute_inverse (const size_t * @var{p}, double * @var{data}, size_t @var{stride}, size_t @var{n})
165 This function applies the inverse of the permutation @var{p} to the
166 array @var{data} of size @var{n} with stride @var{stride}.
167 @end deftypefun
168
169 @deftypefun int gsl_permute_vector (const gsl_permutation * @var{p}, gsl_vector * @var{v})
170 This function applies the permutation @var{p} to the elements of the
171 vector @var{v}, considered as a row-vector acted on by a permutation
172 matrix from the right, @math{v' = v P}.  The @math{j}-th column of the
173 permutation matrix @math{P} is given by the @math{p_j}-th column of the
174 identity matrix. The permutation @var{p} and the vector @var{v} must
175 have the same length.
176 @end deftypefun
177
178 @deftypefun int gsl_permute_vector_inverse (const gsl_permutation * @var{p}, gsl_vector * @var{v})
179 This function applies the inverse of the permutation @var{p} to the
180 elements of the vector @var{v}, considered as a row-vector acted on by
181 an inverse permutation matrix from the right, @math{v' = v P^T}.  Note
182 that for permutation matrices the inverse is the same as the transpose.
183 The @math{j}-th column of the permutation matrix @math{P} is given by
184 the @math{p_j}-th column of the identity matrix. The permutation @var{p}
185 and the vector @var{v} must have the same length.
186 @end deftypefun
187
188 @deftypefun int gsl_permutation_mul (gsl_permutation * @var{p}, const gsl_permutation * @var{pa}, const gsl_permutation * @var{pb})
189 This function combines the two permutations @var{pa} and @var{pb} into a
190 single permutation @var{p}, where @math{p = pa . pb}. The permutation
191 @var{p} is equivalent to applying @math{pb} first and then @var{pa}.
192 @end deftypefun
193
194 @node Reading and writing permutations
195 @section Reading and writing permutations
196
197 The library provides functions for reading and writing permutations to a
198 file as binary data or formatted text.
199
200 @deftypefun int gsl_permutation_fwrite (FILE * @var{stream}, const gsl_permutation * @var{p})
201 This function writes the elements of the permutation @var{p} to the
202 stream @var{stream} in binary format.  The function returns
203 @code{GSL_EFAILED} if there was a problem writing to the file.  Since the
204 data is written in the native binary format it may not be portable
205 between different architectures.
206 @end deftypefun
207
208 @deftypefun int gsl_permutation_fread (FILE * @var{stream}, gsl_permutation * @var{p})
209 This function reads into the permutation @var{p} from the open stream
210 @var{stream} in binary format.  The permutation @var{p} must be
211 preallocated with the correct length since the function uses the size of
212 @var{p} to determine how many bytes to read.  The function returns
213 @code{GSL_EFAILED} if there was a problem reading from the file.  The
214 data is assumed to have been written in the native binary format on the
215 same architecture.
216 @end deftypefun
217
218 @deftypefun int gsl_permutation_fprintf (FILE * @var{stream}, const gsl_permutation * @var{p}, const char * @var{format})
219 This function writes the elements of the permutation @var{p}
220 line-by-line to the stream @var{stream} using the format specifier
221 @var{format}, which should be suitable for a type of @var{size_t}. 
222 In ISO C99 the type modifier @code{z} represents @code{size_t}, so
223 @code{"%zu\n"} is a suitable format.@footnote{In versions of the 
224 GNU C library prior to the ISO C99 standard, 
225 the type modifier @code{Z} was used instead.}
226 The function returns @code{GSL_EFAILED} if there was a problem writing
227 to the file.
228 @end deftypefun
229
230 @deftypefun int gsl_permutation_fscanf (FILE * @var{stream}, gsl_permutation * @var{p})
231 This function reads formatted data from the stream @var{stream} into the
232 permutation @var{p}.  The permutation @var{p} must be preallocated with
233 the correct length since the function uses the size of @var{p} to
234 determine how many numbers to read.  The function returns
235 @code{GSL_EFAILED} if there was a problem reading from the file.
236 @end deftypefun
237
238 @node Permutations in cyclic form
239 @section Permutations in cyclic form
240
241 A permutation can be represented in both @dfn{linear} and @dfn{cyclic}
242 notations.  The functions described in this section convert between the
243 two forms.  The linear notation is an index mapping, and has already
244 been described above.  The cyclic notation expresses a permutation as a
245 series of circular rearrangements of groups of elements, or
246 @dfn{cycles}.
247
248 For example, under the cycle (1 2 3), 1 is replaced by 2, 2 is replaced
249 by 3 and 3 is replaced by 1 in a circular fashion. Cycles of different
250 sets of elements can be combined independently, for example (1 2 3) (4
251 5) combines the cycle (1 2 3) with the cycle (4 5), which is an exchange
252 of elements 4 and 5.  A cycle of length one represents an element which
253 is unchanged by the permutation and is referred to as a @dfn{singleton}.
254
255 It can be shown that every permutation can be decomposed into
256 combinations of cycles.  The decomposition is not unique, but can always
257 be rearranged into a standard @dfn{canonical form} by a reordering of
258 elements.  The library uses the canonical form defined in Knuth's
259 @cite{Art of Computer Programming} (Vol 1, 3rd Ed, 1997) Section 1.3.3,
260 p.178.
261
262 The procedure for obtaining the canonical form given by Knuth is,
263
264 @enumerate
265 @item Write all singleton cycles explicitly
266 @item Within each cycle, put the smallest number first
267 @item Order the cycles in decreasing order of the first number in the cycle.
268 @end enumerate
269
270 @noindent
271 For example, the linear representation (2 4 3 0 1) is represented as (1
272 4) (0 2 3) in canonical form. The permutation corresponds to an
273 exchange of elements 1 and 4, and rotation of elements 0, 2 and 3.
274
275 The important property of the canonical form is that it can be
276 reconstructed from the contents of each cycle without the brackets. In
277 addition, by removing the brackets it can be considered as a linear
278 representation of a different permutation. In the example given above
279 the permutation (2 4 3 0 1) would become (1 4 0 2 3).  This mapping has
280 many applications in the theory of permutations.
281
282 @deftypefun int gsl_permutation_linear_to_canonical (gsl_permutation * @var{q}, const gsl_permutation * @var{p})
283 This function computes the canonical form of the permutation @var{p} and
284 stores it in the output argument @var{q}.
285 @end deftypefun
286
287 @deftypefun int gsl_permutation_canonical_to_linear (gsl_permutation * @var{p}, const gsl_permutation * @var{q})
288 This function converts a permutation @var{q} in canonical form back into
289 linear form storing it in the output argument @var{p}.
290 @end deftypefun
291
292 @deftypefun size_t gsl_permutation_inversions (const gsl_permutation * @var{p})
293 This function counts the number of inversions in the permutation
294 @var{p}.  An inversion is any pair of elements that are not in order.
295 For example, the permutation 2031 has three inversions, corresponding to
296 the pairs (2,0) (2,1) and (3,1).  The identity permutation has no
297 inversions.
298 @end deftypefun
299
300 @deftypefun size_t gsl_permutation_linear_cycles (const gsl_permutation * @var{p})
301 This function counts the number of cycles in the permutation @var{p}, given in linear form.
302 @end deftypefun
303
304 @deftypefun size_t gsl_permutation_canonical_cycles (const gsl_permutation * @var{q})
305 This function counts the number of cycles in the permutation @var{q}, given in canonical form.
306 @end deftypefun
307
308
309 @node Permutation Examples
310 @section Examples
311 The example program below creates a random permutation (by shuffling the
312 elements of the identity) and finds its inverse.
313
314 @example
315 @verbatiminclude examples/permshuffle.c
316 @end example
317
318 @noindent
319 Here is the output from the program,
320
321 @example
322 $ ./a.out 
323 initial permutation: 0 1 2 3 4 5 6 7 8 9
324  random permutation: 1 3 5 2 7 6 0 4 9 8
325 inverse permutation: 6 0 3 1 7 2 5 4 9 8
326 @end example
327
328 @noindent
329 The random permutation @code{p[i]} and its inverse @code{q[i]} are
330 related through the identity @code{p[q[i]] = i}, which can be verified
331 from the output.
332
333 The next example program steps forwards through all possible third order
334 permutations, starting from the identity,
335
336 @example
337 @verbatiminclude examples/permseq.c
338 @end example
339
340 @noindent
341 Here is the output from the program,
342
343 @example
344 $ ./a.out 
345  0 1 2
346  0 2 1
347  1 0 2
348  1 2 0
349  2 0 1
350  2 1 0
351 @end example
352
353 @noindent
354 The permutations are generated in lexicographic order.  To reverse the
355 sequence, begin with the final permutation (which is the reverse of the
356 identity) and replace @code{gsl_permutation_next} with
357 @code{gsl_permutation_prev}.
358
359 @node Permutation References and Further Reading
360 @section References and Further Reading
361
362 The subject of permutations is covered extensively in Knuth's
363 @cite{Sorting and Searching},
364
365 @itemize @asis
366 @item
367 Donald E. Knuth, @cite{The Art of Computer Programming: Sorting and
368 Searching} (Vol 3, 3rd Ed, 1997), Addison-Wesley, ISBN 0201896850.
369 @end itemize
370
371 @noindent
372 For the definition of the @dfn{canonical form} see,
373
374 @itemize @asis
375 @item
376 Donald E. Knuth, @cite{The Art of Computer Programming: Fundamental
377 Algorithms} (Vol 1, 3rd Ed, 1997), Addison-Wesley, ISBN 0201896850.
378 Section 1.3.3, @cite{An Unusual Correspondence}, p.178--179.
379 @end itemize
380