gsl_roots__fsolver.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /* roots/fsolver.c
  2. *
  3. * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2007 Reid Priedhorsky, 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_roots.h"
  24. gsl_root_fsolver *
  25. gsl_root_fsolver_alloc (const gsl_root_fsolver_type * T)
  26. {
  27. gsl_root_fsolver * s = (gsl_root_fsolver *) malloc (sizeof (gsl_root_fsolver));
  28. if (s == 0)
  29. {
  30. GSL_ERROR_VAL ("failed to allocate space for root solver struct",
  31. GSL_ENOMEM, 0);
  32. };
  33. s->state = malloc (T->size);
  34. if (s->state == 0)
  35. {
  36. free (s); /* exception in constructor, avoid memory leak */
  37. GSL_ERROR_VAL ("failed to allocate space for root solver state",
  38. GSL_ENOMEM, 0);
  39. };
  40. s->type = T ;
  41. s->function = NULL ;
  42. return s;
  43. }
  44. int
  45. gsl_root_fsolver_set (gsl_root_fsolver * s, gsl_function * f, double x_lower, double x_upper)
  46. {
  47. if (x_lower > x_upper)
  48. {
  49. GSL_ERROR ("invalid interval (lower > upper)", GSL_EINVAL);
  50. }
  51. s->function = f;
  52. s->root = 0.5 * (x_lower + x_upper); /* initial estimate */
  53. s->x_lower = x_lower;
  54. s->x_upper = x_upper;
  55. return (s->type->set) (s->state, s->function, &(s->root), x_lower, x_upper);
  56. }
  57. int
  58. gsl_root_fsolver_iterate (gsl_root_fsolver * s)
  59. {
  60. return (s->type->iterate) (s->state,
  61. s->function, &(s->root),
  62. &(s->x_lower), &(s->x_upper));
  63. }
  64. void
  65. gsl_root_fsolver_free (gsl_root_fsolver * s)
  66. {
  67. free (s->state);
  68. free (s);
  69. }
  70. const char *
  71. gsl_root_fsolver_name (const gsl_root_fsolver * s)
  72. {
  73. return s->type->name;
  74. }
  75. double
  76. gsl_root_fsolver_root (const gsl_root_fsolver * s)
  77. {
  78. return s->root;
  79. }
  80. double
  81. gsl_root_fsolver_x_lower (const gsl_root_fsolver * s)
  82. {
  83. return s->x_lower;
  84. }
  85. double
  86. gsl_root_fsolver_x_upper (const gsl_root_fsolver * s)
  87. {
  88. return s->x_upper;
  89. }