gsl_roots__fdfsolver.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /* roots/fdfsolver.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_fdfsolver *
  25. gsl_root_fdfsolver_alloc (const gsl_root_fdfsolver_type * T)
  26. {
  27. gsl_root_fdfsolver * s = (gsl_root_fdfsolver *) malloc (sizeof (gsl_root_fdfsolver));
  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->fdf = NULL;
  42. return s;
  43. }
  44. int
  45. gsl_root_fdfsolver_set (gsl_root_fdfsolver * s, gsl_function_fdf * f, double root)
  46. {
  47. s->fdf = f;
  48. s->root = root;
  49. return (s->type->set) (s->state, s->fdf, &(s->root));
  50. }
  51. int
  52. gsl_root_fdfsolver_iterate (gsl_root_fdfsolver * s)
  53. {
  54. return (s->type->iterate) (s->state, s->fdf, &(s->root));
  55. }
  56. void
  57. gsl_root_fdfsolver_free (gsl_root_fdfsolver * s)
  58. {
  59. free (s->state);
  60. free (s);
  61. }
  62. const char *
  63. gsl_root_fdfsolver_name (const gsl_root_fdfsolver * s)
  64. {
  65. return s->type->name;
  66. }
  67. double
  68. gsl_root_fdfsolver_root (const gsl_root_fdfsolver * s)
  69. {
  70. return s->root;
  71. }