gsl_multifit__fdfsolver.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /* multifit/fdfsolver.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_fdfsolver *
  25. gsl_multifit_fdfsolver_alloc (const gsl_multifit_fdfsolver_type * T,
  26. size_t n, size_t p)
  27. {
  28. int status;
  29. gsl_multifit_fdfsolver * s;
  30. if (n < p)
  31. {
  32. GSL_ERROR_VAL ("insufficient data points, n < p", GSL_EINVAL, 0);
  33. }
  34. s = (gsl_multifit_fdfsolver *) malloc (sizeof (gsl_multifit_fdfsolver));
  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->J = gsl_matrix_calloc (n,p);
  54. if (s->J == 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 g", GSL_ENOMEM, 0);
  60. }
  61. s->dx = gsl_vector_calloc (p);
  62. if (s->dx == 0)
  63. {
  64. gsl_matrix_free (s->J);
  65. gsl_vector_free (s->x);
  66. gsl_vector_free (s->f);
  67. free (s);
  68. GSL_ERROR_VAL ("failed to allocate space for dx", GSL_ENOMEM, 0);
  69. }
  70. s->state = malloc (T->size);
  71. if (s->state == 0)
  72. {
  73. gsl_vector_free (s->dx);
  74. gsl_vector_free (s->x);
  75. gsl_vector_free (s->f);
  76. gsl_matrix_free (s->J);
  77. free (s); /* exception in constructor, avoid memory leak */
  78. GSL_ERROR_VAL ("failed to allocate space for multifit solver state",
  79. GSL_ENOMEM, 0);
  80. }
  81. s->type = T ;
  82. status = (s->type->alloc)(s->state, n, p);
  83. if (status != GSL_SUCCESS)
  84. {
  85. free (s->state);
  86. gsl_vector_free (s->dx);
  87. gsl_vector_free (s->x);
  88. gsl_vector_free (s->f);
  89. gsl_matrix_free (s->J);
  90. free (s); /* exception in constructor, avoid memory leak */
  91. GSL_ERROR_VAL ("failed to set solver", status, 0);
  92. }
  93. s->fdf = NULL;
  94. return s;
  95. }
  96. int
  97. gsl_multifit_fdfsolver_set (gsl_multifit_fdfsolver * s,
  98. gsl_multifit_function_fdf * f,
  99. const gsl_vector * x)
  100. {
  101. if (s->f->size != f->n)
  102. {
  103. GSL_ERROR ("function size does not match solver", GSL_EBADLEN);
  104. }
  105. if (s->x->size != x->size)
  106. {
  107. GSL_ERROR ("vector length does not match solver", GSL_EBADLEN);
  108. }
  109. s->fdf = f;
  110. gsl_vector_memcpy(s->x,x);
  111. return (s->type->set) (s->state, s->fdf, s->x, s->f, s->J, s->dx);
  112. }
  113. int
  114. gsl_multifit_fdfsolver_iterate (gsl_multifit_fdfsolver * s)
  115. {
  116. return (s->type->iterate) (s->state, s->fdf, s->x, s->f, s->J, s->dx);
  117. }
  118. void
  119. gsl_multifit_fdfsolver_free (gsl_multifit_fdfsolver * s)
  120. {
  121. (s->type->free) (s->state);
  122. free (s->state);
  123. gsl_vector_free (s->dx);
  124. gsl_vector_free (s->x);
  125. gsl_vector_free (s->f);
  126. gsl_matrix_free (s->J);
  127. free (s);
  128. }
  129. const char *
  130. gsl_multifit_fdfsolver_name (const gsl_multifit_fdfsolver * s)
  131. {
  132. return s->type->name;
  133. }
  134. gsl_vector *
  135. gsl_multifit_fdfsolver_position (const gsl_multifit_fdfsolver * s)
  136. {
  137. return s->x;
  138. }