gsl_matrix__swap_source.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /* matrix/swap_source.c
  2. *
  3. * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2007 Gerard Jungman, 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. int
  20. FUNCTION (gsl_matrix, swap_rows) (TYPE (gsl_matrix) * m,
  21. const size_t i, const size_t j)
  22. {
  23. const size_t size1 = m->size1;
  24. const size_t size2 = m->size2;
  25. if (i >= size1)
  26. {
  27. GSL_ERROR ("first row index is out of range", GSL_EINVAL);
  28. }
  29. if (j >= size1)
  30. {
  31. GSL_ERROR ("second row index is out of range", GSL_EINVAL);
  32. }
  33. if (i != j)
  34. {
  35. ATOMIC *row1 = m->data + MULTIPLICITY * i * m->tda;
  36. ATOMIC *row2 = m->data + MULTIPLICITY * j * m->tda;
  37. size_t k;
  38. for (k = 0; k < MULTIPLICITY * size2; k++)
  39. {
  40. ATOMIC tmp = row1[k] ;
  41. row1[k] = row2[k] ;
  42. row2[k] = tmp ;
  43. }
  44. }
  45. return GSL_SUCCESS;
  46. }
  47. int
  48. FUNCTION (gsl_matrix, swap_columns) (TYPE (gsl_matrix) * m,
  49. const size_t i, const size_t j)
  50. {
  51. const size_t size1 = m->size1;
  52. const size_t size2 = m->size2;
  53. if (i >= size2)
  54. {
  55. GSL_ERROR ("first column index is out of range", GSL_EINVAL);
  56. }
  57. if (j >= size2)
  58. {
  59. GSL_ERROR ("second column index is out of range", GSL_EINVAL);
  60. }
  61. if (i != j)
  62. {
  63. ATOMIC *col1 = m->data + MULTIPLICITY * i;
  64. ATOMIC *col2 = m->data + MULTIPLICITY * j;
  65. size_t p;
  66. for (p = 0; p < size1; p++)
  67. {
  68. size_t k;
  69. size_t n = p * MULTIPLICITY * m->tda;
  70. for (k = 0; k < MULTIPLICITY; k++)
  71. {
  72. ATOMIC tmp = col1[n+k] ;
  73. col1[n+k] = col2[n+k] ;
  74. col2[n+k] = tmp ;
  75. }
  76. }
  77. }
  78. return GSL_SUCCESS;
  79. }
  80. int
  81. FUNCTION (gsl_matrix, swap_rowcol) (TYPE (gsl_matrix) * m,
  82. const size_t i, const size_t j)
  83. {
  84. const size_t size1 = m->size1;
  85. const size_t size2 = m->size2;
  86. if (size1 != size2)
  87. {
  88. GSL_ERROR ("matrix must be square to swap row and column", GSL_ENOTSQR);
  89. }
  90. if (i >= size1)
  91. {
  92. GSL_ERROR ("row index is out of range", GSL_EINVAL);
  93. }
  94. if (j >= size2)
  95. {
  96. GSL_ERROR ("column index is out of range", GSL_EINVAL);
  97. }
  98. {
  99. ATOMIC *row = m->data + MULTIPLICITY * i * m->tda;
  100. ATOMIC *col = m->data + MULTIPLICITY * j;
  101. size_t p;
  102. for (p = 0; p < size1; p++)
  103. {
  104. size_t k;
  105. size_t r = p * MULTIPLICITY;
  106. size_t c = p * MULTIPLICITY * m->tda;
  107. for (k = 0; k < MULTIPLICITY; k++)
  108. {
  109. ATOMIC tmp = col[c+k] ;
  110. col[c+k] = row[r+k] ;
  111. row[r+k] = tmp ;
  112. }
  113. }
  114. }
  115. return GSL_SUCCESS;
  116. }
  117. int
  118. FUNCTION (gsl_matrix, transpose) (TYPE (gsl_matrix) * m)
  119. {
  120. const size_t size1 = m->size1;
  121. const size_t size2 = m->size2;
  122. size_t i, j, k;
  123. if (size1 != size2)
  124. {
  125. GSL_ERROR ("matrix must be square to take transpose", GSL_ENOTSQR);
  126. }
  127. for (i = 0; i < size1; i++)
  128. {
  129. for (j = i + 1 ; j < size2 ; j++)
  130. {
  131. for (k = 0; k < MULTIPLICITY; k++)
  132. {
  133. size_t e1 = (i * m->tda + j) * MULTIPLICITY + k ;
  134. size_t e2 = (j * m->tda + i) * MULTIPLICITY + k ;
  135. {
  136. ATOMIC tmp = m->data[e1] ;
  137. m->data[e1] = m->data[e2] ;
  138. m->data[e2] = tmp ;
  139. }
  140. }
  141. }
  142. }
  143. return GSL_SUCCESS;
  144. }
  145. int
  146. FUNCTION (gsl_matrix, transpose_memcpy) (TYPE (gsl_matrix) * dest,
  147. const TYPE (gsl_matrix) * src)
  148. {
  149. const size_t src_size1 = src->size1;
  150. const size_t src_size2 = src->size2;
  151. const size_t dest_size1 = dest->size1;
  152. const size_t dest_size2 = dest->size2;
  153. size_t i, j, k;
  154. if (dest_size2 != src_size1 || dest_size1 != src_size2)
  155. {
  156. GSL_ERROR ("dimensions of dest matrix must be transpose of src matrix",
  157. GSL_EBADLEN);
  158. }
  159. for (i = 0; i < dest_size1; i++)
  160. {
  161. for (j = 0 ; j < dest_size2; j++)
  162. {
  163. for (k = 0; k < MULTIPLICITY; k++)
  164. {
  165. size_t e1 = (i * dest->tda + j) * MULTIPLICITY + k ;
  166. size_t e2 = (j * src->tda + i) * MULTIPLICITY + k ;
  167. dest->data[e1] = src->data[e2] ;
  168. }
  169. }
  170. }
  171. return GSL_SUCCESS;
  172. }