gsl_multifit__fsolver.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /* multifit/fsolver.c
  2. *
  3. * Copyright (C) 1996, 1997, 1998, 1999, 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 <stdlib.h>
  21. #include <string.h>
  22. #include "gsl_errno.h"
  23. #include "gsl_multifit_nlin.h"
  24. gsl_multifit_fsolver *
  25. gsl_multifit_fsolver_alloc (const gsl_multifit_fsolver_type * T,
  26. size_t n, size_t p)
  27. {
  28. int status;
  29. gsl_multifit_fsolver * s;
  30. if (n < p)
  31. {
  32. GSL_ERROR_VAL ("insufficient data points, n < p", GSL_EINVAL, 0);
  33. }
  34. s = (gsl_multifit_fsolver *) malloc (sizeof (gsl_multifit_fsolver));
  35. if (s == 0)
  36. {
  37. GSL_ERROR_VAL ("failed to allocate space for multifit solver struct",
  38. GSL_ENOMEM, 0);
  39. }
  40. s->x = gsl_vector_calloc (p);
  41. if (s->x == 0)
  42. {
  43. free (s);
  44. GSL_ERROR_VAL ("failed to allocate space for x", GSL_ENOMEM, 0);
  45. }
  46. s->f = gsl_vector_calloc (n);
  47. if (s->f == 0)
  48. {
  49. gsl_vector_free (s->x);
  50. free (s);
  51. GSL_ERROR_VAL ("failed to allocate space for f", GSL_ENOMEM, 0);
  52. }
  53. s->dx = gsl_vector_calloc (p);
  54. if (s->dx == 0)
  55. {
  56. gsl_vector_free (s->x);
  57. gsl_vector_free (s->f);
  58. free (s);
  59. GSL_ERROR_VAL ("failed to allocate space for dx", GSL_ENOMEM, 0);
  60. }
  61. s->state = malloc (T->size);
  62. if (s->state == 0)
  63. {
  64. gsl_vector_free (s->dx);
  65. gsl_vector_free (s->x);
  66. gsl_vector_free (s->f);
  67. free (s); /* exception in constructor, avoid memory leak */
  68. GSL_ERROR_VAL ("failed to allocate space for multifit solver state",
  69. GSL_ENOMEM, 0);
  70. }
  71. s->type = T ;
  72. status = (s->type->alloc)(s->state, n, p);
  73. if (status != GSL_SUCCESS)
  74. {
  75. (s->type->free)(s->state);
  76. free (s->state);
  77. gsl_vector_free (s->dx);
  78. gsl_vector_free (s->x);
  79. gsl_vector_free (s->f);
  80. free (s); /* exception in constructor, avoid memory leak */
  81. GSL_ERROR_VAL ("failed to set solver", status, 0);
  82. }
  83. s->function = NULL;
  84. return s;
  85. }
  86. int
  87. gsl_multifit_fsolver_set (gsl_multifit_fsolver * s,
  88. gsl_multifit_function * f,
  89. const gsl_vector * x)
  90. {
  91. if (s->f->size != f->n)
  92. {
  93. GSL_ERROR ("function size does not match solver", GSL_EBADLEN);
  94. }
  95. if (s->x->size != x->size)
  96. {
  97. GSL_ERROR ("vector length does not match solver", GSL_EBADLEN);
  98. }
  99. s->function = f;
  100. gsl_vector_memcpy(s->x,x);
  101. return (s->type->set) (s->state, s->function, s->x, s->f, s->dx);
  102. }
  103. int
  104. gsl_multifit_fsolver_iterate (gsl_multifit_fsolver * s)
  105. {
  106. return (s->type->iterate) (s->state, s->function, s->x, s->f, s->dx);
  107. }
  108. void
  109. gsl_multifit_fsolver_free (gsl_multifit_fsolver * s)
  110. {
  111. (s->type->free) (s->state);
  112. free (s->state);
  113. gsl_vector_free (s->dx);
  114. gsl_vector_free (s->x);
  115. gsl_vector_free (s->f);
  116. free (s);
  117. }
  118. const char *
  119. gsl_multifit_fsolver_name (const gsl_multifit_fsolver * s)
  120. {
  121. return s->type->name;
  122. }
  123. gsl_vector *
  124. gsl_multifit_fsolver_position (const gsl_multifit_fsolver * s)
  125. {
  126. return s->x;
  127. }