| 1 | /* multifit/work.c |
| 2 | * |
| 3 | * Copyright (C) 2000, 2007 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 | |
| 20 | #include "gsl__config.h" |
| 21 | #include "gsl_errno.h" |
| 22 | #include "gsl_multifit.h" |
| 23 | |
| 24 | gsl_multifit_linear_workspace * |
| 25 | gsl_multifit_linear_alloc (size_t n, size_t p) |
| 26 | { |
| 27 | gsl_multifit_linear_workspace *w; |
| 28 | |
| 29 | w = (gsl_multifit_linear_workspace *) |
| 30 | malloc (sizeof (gsl_multifit_linear_workspace)); |
| 31 | |
| 32 | if (w == 0) |
| 33 | { |
| 34 | GSL_ERROR_VAL ("failed to allocate space for multifit_linear struct", |
| 35 | GSL_ENOMEM, 0); |
| 36 | } |
| 37 | |
| 38 | w->n = n; /* number of observations */ |
| 39 | w->p = p; /* number of parameters */ |
| 40 | |
| 41 | w->A = gsl_matrix_alloc (n, p); |
| 42 | |
| 43 | if (w->A == 0) |
| 44 | { |
| 45 | free (w); |
| 46 | GSL_ERROR_VAL ("failed to allocate space for A", GSL_ENOMEM, 0); |
| 47 | } |
| 48 | |
| 49 | w->Q = gsl_matrix_alloc (p, p); |
| 50 | |
| 51 | if (w->Q == 0) |
| 52 | { |
| 53 | gsl_matrix_free (w->A); |
| 54 | free (w); |
| 55 | GSL_ERROR_VAL ("failed to allocate space for Q", GSL_ENOMEM, 0); |
| 56 | } |
| 57 | |
| 58 | w->QSI = gsl_matrix_alloc (p, p); |
| 59 | |
| 60 | if (w->QSI == 0) |
| 61 | { |
| 62 | gsl_matrix_free (w->Q); |
| 63 | gsl_matrix_free (w->A); |
| 64 | free (w); |
| 65 | GSL_ERROR_VAL ("failed to allocate space for QSI", GSL_ENOMEM, 0); |
| 66 | } |
| 67 | |
| 68 | w->S = gsl_vector_alloc (p); |
| 69 | |
| 70 | if (w->S == 0) |
| 71 | { |
| 72 | gsl_matrix_free (w->QSI); |
| 73 | gsl_matrix_free (w->Q); |
| 74 | gsl_matrix_free (w->A); |
| 75 | free (w); |
| 76 | GSL_ERROR_VAL ("failed to allocate space for S", GSL_ENOMEM, 0); |
| 77 | } |
| 78 | |
| 79 | w->t = gsl_vector_alloc (n); |
| 80 | |
| 81 | if (w->t == 0) |
| 82 | { |
| 83 | gsl_vector_free (w->S); |
| 84 | gsl_matrix_free (w->QSI); |
| 85 | gsl_matrix_free (w->Q); |
| 86 | gsl_matrix_free (w->A); |
| 87 | free (w); |
| 88 | GSL_ERROR_VAL ("failed to allocate space for t", GSL_ENOMEM, 0); |
| 89 | } |
| 90 | |
| 91 | w->xt = gsl_vector_calloc (p); |
| 92 | |
| 93 | if (w->xt == 0) |
| 94 | { |
| 95 | gsl_vector_free (w->t); |
| 96 | gsl_vector_free (w->S); |
| 97 | gsl_matrix_free (w->QSI); |
| 98 | gsl_matrix_free (w->Q); |
| 99 | gsl_matrix_free (w->A); |
| 100 | free (w); |
| 101 | GSL_ERROR_VAL ("failed to allocate space for xt", GSL_ENOMEM, 0); |
| 102 | } |
| 103 | |
| 104 | w->D = gsl_vector_calloc (p); |
| 105 | |
| 106 | if (w->D == 0) |
| 107 | { |
| 108 | gsl_vector_free (w->D); |
| 109 | gsl_vector_free (w->t); |
| 110 | gsl_vector_free (w->S); |
| 111 | gsl_matrix_free (w->QSI); |
| 112 | gsl_matrix_free (w->Q); |
| 113 | gsl_matrix_free (w->A); |
| 114 | free (w); |
| 115 | GSL_ERROR_VAL ("failed to allocate space for xt", GSL_ENOMEM, 0); |
| 116 | } |
| 117 | |
| 118 | return w; |
| 119 | } |
| 120 | |
| 121 | void |
| 122 | gsl_multifit_linear_free (gsl_multifit_linear_workspace * work) |
| 123 | { |
| 124 | gsl_matrix_free (work->A); |
| 125 | gsl_matrix_free (work->Q); |
| 126 | gsl_matrix_free (work->QSI); |
| 127 | gsl_vector_free (work->S); |
| 128 | gsl_vector_free (work->t); |
| 129 | gsl_vector_free (work->xt); |
| 130 | gsl_vector_free (work->D); |
| 131 | free (work); |
| 132 | } |
| 133 |