1 /* matrix/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_matrix, alloc) (const size_t n1, const size_t n2)
23 TYPE (gsl_block) * block;
24 TYPE (gsl_matrix) * m;
28 GSL_ERROR_VAL ("matrix dimension n1 must be positive integer",
33 GSL_ERROR_VAL ("matrix dimension n2 must be positive integer",
37 m = (TYPE (gsl_matrix) *) malloc (sizeof (TYPE (gsl_matrix)));
41 GSL_ERROR_VAL ("failed to allocate space for matrix struct",
45 /* FIXME: n1*n2 could overflow for large dimensions */
47 block = FUNCTION(gsl_block, alloc) (n1 * n2) ;
51 GSL_ERROR_VAL ("failed to allocate space for block",
55 m->data = block->data;
66 FUNCTION (gsl_matrix, calloc) (const size_t n1, const size_t n2)
70 TYPE (gsl_matrix) * m = FUNCTION (gsl_matrix, alloc) (n1, n2);
75 /* initialize matrix to zero */
77 for (i = 0; i < MULTIPLICITY * n1 * n2; i++)
86 FUNCTION (gsl_matrix, alloc_from_block) (TYPE(gsl_block) * block,
92 TYPE (gsl_matrix) * m;
96 GSL_ERROR_VAL ("matrix dimension n1 must be positive integer",
101 GSL_ERROR_VAL ("matrix dimension n2 must be positive integer",
106 GSL_ERROR_VAL ("matrix dimension d2 must be greater than n2",
109 else if (block->size < offset + n1 * d2)
111 GSL_ERROR_VAL ("matrix size exceeds available block size",
115 m = (TYPE (gsl_matrix) *) malloc (sizeof (TYPE (gsl_matrix)));
119 GSL_ERROR_VAL ("failed to allocate space for matrix struct",
123 m->data = block->data + MULTIPLICITY * offset;
135 FUNCTION (gsl_matrix, alloc_from_matrix) (TYPE(gsl_matrix) * mm,
141 TYPE (gsl_matrix) * m;
145 GSL_ERROR_VAL ("matrix dimension n1 must be positive integer",
150 GSL_ERROR_VAL ("matrix dimension n2 must be positive integer",
153 else if (k1 + n1 > mm->size1)
155 GSL_ERROR_VAL ("submatrix dimension 1 exceeds size of original",
158 else if (k2 + n2 > mm->size2)
160 GSL_ERROR_VAL ("submatrix dimension 2 exceeds size of original",
164 m = (TYPE (gsl_matrix) *) malloc (sizeof (TYPE (gsl_matrix)));
168 GSL_ERROR_VAL ("failed to allocate space for matrix struct",
172 m->data = mm->data + k1 * mm->tda + k2 ;
176 m->block = mm->block;
183 FUNCTION (gsl_matrix, free) (TYPE (gsl_matrix) * m)
187 FUNCTION(gsl_block, free) (m->block);
193 FUNCTION (gsl_matrix, set_identity) (TYPE (gsl_matrix) * m)
196 ATOMIC * const data = m->data;
197 const size_t p = m->size1 ;
198 const size_t q = m->size2 ;
199 const size_t tda = m->tda ;
201 const BASE zero = ZERO;
202 const BASE one = ONE;
204 for (i = 0; i < p; i++)
206 for (j = 0; j < q; j++)
208 *(BASE *) (data + MULTIPLICITY * (i * tda + j)) = ((i == j) ? one : zero);
214 FUNCTION (gsl_matrix, set_zero) (TYPE (gsl_matrix) * m)
217 ATOMIC * const data = m->data;
218 const size_t p = m->size1 ;
219 const size_t q = m->size2 ;
220 const size_t tda = m->tda ;
222 const BASE zero = ZERO;
224 for (i = 0; i < p; i++)
226 for (j = 0; j < q; j++)
228 *(BASE *) (data + MULTIPLICITY * (i * tda + j)) = zero;
234 FUNCTION (gsl_matrix, set_all) (TYPE (gsl_matrix) * m, BASE x)
237 ATOMIC * const data = m->data;
238 const size_t p = m->size1 ;
239 const size_t q = m->size2 ;
240 const size_t tda = m->tda ;
242 for (i = 0; i < p; i++)
244 for (j = 0; j < q; j++)
246 *(BASE *) (data + MULTIPLICITY * (i * tda + j)) = x;