gsl_randist__rayleigh.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /* randist/rayleigh.c
  2. *
  3. * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2007 James Theiler, 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 <math.h>
  21. #include "gsl_rng.h"
  22. #include "gsl_randist.h"
  23. /* The Rayleigh distribution has the form
  24. p(x) dx = (x / sigma^2) exp(-x^2/(2 sigma^2)) dx
  25. for x = 0 ... +infty */
  26. double
  27. gsl_ran_rayleigh (const gsl_rng * r, const double sigma)
  28. {
  29. double u = gsl_rng_uniform_pos (r);
  30. return sigma * sqrt(-2.0 * log (u));
  31. }
  32. double
  33. gsl_ran_rayleigh_pdf (const double x, const double sigma)
  34. {
  35. if (x < 0)
  36. {
  37. return 0 ;
  38. }
  39. else
  40. {
  41. double u = x / sigma ;
  42. double p = (u / sigma) * exp(-u * u / 2.0) ;
  43. return p;
  44. }
  45. }
  46. /* The Rayleigh tail distribution has the form
  47. p(x) dx = (x / sigma^2) exp((a^2 - x^2)/(2 sigma^2)) dx
  48. for x = a ... +infty */
  49. double
  50. gsl_ran_rayleigh_tail (const gsl_rng * r, const double a, const double sigma)
  51. {
  52. double u = gsl_rng_uniform_pos (r);
  53. return sqrt(a * a - 2.0 * sigma * sigma * log (u));
  54. }
  55. double
  56. gsl_ran_rayleigh_tail_pdf (const double x, const double a, const double sigma)
  57. {
  58. if (x < a)
  59. {
  60. return 0 ;
  61. }
  62. else
  63. {
  64. double u = x / sigma ;
  65. double v = a / sigma ;
  66. double p = (u / sigma) * exp((v + u) * (v - u) / 2.0) ;
  67. return p;
  68. }
  69. }