⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 permutation.texi

📁 math library from gnu
💻 TEXI
📖 第 1 页 / 共 2 页
字号:
@end deftypefun@node Reading and writing permutations@section Reading and writing permutationsThe library provides functions for reading and writing permutations to afile as binary data or formatted text.@deftypefun int gsl_permutation_fwrite (FILE * @var{stream}, const gsl_permutation * @var{p})This function writes the elements of the permutation @var{p} to thestream @var{stream} in binary format.  The function returns@code{GSL_EFAILED} if there was a problem writing to the file.  Since thedata is written in the native binary format it may not be portablebetween different architectures.@end deftypefun@deftypefun int gsl_permutation_fread (FILE * @var{stream}, gsl_permutation * @var{p})This function reads into the permutation @var{p} from the open stream@var{stream} in binary format.  The permutation @var{p} must bepreallocated with the correct length since the function uses the size of@var{p} to determine how many bytes to read.  The function returns@code{GSL_EFAILED} if there was a problem reading from the file.  Thedata is assumed to have been written in the native binary format on thesame architecture.@end deftypefun@deftypefun int gsl_permutation_fprintf (FILE * @var{stream}, const gsl_permutation * @var{p}, const char * @var{format})This function writes the elements of the permutation @var{p}line-by-line to the stream @var{stream} using the format specifier@var{format}, which should be suitable for a type of @var{size_t}. In ISO C99 the type modifier @code{z} represents @code{size_t}, so@code{"%zu\n"} is a suitable format.@footnote{In versions of the GNU C library prior to the ISO C99 standard, the type modifier @code{Z} was used instead.}The function returns @code{GSL_EFAILED} if there was a problem writingto the file.@end deftypefun@deftypefun int gsl_permutation_fscanf (FILE * @var{stream}, gsl_permutation * @var{p})This function reads formatted data from the stream @var{stream} into thepermutation @var{p}.  The permutation @var{p} must be preallocated withthe correct length since the function uses the size of @var{p} todetermine how many numbers to read.  The function returns@code{GSL_EFAILED} if there was a problem reading from the file.@end deftypefun@node Permutations in cyclic form@section Permutations in cyclic formA permutation can be represented in both @dfn{linear} and @dfn{cyclic}notations.  The functions described in this section convert between thetwo forms.  The linear notation is an index mapping, and has alreadybeen described above.  The cyclic notation expresses a permutation as aseries of circular rearrangements of groups of elements, or@dfn{cycles}.For example, under the cycle (1 2 3), 1 is replaced by 2, 2 is replacedby 3 and 3 is replaced by 1 in a circular fashion. Cycles of differentsets of elements can be combined independently, for example (1 2 3) (45) combines the cycle (1 2 3) with the cycle (4 5), which is an exchangeof elements 4 and 5.  A cycle of length one represents an element whichis unchanged by the permutation and is referred to as a @dfn{singleton}.It can be shown that every permutation can be decomposed intocombinations of cycles.  The decomposition is not unique, but can alwaysbe rearranged into a standard @dfn{canonical form} by a reordering ofelements.  The library uses the canonical form defined in Knuth's@cite{Art of Computer Programming} (Vol 1, 3rd Ed, 1997) Section 1.3.3,p.178.The procedure for obtaining the canonical form given by Knuth is,@enumerate@item Write all singleton cycles explicitly@item Within each cycle, put the smallest number first@item Order the cycles in decreasing order of the first number in the cycle.@end enumerate@noindentFor example, the linear representation (2 4 3 0 1) is represented as (14) (0 2 3) in canonical form. The permutation corresponds to anexchange of elements 1 and 4, and rotation of elements 0, 2 and 3.The important property of the canonical form is that it can bereconstructed from the contents of each cycle without the brackets. Inaddition, by removing the brackets it can be considered as a linearrepresentation of a different permutation. In the example given abovethe permutation (2 4 3 0 1) would become (1 4 0 2 3).  This mapping hasmany applications in the theory of permutations.@deftypefun int gsl_permutation_linear_to_canonical (gsl_permutation * @var{q}, const gsl_permutation * @var{p})This function computes the canonical form of the permutation @var{p} andstores it in the output argument @var{q}.@end deftypefun@deftypefun int gsl_permutation_canonical_to_linear (gsl_permutation * @var{p}, const gsl_permutation * @var{q})This function converts a permutation @var{q} in canonical form back intolinear form storing it in the output argument @var{p}.@end deftypefun@deftypefun size_t gsl_permutation_inversions (const gsl_permutation * @var{p})This function counts the number of inversions in the permutation@var{p}.  An inversion is any pair of elements that are not in order.For example, the permutation 2031 has three inversions, corresponding tothe pairs (2,0) (2,1) and (3,1).  The identity permutation has noinversions.@end deftypefun@deftypefun size_t gsl_permutation_linear_cycles (const gsl_permutation * @var{p})This function counts the number of cycles in the permutation @var{p}, given in linear form.@end deftypefun@deftypefun size_t gsl_permutation_canonical_cycles (const gsl_permutation * @var{q})This function counts the number of cycles in the permutation @var{q}, given in canonical form.@end deftypefun@node Permutation Examples@section ExamplesThe example program below creates a random permutation (by shuffling theelements of the identity) and finds its inverse.@example@verbatiminclude examples/permshuffle.c@end example@noindentHere is the output from the program,@example$ ./a.out initial permutation: 0 1 2 3 4 5 6 7 8 9 random permutation: 1 3 5 2 7 6 0 4 9 8inverse permutation: 6 0 3 1 7 2 5 4 9 8@end example@noindentThe random permutation @code{p[i]} and its inverse @code{q[i]} arerelated through the identity @code{p[q[i]] = i}, which can be verifiedfrom the output.The next example program steps forwards through all possible third orderpermutations, starting from the identity,@example@verbatiminclude examples/permseq.c@end example@noindentHere is the output from the program,@example$ ./a.out  0 1 2 0 2 1 1 0 2 1 2 0 2 0 1 2 1 0@end example@noindentThe permutations are generated in lexicographic order.  To reverse thesequence, begin with the final permutation (which is the reverse of theidentity) and replace @code{gsl_permutation_next} with@code{gsl_permutation_prev}.@node Permutation References and Further Reading@section References and Further ReadingThe subject of permutations is covered extensively in Knuth's@cite{Sorting and Searching},@itemize @asis@itemDonald E. Knuth, @cite{The Art of Computer Programming: Sorting andSearching} (Vol 3, 3rd Ed, 1997), Addison-Wesley, ISBN 0201896850.@end itemize@noindentFor the definition of the @dfn{canonical form} see,@itemize @asis@itemDonald E. Knuth, @cite{The Art of Computer Programming: FundamentalAlgorithms} (Vol 1, 3rd Ed, 1997), Addison-Wesley, ISBN 0201896850.Section 1.3.3, @cite{An Unusual Correspondence}, p.178--179.@end itemize

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -