gsl_matrix__init_source.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /* matrix/init_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. TYPE (gsl_matrix) *
  20. FUNCTION (gsl_matrix, alloc) (const size_t n1, const size_t n2)
  21. {
  22. TYPE (gsl_block) * block;
  23. TYPE (gsl_matrix) * m;
  24. if (n1 == 0)
  25. {
  26. GSL_ERROR_VAL ("matrix dimension n1 must be positive integer",
  27. GSL_EINVAL, 0);
  28. }
  29. else if (n2 == 0)
  30. {
  31. GSL_ERROR_VAL ("matrix dimension n2 must be positive integer",
  32. GSL_EINVAL, 0);
  33. }
  34. m = (TYPE (gsl_matrix) *) malloc (sizeof (TYPE (gsl_matrix)));
  35. if (m == 0)
  36. {
  37. GSL_ERROR_VAL ("failed to allocate space for matrix struct",
  38. GSL_ENOMEM, 0);
  39. }
  40. /* FIXME: n1*n2 could overflow for large dimensions */
  41. block = FUNCTION(gsl_block, alloc) (n1 * n2) ;
  42. if (block == 0)
  43. {
  44. GSL_ERROR_VAL ("failed to allocate space for block",
  45. GSL_ENOMEM, 0);
  46. }
  47. m->data = block->data;
  48. m->size1 = n1;
  49. m->size2 = n2;
  50. m->tda = n2;
  51. m->block = block;
  52. m->owner = 1;
  53. return m;
  54. }
  55. TYPE (gsl_matrix) *
  56. FUNCTION (gsl_matrix, calloc) (const size_t n1, const size_t n2)
  57. {
  58. size_t i;
  59. TYPE (gsl_matrix) * m = FUNCTION (gsl_matrix, alloc) (n1, n2);
  60. if (m == 0)
  61. return 0;
  62. /* initialize matrix to zero */
  63. for (i = 0; i < MULTIPLICITY * n1 * n2; i++)
  64. {
  65. m->data[i] = 0;
  66. }
  67. return m;
  68. }
  69. TYPE (gsl_matrix) *
  70. FUNCTION (gsl_matrix, alloc_from_block) (TYPE(gsl_block) * block,
  71. const size_t offset,
  72. const size_t n1,
  73. const size_t n2,
  74. const size_t d2)
  75. {
  76. TYPE (gsl_matrix) * m;
  77. if (n1 == 0)
  78. {
  79. GSL_ERROR_VAL ("matrix dimension n1 must be positive integer",
  80. GSL_EINVAL, 0);
  81. }
  82. else if (n2 == 0)
  83. {
  84. GSL_ERROR_VAL ("matrix dimension n2 must be positive integer",
  85. GSL_EINVAL, 0);
  86. }
  87. else if (d2 < n2)
  88. {
  89. GSL_ERROR_VAL ("matrix dimension d2 must be greater than n2",
  90. GSL_EINVAL, 0);
  91. }
  92. else if (block->size < offset + n1 * d2)
  93. {
  94. GSL_ERROR_VAL ("matrix size exceeds available block size",
  95. GSL_EINVAL, 0);
  96. }
  97. m = (TYPE (gsl_matrix) *) malloc (sizeof (TYPE (gsl_matrix)));
  98. if (m == 0)
  99. {
  100. GSL_ERROR_VAL ("failed to allocate space for matrix struct",
  101. GSL_ENOMEM, 0);
  102. }
  103. m->data = block->data + MULTIPLICITY * offset;
  104. m->size1 = n1;
  105. m->size2 = n2;
  106. m->tda = d2;
  107. m->block = block;
  108. m->owner = 0;
  109. return m;
  110. }
  111. TYPE (gsl_matrix) *
  112. FUNCTION (gsl_matrix, alloc_from_matrix) (TYPE(gsl_matrix) * mm,
  113. const size_t k1,
  114. const size_t k2,
  115. const size_t n1,
  116. const size_t n2)
  117. {
  118. TYPE (gsl_matrix) * m;
  119. if (n1 == 0)
  120. {
  121. GSL_ERROR_VAL ("matrix dimension n1 must be positive integer",
  122. GSL_EINVAL, 0);
  123. }
  124. else if (n2 == 0)
  125. {
  126. GSL_ERROR_VAL ("matrix dimension n2 must be positive integer",
  127. GSL_EINVAL, 0);
  128. }
  129. else if (k1 + n1 > mm->size1)
  130. {
  131. GSL_ERROR_VAL ("submatrix dimension 1 exceeds size of original",
  132. GSL_EINVAL, 0);
  133. }
  134. else if (k2 + n2 > mm->size2)
  135. {
  136. GSL_ERROR_VAL ("submatrix dimension 2 exceeds size of original",
  137. GSL_EINVAL, 0);
  138. }
  139. m = (TYPE (gsl_matrix) *) malloc (sizeof (TYPE (gsl_matrix)));
  140. if (m == 0)
  141. {
  142. GSL_ERROR_VAL ("failed to allocate space for matrix struct",
  143. GSL_ENOMEM, 0);
  144. }
  145. m->data = mm->data + k1 * mm->tda + k2 ;
  146. m->size1 = n1;
  147. m->size2 = n2;
  148. m->tda = mm->tda;
  149. m->block = mm->block;
  150. m->owner = 0;
  151. return m;
  152. }
  153. void
  154. FUNCTION (gsl_matrix, free) (TYPE (gsl_matrix) * m)
  155. {
  156. if (m->owner)
  157. {
  158. FUNCTION(gsl_block, free) (m->block);
  159. }
  160. free (m);
  161. }
  162. void
  163. FUNCTION (gsl_matrix, set_identity) (TYPE (gsl_matrix) * m)
  164. {
  165. size_t i, j;
  166. ATOMIC * const data = m->data;
  167. const size_t p = m->size1 ;
  168. const size_t q = m->size2 ;
  169. const size_t tda = m->tda ;
  170. const BASE zero = ZERO;
  171. const BASE one = ONE;
  172. for (i = 0; i < p; i++)
  173. {
  174. for (j = 0; j < q; j++)
  175. {
  176. *(BASE *) (data + MULTIPLICITY * (i * tda + j)) = ((i == j) ? one : zero);
  177. }
  178. }
  179. }
  180. void
  181. FUNCTION (gsl_matrix, set_zero) (TYPE (gsl_matrix) * m)
  182. {
  183. size_t i, j;
  184. ATOMIC * const data = m->data;
  185. const size_t p = m->size1 ;
  186. const size_t q = m->size2 ;
  187. const size_t tda = m->tda ;
  188. const BASE zero = ZERO;
  189. for (i = 0; i < p; i++)
  190. {
  191. for (j = 0; j < q; j++)
  192. {
  193. *(BASE *) (data + MULTIPLICITY * (i * tda + j)) = zero;
  194. }
  195. }
  196. }
  197. void
  198. FUNCTION (gsl_matrix, set_all) (TYPE (gsl_matrix) * m, BASE x)
  199. {
  200. size_t i, j;
  201. ATOMIC * const data = m->data;
  202. const size_t p = m->size1 ;
  203. const size_t q = m->size2 ;
  204. const size_t tda = m->tda ;
  205. for (i = 0; i < p; i++)
  206. {
  207. for (j = 0; j < q; j++)
  208. {
  209. *(BASE *) (data + MULTIPLICITY * (i * tda + j)) = x;
  210. }
  211. }
  212. }