gsl_multiroots__fsolver.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /* multiroots/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_multiroots.h"
  24. gsl_multiroot_fsolver *
  25. gsl_multiroot_fsolver_alloc (const gsl_multiroot_fsolver_type * T,
  26. size_t n)
  27. {
  28. int status;
  29. gsl_multiroot_fsolver * s;
  30. s = (gsl_multiroot_fsolver *) malloc (sizeof (gsl_multiroot_fsolver));
  31. if (s == 0)
  32. {
  33. GSL_ERROR_VAL ("failed to allocate space for multiroot solver struct",
  34. GSL_ENOMEM, 0);
  35. }
  36. s->x = gsl_vector_calloc (n);
  37. if (s->x == 0)
  38. {
  39. free (s);
  40. GSL_ERROR_VAL ("failed to allocate space for x", GSL_ENOMEM, 0);
  41. }
  42. s->f = gsl_vector_calloc (n);
  43. if (s->f == 0)
  44. {
  45. gsl_vector_free (s->x);
  46. free (s);
  47. GSL_ERROR_VAL ("failed to allocate space for f", GSL_ENOMEM, 0);
  48. }
  49. s->dx = gsl_vector_calloc (n);
  50. if (s->dx == 0)
  51. {
  52. gsl_vector_free (s->x);
  53. gsl_vector_free (s->f);
  54. free (s);
  55. GSL_ERROR_VAL ("failed to allocate space for dx", GSL_ENOMEM, 0);
  56. }
  57. s->state = malloc (T->size);
  58. if (s->state == 0)
  59. {
  60. gsl_vector_free (s->dx);
  61. gsl_vector_free (s->x);
  62. gsl_vector_free (s->f);
  63. free (s); /* exception in constructor, avoid memory leak */
  64. GSL_ERROR_VAL ("failed to allocate space for multiroot solver state",
  65. GSL_ENOMEM, 0);
  66. }
  67. s->type = T ;
  68. status = (s->type->alloc)(s->state, n);
  69. if (status != GSL_SUCCESS)
  70. {
  71. (s->type->free)(s->state);
  72. free (s->state);
  73. gsl_vector_free (s->dx);
  74. gsl_vector_free (s->x);
  75. gsl_vector_free (s->f);
  76. free (s); /* exception in constructor, avoid memory leak */
  77. GSL_ERROR_VAL ("failed to set solver", status, 0);
  78. }
  79. s->function = NULL;
  80. return s;
  81. }
  82. int
  83. gsl_multiroot_fsolver_set (gsl_multiroot_fsolver * s,
  84. gsl_multiroot_function * f,
  85. const gsl_vector * x)
  86. {
  87. if (s->x->size != f->n)
  88. {
  89. GSL_ERROR ("function incompatible with solver size", GSL_EBADLEN);
  90. }
  91. if (x->size != f->n)
  92. {
  93. GSL_ERROR ("vector length not compatible with function", GSL_EBADLEN);
  94. }
  95. s->function = f;
  96. gsl_vector_memcpy(s->x,x);
  97. return (s->type->set) (s->state, s->function, s->x, s->f, s->dx);
  98. }
  99. int
  100. gsl_multiroot_fsolver_iterate (gsl_multiroot_fsolver * s)
  101. {
  102. return (s->type->iterate) (s->state, s->function, s->x, s->f, s->dx);
  103. }
  104. void
  105. gsl_multiroot_fsolver_free (gsl_multiroot_fsolver * s)
  106. {
  107. (s->type->free) (s->state);
  108. free (s->state);
  109. gsl_vector_free (s->dx);
  110. gsl_vector_free (s->x);
  111. gsl_vector_free (s->f);
  112. free (s);
  113. }
  114. const char *
  115. gsl_multiroot_fsolver_name (const gsl_multiroot_fsolver * s)
  116. {
  117. return s->type->name;
  118. }
  119. gsl_vector *
  120. gsl_multiroot_fsolver_root (const gsl_multiroot_fsolver * s)
  121. {
  122. return s->x;
  123. }
  124. gsl_vector *
  125. gsl_multiroot_fsolver_dx (const gsl_multiroot_fsolver * s)
  126. {
  127. return s->dx;
  128. }
  129. gsl_vector *
  130. gsl_multiroot_fsolver_f (const gsl_multiroot_fsolver * s)
  131. {
  132. return s->f;
  133. }