gsl_multimin__fminimizer.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /* multimin/fminimizer.c
  2. *
  3. * Copyright (C) 2002 Tuomo Keskitalo, Ivo Alxneit
  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 "gsl_errno.h"
  21. #include "gsl_multimin.h"
  22. gsl_multimin_fminimizer *
  23. gsl_multimin_fminimizer_alloc (const gsl_multimin_fminimizer_type * T,
  24. size_t n)
  25. {
  26. int status;
  27. gsl_multimin_fminimizer *s =
  28. (gsl_multimin_fminimizer *) malloc (sizeof (gsl_multimin_fminimizer));
  29. if (s == 0)
  30. {
  31. GSL_ERROR_VAL ("failed to allocate space for minimizer struct",
  32. GSL_ENOMEM, 0);
  33. }
  34. s->type = T;
  35. s->x = gsl_vector_calloc (n);
  36. if (s->x == 0)
  37. {
  38. free (s);
  39. GSL_ERROR_VAL ("failed to allocate space for x", GSL_ENOMEM, 0);
  40. }
  41. s->state = malloc (T->size);
  42. if (s->state == 0)
  43. {
  44. gsl_vector_free (s->x);
  45. free (s);
  46. GSL_ERROR_VAL ("failed to allocate space for minimizer state",
  47. GSL_ENOMEM, 0);
  48. }
  49. status = (T->alloc) (s->state, n);
  50. if (status != GSL_SUCCESS)
  51. {
  52. free (s->state);
  53. gsl_vector_free (s->x);
  54. free (s);
  55. GSL_ERROR_VAL ("failed to initialize minimizer state", GSL_ENOMEM, 0);
  56. }
  57. return s;
  58. }
  59. int
  60. gsl_multimin_fminimizer_set (gsl_multimin_fminimizer * s,
  61. gsl_multimin_function * f,
  62. const gsl_vector * x,
  63. const gsl_vector * step_size)
  64. {
  65. if (s->x->size != f->n)
  66. {
  67. GSL_ERROR ("function incompatible with solver size", GSL_EBADLEN);
  68. }
  69. if (x->size != f->n || step_size->size != f->n)
  70. {
  71. GSL_ERROR ("vector length not compatible with function", GSL_EBADLEN);
  72. }
  73. s->f = f;
  74. gsl_vector_memcpy (s->x,x);
  75. return (s->type->set) (s->state, s->f, s->x, &(s->size), step_size);
  76. }
  77. void
  78. gsl_multimin_fminimizer_free (gsl_multimin_fminimizer * s)
  79. {
  80. (s->type->free) (s->state);
  81. free (s->state);
  82. gsl_vector_free (s->x);
  83. free (s);
  84. }
  85. int
  86. gsl_multimin_fminimizer_iterate (gsl_multimin_fminimizer * s)
  87. {
  88. return (s->type->iterate) (s->state, s->f, s->x, &(s->size), &(s->fval));
  89. }
  90. const char *
  91. gsl_multimin_fminimizer_name (const gsl_multimin_fminimizer * s)
  92. {
  93. return s->type->name;
  94. }
  95. gsl_vector *
  96. gsl_multimin_fminimizer_x (const gsl_multimin_fminimizer * s)
  97. {
  98. return s->x;
  99. }
  100. double
  101. gsl_multimin_fminimizer_minimum (const gsl_multimin_fminimizer * s)
  102. {
  103. return s->fval;
  104. }
  105. double
  106. gsl_multimin_fminimizer_size (const gsl_multimin_fminimizer * s)
  107. {
  108. return s->size;
  109. }