gsl_multifit__work.c 3.3 KB

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