gsl_matrix__oper_source.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /* matrix/oper_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, const TYPE(gsl_matrix) * b)
  21. {
  22. const size_t M = a->size1;
  23. const size_t N = a->size2;
  24. if (b->size1 != M || b->size2 != N)
  25. {
  26. GSL_ERROR ("matrices must have same dimensions", GSL_EBADLEN);
  27. }
  28. else
  29. {
  30. const size_t tda_a = a->tda;
  31. const size_t tda_b = b->tda;
  32. size_t i, j;
  33. for (i = 0; i < M; i++)
  34. {
  35. for (j = 0; j < N; j++)
  36. {
  37. a->data[i * tda_a + j] += b->data[i * tda_b + j];
  38. }
  39. }
  40. return GSL_SUCCESS;
  41. }
  42. }
  43. int
  44. FUNCTION(gsl_matrix, sub) (TYPE(gsl_matrix) * a, const TYPE(gsl_matrix) * b)
  45. {
  46. const size_t M = a->size1;
  47. const size_t N = a->size2;
  48. if (b->size1 != M || b->size2 != N)
  49. {
  50. GSL_ERROR ("matrices must have same dimensions", GSL_EBADLEN);
  51. }
  52. else
  53. {
  54. const size_t tda_a = a->tda;
  55. const size_t tda_b = b->tda;
  56. size_t i, j;
  57. for (i = 0; i < M; i++)
  58. {
  59. for (j = 0; j < N; j++)
  60. {
  61. a->data[i * tda_a + j] -= b->data[i * tda_b + j];
  62. }
  63. }
  64. return GSL_SUCCESS;
  65. }
  66. }
  67. int
  68. FUNCTION(gsl_matrix, mul_elements) (TYPE(gsl_matrix) * a, const TYPE(gsl_matrix) * b)
  69. {
  70. const size_t M = a->size1;
  71. const size_t N = a->size2;
  72. if (b->size1 != M || b->size2 != N)
  73. {
  74. GSL_ERROR ("matrices must have same dimensions", GSL_EBADLEN);
  75. }
  76. else
  77. {
  78. const size_t tda_a = a->tda;
  79. const size_t tda_b = b->tda;
  80. size_t i, j;
  81. for (i = 0; i < M; i++)
  82. {
  83. for (j = 0; j < N; j++)
  84. {
  85. a->data[i * tda_a + j] *= b->data[i * tda_b + j];
  86. }
  87. }
  88. return GSL_SUCCESS;
  89. }
  90. }
  91. int
  92. FUNCTION(gsl_matrix, div_elements) (TYPE(gsl_matrix) * a, const TYPE(gsl_matrix) * b)
  93. {
  94. const size_t M = a->size1;
  95. const size_t N = a->size2;
  96. if (b->size1 != M || b->size2 != N)
  97. {
  98. GSL_ERROR ("matrices must have same dimensions", GSL_EBADLEN);
  99. }
  100. else
  101. {
  102. const size_t tda_a = a->tda;
  103. const size_t tda_b = b->tda;
  104. size_t i, j;
  105. for (i = 0; i < M; i++)
  106. {
  107. for (j = 0; j < N; j++)
  108. {
  109. a->data[i * tda_a + j] /= b->data[i * tda_b + j];
  110. }
  111. }
  112. return GSL_SUCCESS;
  113. }
  114. }
  115. int
  116. FUNCTION(gsl_matrix, scale) (TYPE(gsl_matrix) * a, const double x)
  117. {
  118. const size_t M = a->size1;
  119. const size_t N = a->size2;
  120. const size_t tda = a->tda;
  121. size_t i, j;
  122. for (i = 0; i < M; i++)
  123. {
  124. for (j = 0; j < N; j++)
  125. {
  126. a->data[i * tda + j] *= x;
  127. }
  128. }
  129. return GSL_SUCCESS;
  130. }
  131. int
  132. FUNCTION(gsl_matrix, add_constant) (TYPE(gsl_matrix) * a, const double x)
  133. {
  134. const size_t M = a->size1;
  135. const size_t N = a->size2;
  136. const size_t tda = a->tda;
  137. size_t i, j;
  138. for (i = 0; i < M; i++)
  139. {
  140. for (j = 0; j < N; j++)
  141. {
  142. a->data[i * tda + j] += x;
  143. }
  144. }
  145. return GSL_SUCCESS;
  146. }
  147. int
  148. FUNCTION(gsl_matrix, add_diagonal) (TYPE(gsl_matrix) * a, const double x)
  149. {
  150. const size_t M = a->size1;
  151. const size_t N = a->size2;
  152. const size_t tda = a->tda;
  153. const size_t loop_lim = ( M < N ? M : N );
  154. size_t i;
  155. for (i = 0; i < loop_lim; i++)
  156. {
  157. a->data[i * tda + i] += x;
  158. }
  159. return GSL_SUCCESS;
  160. }