gsl_monte__plain.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /* monte/plain.c
  2. *
  3. * Copyright (C) 1996, 1997, 1998, 1999, 2000 Michael Booth
  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. /* Plain Monte-Carlo. */
  20. /* Author: MJB */
  21. #include "gsl__config.h"
  22. #include <math.h>
  23. #include "gsl_math.h"
  24. #include "gsl_rng.h"
  25. #include "gsl_monte_plain.h"
  26. int
  27. gsl_monte_plain_integrate (const gsl_monte_function * f,
  28. const double xl[], const double xu[],
  29. const size_t dim,
  30. const size_t calls,
  31. gsl_rng * r,
  32. gsl_monte_plain_state * state,
  33. double *result, double *abserr)
  34. {
  35. double vol, m = 0, q = 0;
  36. double *x = state->x;
  37. size_t n, i;
  38. if (dim != state->dim)
  39. {
  40. GSL_ERROR ("number of dimensions must match allocated size", GSL_EINVAL);
  41. }
  42. for (i = 0; i < dim; i++)
  43. {
  44. if (xu[i] <= xl[i])
  45. {
  46. GSL_ERROR ("xu must be greater than xl", GSL_EINVAL);
  47. }
  48. if (xu[i] - xl[i] > GSL_DBL_MAX)
  49. {
  50. GSL_ERROR ("Range of integration is too large, please rescale",
  51. GSL_EINVAL);
  52. }
  53. }
  54. /* Compute the volume of the region */
  55. vol = 1;
  56. for (i = 0; i < dim; i++)
  57. {
  58. vol *= xu[i] - xl[i];
  59. }
  60. for (n = 0; n < calls; n++)
  61. {
  62. /* Choose a random point in the integration region */
  63. for (i = 0; i < dim; i++)
  64. {
  65. x[i] = xl[i] + gsl_rng_uniform_pos (r) * (xu[i] - xl[i]);
  66. }
  67. {
  68. double fval = GSL_MONTE_FN_EVAL (f, x);
  69. /* recurrence for mean and variance */
  70. double d = fval - m;
  71. m += d / (n + 1.0);
  72. q += d * d * (n / (n + 1.0));
  73. }
  74. }
  75. *result = vol * m;
  76. if (calls < 2)
  77. {
  78. *abserr = GSL_POSINF;
  79. }
  80. else
  81. {
  82. *abserr = vol * sqrt (q / (calls * (calls - 1.0)));
  83. }
  84. return GSL_SUCCESS;
  85. }
  86. gsl_monte_plain_state *
  87. gsl_monte_plain_alloc (size_t dim)
  88. {
  89. gsl_monte_plain_state *s =
  90. (gsl_monte_plain_state *) malloc (sizeof (gsl_monte_plain_state));
  91. if (s == 0)
  92. {
  93. GSL_ERROR_VAL ("failed to allocate space for state struct",
  94. GSL_ENOMEM, 0);
  95. }
  96. s->x = (double *) malloc (dim * sizeof (double));
  97. if (s->x == 0)
  98. {
  99. free (s);
  100. GSL_ERROR_VAL ("failed to allocate space for working vector",
  101. GSL_ENOMEM, 0);
  102. }
  103. s->dim = dim;
  104. return s;
  105. }
  106. /* Set some default values and whatever */
  107. int
  108. gsl_monte_plain_init (gsl_monte_plain_state * s)
  109. {
  110. size_t i;
  111. for (i = 0; i < s->dim; i++)
  112. {
  113. s->x[i] = 0.0;
  114. }
  115. return GSL_SUCCESS;
  116. }
  117. void
  118. gsl_monte_plain_free (gsl_monte_plain_state * s)
  119. {
  120. free (s->x);
  121. free (s);
  122. }