gsl_matrix__oper_complex_source.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /* matrix/oper_complex_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. int
  20. FUNCTION (gsl_matrix, add) (TYPE (gsl_matrix) * a,
  21. const TYPE (gsl_matrix) * b)
  22. {
  23. const size_t M = a->size1;
  24. const size_t N = a->size2;
  25. if (b->size1 != M || b->size2 != N)
  26. {
  27. GSL_ERROR ("matrices must have same dimensions", GSL_EBADLEN);
  28. }
  29. else
  30. {
  31. const size_t tda_a = a->tda;
  32. const size_t tda_b = b->tda;
  33. size_t i, j;
  34. for (i = 0; i < M; i++)
  35. {
  36. for (j = 0; j < N; j++)
  37. {
  38. const size_t aij = 2 * (i * tda_a + j);
  39. const size_t bij = 2 * (i * tda_b + j);
  40. a->data[aij] += b->data[bij];
  41. a->data[aij + 1] += b->data[bij + 1];
  42. }
  43. }
  44. return GSL_SUCCESS;
  45. }
  46. }
  47. int
  48. FUNCTION (gsl_matrix, sub) (TYPE (gsl_matrix) * a,
  49. const TYPE (gsl_matrix) * b)
  50. {
  51. const size_t M = a->size1;
  52. const size_t N = a->size2;
  53. if (b->size1 != M || b->size2 != N)
  54. {
  55. GSL_ERROR ("matrices must have same dimensions", GSL_EBADLEN);
  56. }
  57. else
  58. {
  59. const size_t tda_a = a->tda;
  60. const size_t tda_b = b->tda;
  61. size_t i, j;
  62. for (i = 0; i < M; i++)
  63. {
  64. for (j = 0; j < N; j++)
  65. {
  66. const size_t aij = 2 * (i * tda_a + j);
  67. const size_t bij = 2 * (i * tda_b + j);
  68. a->data[aij] -= b->data[bij];
  69. a->data[aij + 1] -= b->data[bij + 1];
  70. }
  71. }
  72. return GSL_SUCCESS;
  73. }
  74. }
  75. int
  76. FUNCTION (gsl_matrix, mul_elements) (TYPE (gsl_matrix) * a,
  77. const TYPE (gsl_matrix) * b)
  78. {
  79. const size_t M = a->size1;
  80. const size_t N = a->size2;
  81. if (b->size1 != M || b->size2 != N)
  82. {
  83. GSL_ERROR ("matrices must have same dimensions", GSL_EBADLEN);
  84. }
  85. else
  86. {
  87. const size_t tda_a = a->tda;
  88. const size_t tda_b = b->tda;
  89. size_t i, j;
  90. for (i = 0; i < M; i++)
  91. {
  92. for (j = 0; j < N; j++)
  93. {
  94. const size_t aij = 2 * (i * tda_a + j);
  95. const size_t bij = 2 * (i * tda_b + j);
  96. ATOMIC ar = a->data[aij];
  97. ATOMIC ai = a->data[aij + 1];
  98. ATOMIC br = b->data[bij];
  99. ATOMIC bi = b->data[bij + 1];
  100. a->data[aij] = ar * br - ai * bi;
  101. a->data[aij + 1] = ar * bi + ai * br;
  102. }
  103. }
  104. return GSL_SUCCESS;
  105. }
  106. }
  107. int
  108. FUNCTION (gsl_matrix, div_elements) (TYPE (gsl_matrix) * a,
  109. const TYPE (gsl_matrix) * b)
  110. {
  111. const size_t M = a->size1;
  112. const size_t N = a->size2;
  113. if (b->size1 != M || b->size2 != N)
  114. {
  115. GSL_ERROR ("matrices must have same dimensions", GSL_EBADLEN);
  116. }
  117. else
  118. {
  119. const size_t tda_a = a->tda;
  120. const size_t tda_b = b->tda;
  121. size_t i, j;
  122. for (i = 0; i < M; i++)
  123. {
  124. for (j = 0; j < N; j++)
  125. {
  126. const size_t aij = 2 * (i * tda_a + j);
  127. const size_t bij = 2 * (i * tda_b + j);
  128. ATOMIC ar = a->data[aij];
  129. ATOMIC ai = a->data[aij + 1];
  130. ATOMIC br = b->data[bij];
  131. ATOMIC bi = b->data[bij + 1];
  132. ATOMIC s = 1.0 / hypot(br, bi);
  133. ATOMIC sbr = s * br;
  134. ATOMIC sbi = s * bi;
  135. a->data[aij] = (ar * sbr + ai * sbi) * s;
  136. a->data[aij + 1] = (ai * sbr - ar * sbi) * s;
  137. }
  138. }
  139. return GSL_SUCCESS;
  140. }
  141. }
  142. int FUNCTION (gsl_matrix, scale) (TYPE (gsl_matrix) * a, const BASE x)
  143. {
  144. const size_t M = a->size1;
  145. const size_t N = a->size2;
  146. const size_t tda = a->tda;
  147. size_t i, j;
  148. ATOMIC xr = GSL_REAL(x);
  149. ATOMIC xi = GSL_IMAG(x);
  150. for (i = 0; i < M; i++)
  151. {
  152. for (j = 0; j < N; j++)
  153. {
  154. const size_t aij = 2 * (i * tda + j);
  155. ATOMIC ar = a->data[aij];
  156. ATOMIC ai = a->data[aij + 1];
  157. a->data[aij] = ar * xr - ai * xi;
  158. a->data[aij + 1] = ar * xi + ai * xr;
  159. }
  160. }
  161. return GSL_SUCCESS;
  162. }
  163. int FUNCTION (gsl_matrix, add_constant) (TYPE (gsl_matrix) * a, const BASE x)
  164. {
  165. const size_t M = a->size1;
  166. const size_t N = a->size2;
  167. const size_t tda = a->tda;
  168. size_t i, j;
  169. for (i = 0; i < M; i++)
  170. {
  171. for (j = 0; j < N; j++)
  172. {
  173. a->data[2 * (i * tda + j)] += GSL_REAL (x);
  174. a->data[2 * (i * tda + j) + 1] += GSL_IMAG (x);
  175. }
  176. }
  177. return GSL_SUCCESS;
  178. }
  179. int FUNCTION (gsl_matrix, add_diagonal) (TYPE (gsl_matrix) * a, const BASE x)
  180. {
  181. const size_t M = a->size1;
  182. const size_t N = a->size2;
  183. const size_t tda = a->tda;
  184. const size_t loop_lim = (M < N ? M : N);
  185. size_t i;
  186. for (i = 0; i < loop_lim; i++)
  187. {
  188. a->data[2 * (i * tda + i)] += GSL_REAL (x);
  189. a->data[2 * (i * tda + i) + 1] += GSL_IMAG (x);
  190. }
  191. return GSL_SUCCESS;
  192. }