gsl_matrix__view_source.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /* matrix/view_source.c
  2. *
  3. * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 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. QUALIFIED_VIEW (_gsl_matrix,view)
  20. FUNCTION (gsl_matrix, view_array) (QUALIFIER ATOMIC * array,
  21. const size_t n1, const size_t n2)
  22. {
  23. QUALIFIED_VIEW (_gsl_matrix,view) view = NULL_MATRIX_VIEW;
  24. if (n1 == 0)
  25. {
  26. GSL_ERROR_VAL ("matrix dimension n1 must be positive integer",
  27. GSL_EINVAL, view);
  28. }
  29. else if (n2 == 0)
  30. {
  31. GSL_ERROR_VAL ("matrix dimension n2 must be positive integer",
  32. GSL_EINVAL, view);
  33. }
  34. {
  35. TYPE(gsl_matrix) m = NULL_MATRIX;
  36. m.data = (ATOMIC *)array;
  37. m.size1 = n1;
  38. m.size2 = n2;
  39. m.tda = n2;
  40. m.block = 0;
  41. m.owner = 0;
  42. view.matrix = m;
  43. return view;
  44. }
  45. }
  46. QUALIFIED_VIEW (_gsl_matrix,view)
  47. FUNCTION(gsl_matrix, view_array_with_tda) (QUALIFIER ATOMIC * base,
  48. const size_t n1,
  49. const size_t n2,
  50. const size_t tda)
  51. {
  52. QUALIFIED_VIEW (_gsl_matrix,view) view = NULL_MATRIX_VIEW;
  53. if (n1 == 0)
  54. {
  55. GSL_ERROR_VAL ("matrix dimension n1 must be positive integer",
  56. GSL_EINVAL, view);
  57. }
  58. else if (n2 == 0)
  59. {
  60. GSL_ERROR_VAL ("matrix dimension n2 must be positive integer",
  61. GSL_EINVAL, view);
  62. }
  63. else if (n2 > tda)
  64. {
  65. GSL_ERROR_VAL ("matrix dimension n2 must not exceed tda",
  66. GSL_EINVAL, view);
  67. }
  68. {
  69. TYPE(gsl_matrix) m = NULL_MATRIX;
  70. m.data = (ATOMIC *)base;
  71. m.size1 = n1;
  72. m.size2 = n2;
  73. m.tda = tda;
  74. m.block = 0;
  75. m.owner = 0;
  76. view.matrix = m;
  77. return view;
  78. }
  79. }
  80. QUALIFIED_VIEW (_gsl_matrix,view)
  81. FUNCTION(gsl_matrix, view_vector) (QUALIFIED_TYPE(gsl_vector) * v,
  82. const size_t n1,
  83. const size_t n2)
  84. {
  85. QUALIFIED_VIEW (_gsl_matrix,view) view = NULL_MATRIX_VIEW;
  86. if (n1 == 0)
  87. {
  88. GSL_ERROR_VAL ("matrix dimension n1 must be positive integer",
  89. GSL_EINVAL, view);
  90. }
  91. else if (n2 == 0)
  92. {
  93. GSL_ERROR_VAL ("matrix dimension n2 must be positive integer",
  94. GSL_EINVAL, view);
  95. }
  96. else if (v->stride != 1)
  97. {
  98. GSL_ERROR_VAL ("vector must have unit stride",
  99. GSL_EINVAL, view);
  100. }
  101. else if (n1 * n2 > v->size)
  102. {
  103. GSL_ERROR_VAL ("matrix size exceeds size of original",
  104. GSL_EINVAL, view);
  105. }
  106. {
  107. TYPE(gsl_matrix) m = NULL_MATRIX;
  108. m.data = v->data;
  109. m.size1 = n1;
  110. m.size2 = n2;
  111. m.tda = n2;
  112. m.block = v->block;
  113. m.owner = 0;
  114. view.matrix = m;
  115. return view;
  116. }
  117. }
  118. QUALIFIED_VIEW (_gsl_matrix,view)
  119. FUNCTION(gsl_matrix, view_vector_with_tda) (QUALIFIED_TYPE(gsl_vector) * v,
  120. const size_t n1,
  121. const size_t n2,
  122. const size_t tda)
  123. {
  124. QUALIFIED_VIEW (_gsl_matrix,view) view = NULL_MATRIX_VIEW;
  125. if (n1 == 0)
  126. {
  127. GSL_ERROR_VAL ("matrix dimension n1 must be positive integer",
  128. GSL_EINVAL, view);
  129. }
  130. else if (n2 == 0)
  131. {
  132. GSL_ERROR_VAL ("matrix dimension n2 must be positive integer",
  133. GSL_EINVAL, view);
  134. }
  135. else if (v->stride != 1)
  136. {
  137. GSL_ERROR_VAL ("vector must have unit stride",
  138. GSL_EINVAL, view);
  139. }
  140. else if (n2 > tda)
  141. {
  142. GSL_ERROR_VAL ("matrix dimension n2 must not exceed tda",
  143. GSL_EINVAL, view);
  144. }
  145. else if (n1 * tda > v->size)
  146. {
  147. GSL_ERROR_VAL ("matrix size exceeds size of original",
  148. GSL_EINVAL, view);
  149. }
  150. {
  151. TYPE(gsl_matrix) m = NULL_MATRIX;
  152. m.data = v->data;
  153. m.size1 = n1;
  154. m.size2 = n2;
  155. m.tda = tda;
  156. m.block = v->block;
  157. m.owner = 0;
  158. view.matrix = m;
  159. return view;
  160. }
  161. }
  162. #ifdef JUNK
  163. int
  164. FUNCTION (gsl_matrix, view_from_matrix) (TYPE(gsl_matrix) * m,
  165. TYPE(gsl_matrix) * mm,
  166. const size_t k1,
  167. const size_t k2,
  168. const size_t n1,
  169. const size_t n2)
  170. {
  171. if (n1 == 0)
  172. {
  173. GSL_ERROR_VAL ("matrix dimension n1 must be positive integer",
  174. GSL_EINVAL, 0);
  175. }
  176. else if (n2 == 0)
  177. {
  178. GSL_ERROR_VAL ("matrix dimension n2 must be positive integer",
  179. GSL_EINVAL, 0);
  180. }
  181. else if (k1 + n1 > mm->size1)
  182. {
  183. GSL_ERROR_VAL ("submatrix dimension 1 exceeds size of original",
  184. GSL_EINVAL, 0);
  185. }
  186. else if (k2 + n2 > mm->size2)
  187. {
  188. GSL_ERROR_VAL ("submatrix dimension 2 exceeds size of original",
  189. GSL_EINVAL, 0);
  190. }
  191. m->data = mm->data + k1 * mm->tda + k2 ;
  192. m->size1 = n1;
  193. m->size2 = n2;
  194. m->tda = mm->tda;
  195. m->block = mm->block;
  196. m->owner = 0;
  197. return GSL_SUCCESS;
  198. }
  199. int
  200. FUNCTION (gsl_vector, view_row_from_matrix) (TYPE(gsl_vector) * v,
  201. TYPE(gsl_matrix) * m,
  202. const size_t i)
  203. {
  204. const size_t column_length = m->size1;
  205. if (i >= column_length)
  206. {
  207. GSL_ERROR ("row index is out of range", GSL_EINVAL);
  208. }
  209. if (v->block != 0)
  210. {
  211. GSL_ERROR ("vector already has memory allocated to it", GSL_ENOMEM);
  212. }
  213. v->data = m->data + MULTIPLICITY * i * m->tda ;
  214. v->size = m->size2;
  215. v->stride = 1;
  216. return GSL_SUCCESS;
  217. }
  218. int
  219. FUNCTION (gsl_vector, view_col_from_matrix) (TYPE(gsl_vector) * v,
  220. TYPE(gsl_matrix) * m,
  221. const size_t j)
  222. {
  223. const size_t row_length = m->size2;
  224. if (j >= row_length)
  225. {
  226. GSL_ERROR_VAL ("column index is out of range", GSL_EINVAL, 0);
  227. }
  228. if (v->block != 0)
  229. {
  230. GSL_ERROR ("vector already has memory allocated to it", GSL_ENOMEM);
  231. }
  232. v->data = m->data + MULTIPLICITY * j ;
  233. v->size = m->size1;
  234. v->stride = m->tda;
  235. return GSL_SUCCESS;
  236. }
  237. #endif /* JUNK */