gsl_vector__init_source.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /* vector/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_vector) *
  20. FUNCTION (gsl_vector, alloc) (const size_t n)
  21. {
  22. TYPE (gsl_block) * block;
  23. TYPE (gsl_vector) * v;
  24. if (n == 0)
  25. {
  26. GSL_ERROR_VAL ("vector length n must be positive integer",
  27. GSL_EINVAL, 0);
  28. }
  29. v = (TYPE (gsl_vector) *) malloc (sizeof (TYPE (gsl_vector)));
  30. if (v == 0)
  31. {
  32. GSL_ERROR_VAL ("failed to allocate space for vector struct",
  33. GSL_ENOMEM, 0);
  34. }
  35. block = FUNCTION (gsl_block,alloc) (n);
  36. if (block == 0)
  37. {
  38. free (v) ;
  39. GSL_ERROR_VAL ("failed to allocate space for block",
  40. GSL_ENOMEM, 0);
  41. }
  42. v->data = block->data ;
  43. v->size = n;
  44. v->stride = 1;
  45. v->block = block;
  46. v->owner = 1;
  47. return v;
  48. }
  49. TYPE (gsl_vector) *
  50. FUNCTION (gsl_vector, calloc) (const size_t n)
  51. {
  52. size_t i;
  53. TYPE (gsl_vector) * v = FUNCTION (gsl_vector, alloc) (n);
  54. if (v == 0)
  55. return 0;
  56. /* initialize vector to zero */
  57. for (i = 0; i < MULTIPLICITY * n; i++)
  58. {
  59. v->data[i] = 0;
  60. }
  61. return v;
  62. }
  63. TYPE (gsl_vector) *
  64. FUNCTION (gsl_vector, alloc_from_block) (TYPE(gsl_block) * block,
  65. const size_t offset,
  66. const size_t n,
  67. const size_t stride)
  68. {
  69. TYPE (gsl_vector) * v;
  70. if (n == 0)
  71. {
  72. GSL_ERROR_VAL ("vector length n must be positive integer",
  73. GSL_EINVAL, 0);
  74. }
  75. if (stride == 0)
  76. {
  77. GSL_ERROR_VAL ("stride must be positive integer", GSL_EINVAL, 0);
  78. }
  79. if (block->size <= offset + (n - 1) * stride)
  80. {
  81. GSL_ERROR_VAL ("vector would extend past end of block", GSL_EINVAL, 0);
  82. }
  83. v = (TYPE (gsl_vector) *) malloc (sizeof (TYPE (gsl_vector)));
  84. if (v == 0)
  85. {
  86. GSL_ERROR_VAL ("failed to allocate space for vector struct",
  87. GSL_ENOMEM, 0);
  88. }
  89. v->data = block->data + MULTIPLICITY * offset ;
  90. v->size = n;
  91. v->stride = stride;
  92. v->block = block;
  93. v->owner = 0;
  94. return v;
  95. }
  96. TYPE (gsl_vector) *
  97. FUNCTION (gsl_vector, alloc_from_vector) (TYPE(gsl_vector) * w,
  98. const size_t offset,
  99. const size_t n,
  100. const size_t stride)
  101. {
  102. TYPE (gsl_vector) * v;
  103. if (n == 0)
  104. {
  105. GSL_ERROR_VAL ("vector length n must be positive integer",
  106. GSL_EINVAL, 0);
  107. }
  108. if (stride == 0)
  109. {
  110. GSL_ERROR_VAL ("stride must be positive integer", GSL_EINVAL, 0);
  111. }
  112. if (offset + (n - 1) * stride >= w->size)
  113. {
  114. GSL_ERROR_VAL ("vector would extend past end of block", GSL_EINVAL, 0);
  115. }
  116. v = (TYPE (gsl_vector) *) malloc (sizeof (TYPE (gsl_vector)));
  117. if (v == 0)
  118. {
  119. GSL_ERROR_VAL ("failed to allocate space for vector struct",
  120. GSL_ENOMEM, 0);
  121. }
  122. v->data = w->data + MULTIPLICITY * w->stride * offset ;
  123. v->size = n;
  124. v->stride = stride * w->stride;
  125. v->block = w->block;
  126. v->owner = 0;
  127. return v;
  128. }
  129. void
  130. FUNCTION (gsl_vector, free) (TYPE (gsl_vector) * v)
  131. {
  132. if (v->owner)
  133. {
  134. FUNCTION(gsl_block, free) (v->block) ;
  135. }
  136. free (v);
  137. }
  138. void
  139. FUNCTION (gsl_vector, set_all) (TYPE (gsl_vector) * v, BASE x)
  140. {
  141. ATOMIC * const data = v->data;
  142. const size_t n = v->size;
  143. const size_t stride = v->stride;
  144. size_t i;
  145. for (i = 0; i < n; i++)
  146. {
  147. *(BASE *) (data + MULTIPLICITY * i * stride) = x;
  148. }
  149. }
  150. void
  151. FUNCTION (gsl_vector, set_zero) (TYPE (gsl_vector) * v)
  152. {
  153. ATOMIC * const data = v->data;
  154. const size_t n = v->size;
  155. const size_t stride = v->stride;
  156. const BASE zero = ZERO ;
  157. size_t i;
  158. for (i = 0; i < n; i++)
  159. {
  160. *(BASE *) (data + MULTIPLICITY * i * stride) = zero;
  161. }
  162. }
  163. int
  164. FUNCTION (gsl_vector, set_basis) (TYPE (gsl_vector) * v, size_t i)
  165. {
  166. ATOMIC * const data = v->data;
  167. const size_t n = v->size;
  168. const size_t stride = v->stride;
  169. const BASE zero = ZERO ;
  170. const BASE one = ONE;
  171. size_t k;
  172. if (i >= n)
  173. {
  174. GSL_ERROR ("index out of range", GSL_EINVAL);
  175. }
  176. for (k = 0; k < n; k++)
  177. {
  178. *(BASE *) (data + MULTIPLICITY * k * stride) = zero;
  179. }
  180. *(BASE *) (data + MULTIPLICITY * i * stride) = one;
  181. return GSL_SUCCESS;
  182. }