1 /* vector/init_source.c
3 * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2007 Gerard Jungman, Brian Gough
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.
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.
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.
21 FUNCTION (gsl_vector, alloc) (const size_t n)
23 TYPE (gsl_block) * block;
24 TYPE (gsl_vector) * v;
28 GSL_ERROR_VAL ("vector length n must be positive integer",
32 v = (TYPE (gsl_vector) *) malloc (sizeof (TYPE (gsl_vector)));
36 GSL_ERROR_VAL ("failed to allocate space for vector struct",
40 block = FUNCTION (gsl_block,alloc) (n);
46 GSL_ERROR_VAL ("failed to allocate space for block",
50 v->data = block->data ;
60 FUNCTION (gsl_vector, calloc) (const size_t n)
64 TYPE (gsl_vector) * v = FUNCTION (gsl_vector, alloc) (n);
69 /* initialize vector to zero */
71 for (i = 0; i < MULTIPLICITY * n; i++)
80 FUNCTION (gsl_vector, alloc_from_block) (TYPE(gsl_block) * block,
85 TYPE (gsl_vector) * v;
89 GSL_ERROR_VAL ("vector length n must be positive integer",
95 GSL_ERROR_VAL ("stride must be positive integer", GSL_EINVAL, 0);
98 if (block->size <= offset + (n - 1) * stride)
100 GSL_ERROR_VAL ("vector would extend past end of block", GSL_EINVAL, 0);
103 v = (TYPE (gsl_vector) *) malloc (sizeof (TYPE (gsl_vector)));
107 GSL_ERROR_VAL ("failed to allocate space for vector struct",
111 v->data = block->data + MULTIPLICITY * offset ;
121 FUNCTION (gsl_vector, alloc_from_vector) (TYPE(gsl_vector) * w,
126 TYPE (gsl_vector) * v;
130 GSL_ERROR_VAL ("vector length n must be positive integer",
136 GSL_ERROR_VAL ("stride must be positive integer", GSL_EINVAL, 0);
139 if (offset + (n - 1) * stride >= w->size)
141 GSL_ERROR_VAL ("vector would extend past end of block", GSL_EINVAL, 0);
144 v = (TYPE (gsl_vector) *) malloc (sizeof (TYPE (gsl_vector)));
148 GSL_ERROR_VAL ("failed to allocate space for vector struct",
152 v->data = w->data + MULTIPLICITY * w->stride * offset ;
154 v->stride = stride * w->stride;
163 FUNCTION (gsl_vector, free) (TYPE (gsl_vector) * v)
167 FUNCTION(gsl_block, free) (v->block) ;
174 FUNCTION (gsl_vector, set_all) (TYPE (gsl_vector) * v, BASE x)
176 ATOMIC * const data = v->data;
177 const size_t n = v->size;
178 const size_t stride = v->stride;
182 for (i = 0; i < n; i++)
184 *(BASE *) (data + MULTIPLICITY * i * stride) = x;
189 FUNCTION (gsl_vector, set_zero) (TYPE (gsl_vector) * v)
191 ATOMIC * const data = v->data;
192 const size_t n = v->size;
193 const size_t stride = v->stride;
194 const BASE zero = ZERO ;
198 for (i = 0; i < n; i++)
200 *(BASE *) (data + MULTIPLICITY * i * stride) = zero;
205 FUNCTION (gsl_vector, set_basis) (TYPE (gsl_vector) * v, size_t i)
207 ATOMIC * const data = v->data;
208 const size_t n = v->size;
209 const size_t stride = v->stride;
210 const BASE zero = ZERO ;
211 const BASE one = ONE;
217 GSL_ERROR ("index out of range", GSL_EINVAL);
220 for (k = 0; k < n; k++)
222 *(BASE *) (data + MULTIPLICITY * k * stride) = zero;
225 *(BASE *) (data + MULTIPLICITY * i * stride) = one;