gsl_min__fsolver.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /* min/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_min.h"
  24. #include "gsl_min__min.h"
  25. static int
  26. compute_f_values (gsl_function * f, double x_minimum, double * f_minimum,
  27. double x_lower, double * f_lower,
  28. double x_upper, double * f_upper);
  29. static int
  30. compute_f_values (gsl_function * f, double x_minimum, double * f_minimum,
  31. double x_lower, double * f_lower,
  32. double x_upper, double * f_upper)
  33. {
  34. SAFE_FUNC_CALL(f, x_lower, f_lower);
  35. SAFE_FUNC_CALL(f, x_upper, f_upper);
  36. SAFE_FUNC_CALL(f, x_minimum, f_minimum);
  37. return GSL_SUCCESS;
  38. }
  39. int
  40. gsl_min_fminimizer_set (gsl_min_fminimizer * s,
  41. gsl_function * f,
  42. double x_minimum, double x_lower, double x_upper)
  43. {
  44. int status ;
  45. double f_minimum, f_lower, f_upper;
  46. status = compute_f_values (f, x_minimum, &f_minimum,
  47. x_lower, &f_lower,
  48. x_upper, &f_upper);
  49. if (status != GSL_SUCCESS)
  50. {
  51. return status ;
  52. }
  53. status = gsl_min_fminimizer_set_with_values (s, f, x_minimum, f_minimum,
  54. x_lower, f_lower,
  55. x_upper, f_upper);
  56. return status;
  57. }
  58. gsl_min_fminimizer *
  59. gsl_min_fminimizer_alloc (const gsl_min_fminimizer_type * T)
  60. {
  61. gsl_min_fminimizer * s =
  62. (gsl_min_fminimizer *) malloc (sizeof (gsl_min_fminimizer));
  63. if (s == 0)
  64. {
  65. GSL_ERROR_VAL ("failed to allocate space for minimizer struct",
  66. GSL_ENOMEM, 0);
  67. };
  68. s->state = malloc (T->size);
  69. if (s->state == 0)
  70. {
  71. free (s); /* exception in constructor, avoid memory leak */
  72. GSL_ERROR_VAL ("failed to allocate space for minimizer state",
  73. GSL_ENOMEM, 0);
  74. };
  75. s->type = T ;
  76. s->function = NULL;
  77. return s;
  78. }
  79. int
  80. gsl_min_fminimizer_set_with_values (gsl_min_fminimizer * s, gsl_function * f,
  81. double x_minimum, double f_minimum,
  82. double x_lower, double f_lower,
  83. double x_upper, double f_upper)
  84. {
  85. s->function = f;
  86. s->x_minimum = x_minimum;
  87. s->x_lower = x_lower;
  88. s->x_upper = x_upper;
  89. if (x_lower > x_upper)
  90. {
  91. GSL_ERROR ("invalid interval (lower > upper)", GSL_EINVAL);
  92. }
  93. if (x_minimum >= x_upper || x_minimum <= x_lower)
  94. {
  95. GSL_ERROR ("x_minimum must lie inside interval (lower < x < upper)",
  96. GSL_EINVAL);
  97. }
  98. s->f_lower = f_lower;
  99. s->f_upper = f_upper;
  100. s->f_minimum = f_minimum;
  101. if (f_minimum >= f_lower || f_minimum >= f_upper)
  102. {
  103. GSL_ERROR ("endpoints do not enclose a minimum", GSL_EINVAL);
  104. }
  105. return (s->type->set) (s->state, s->function,
  106. x_minimum, f_minimum,
  107. x_lower, f_lower,
  108. x_upper, f_upper);
  109. }
  110. int
  111. gsl_min_fminimizer_iterate (gsl_min_fminimizer * s)
  112. {
  113. return (s->type->iterate) (s->state, s->function,
  114. &(s->x_minimum), &(s->f_minimum),
  115. &(s->x_lower), &(s->f_lower),
  116. &(s->x_upper), &(s->f_upper));
  117. }
  118. void
  119. gsl_min_fminimizer_free (gsl_min_fminimizer * s)
  120. {
  121. free (s->state);
  122. free (s);
  123. }
  124. const char *
  125. gsl_min_fminimizer_name (const gsl_min_fminimizer * s)
  126. {
  127. return s->type->name;
  128. }
  129. /* Deprecated, use x_minimum instead */
  130. double
  131. gsl_min_fminimizer_minimum (const gsl_min_fminimizer * s)
  132. {
  133. return s->x_minimum;
  134. }
  135. double
  136. gsl_min_fminimizer_x_minimum (const gsl_min_fminimizer * s)
  137. {
  138. return s->x_minimum;
  139. }
  140. double
  141. gsl_min_fminimizer_x_lower (const gsl_min_fminimizer * s)
  142. {
  143. return s->x_lower;
  144. }
  145. double
  146. gsl_min_fminimizer_x_upper (const gsl_min_fminimizer * s)
  147. {
  148. return s->x_upper;
  149. }
  150. double
  151. gsl_min_fminimizer_f_minimum (const gsl_min_fminimizer * s)
  152. {
  153. return s->f_minimum;
  154. }
  155. double
  156. gsl_min_fminimizer_f_lower (const gsl_min_fminimizer * s)
  157. {
  158. return s->f_lower;
  159. }
  160. double
  161. gsl_min_fminimizer_f_upper (const gsl_min_fminimizer * s)
  162. {
  163. return s->f_upper;
  164. }