gsl_permutation__permute_source.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /* permutation/permute_source.c
  2. *
  3. * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2007 Brian Gough
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. */
  19. /* In-place Permutations
  20. permute: OUT[i] = IN[perm[i]] i = 0 .. N-1
  21. invpermute: OUT[perm[i]] = IN[i] i = 0 .. N-1
  22. PERM is an index map, i.e. a vector which contains a permutation of
  23. the integers 0 .. N-1.
  24. From Knuth "Sorting and Searching", Volume 3 (3rd ed), Section 5.2
  25. Exercise 10 (answers), p 617
  26. FIXME: these have not been fully tested.
  27. */
  28. int
  29. TYPE (gsl_permute) (const size_t * p, ATOMIC * data, const size_t stride, const size_t n)
  30. {
  31. size_t i, k, pk;
  32. for (i = 0; i < n; i++)
  33. {
  34. k = p[i];
  35. while (k > i)
  36. k = p[k];
  37. if (k < i)
  38. continue ;
  39. /* Now have k == i, i.e the least in its cycle */
  40. pk = p[k];
  41. if (pk == i)
  42. continue ;
  43. /* shuffle the elements of the cycle */
  44. {
  45. unsigned int a;
  46. ATOMIC t[MULTIPLICITY];
  47. for (a = 0; a < MULTIPLICITY; a++)
  48. t[a] = data[i*stride*MULTIPLICITY + a];
  49. while (pk != i)
  50. {
  51. for (a = 0; a < MULTIPLICITY; a++)
  52. {
  53. ATOMIC r1 = data[pk*stride*MULTIPLICITY + a];
  54. data[k*stride*MULTIPLICITY + a] = r1;
  55. }
  56. k = pk;
  57. pk = p[k];
  58. };
  59. for (a = 0; a < MULTIPLICITY; a++)
  60. data[k*stride*MULTIPLICITY + a] = t[a];
  61. }
  62. }
  63. return GSL_SUCCESS;
  64. }
  65. int
  66. FUNCTION (gsl_permute,inverse) (const size_t * p, ATOMIC * data, const size_t stride, const size_t n)
  67. {
  68. size_t i, k, pk;
  69. for (i = 0; i < n; i++)
  70. {
  71. k = p[i];
  72. while (k > i)
  73. k = p[k];
  74. if (k < i)
  75. continue ;
  76. /* Now have k == i, i.e the least in its cycle */
  77. pk = p[k];
  78. if (pk == i)
  79. continue ;
  80. /* shuffle the elements of the cycle in the inverse direction */
  81. {
  82. unsigned int a;
  83. ATOMIC t[MULTIPLICITY];
  84. for (a = 0; a < MULTIPLICITY; a++)
  85. t[a] = data[k*stride*MULTIPLICITY+a];
  86. while (pk != i)
  87. {
  88. for (a = 0; a < MULTIPLICITY; a++)
  89. {
  90. ATOMIC r1 = data[pk*stride*MULTIPLICITY + a];
  91. data[pk*stride*MULTIPLICITY + a] = t[a];
  92. t[a] = r1;
  93. }
  94. k = pk;
  95. pk = p[k];
  96. };
  97. for (a = 0; a < MULTIPLICITY; a++)
  98. data[pk*stride*MULTIPLICITY+a] = t[a];
  99. }
  100. }
  101. return GSL_SUCCESS;
  102. }
  103. int
  104. TYPE (gsl_permute_vector) (const gsl_permutation * p, TYPE (gsl_vector) * v)
  105. {
  106. if (v->size != p->size)
  107. {
  108. GSL_ERROR ("vector and permutation must be the same length", GSL_EBADLEN);
  109. }
  110. TYPE (gsl_permute) (p->data, v->data, v->stride, v->size) ;
  111. return GSL_SUCCESS;
  112. }
  113. int
  114. FUNCTION (gsl_permute_vector,inverse) (const gsl_permutation * p, TYPE (gsl_vector) * v)
  115. {
  116. if (v->size != p->size)
  117. {
  118. GSL_ERROR ("vector and permutation must be the same length", GSL_EBADLEN);
  119. }
  120. FUNCTION (gsl_permute,inverse) (p->data, v->data, v->stride, v->size) ;
  121. return GSL_SUCCESS;
  122. }