lognormal_distribution.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #ifndef _LIBCPP___RANDOM_LOGNORMAL_DISTRIBUTION_H
  9. #define _LIBCPP___RANDOM_LOGNORMAL_DISTRIBUTION_H
  10. #include <__config>
  11. #include <__random/normal_distribution.h>
  12. #include <cmath>
  13. #include <iosfwd>
  14. #include <limits>
  15. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  16. # pragma GCC system_header
  17. #endif
  18. _LIBCPP_PUSH_MACROS
  19. #include <__undef_macros>
  20. _LIBCPP_BEGIN_NAMESPACE_STD
  21. template<class _RealType = double>
  22. class _LIBCPP_TEMPLATE_VIS lognormal_distribution
  23. {
  24. public:
  25. // types
  26. typedef _RealType result_type;
  27. class _LIBCPP_TEMPLATE_VIS param_type
  28. {
  29. result_type __m_;
  30. result_type __s_;
  31. public:
  32. typedef lognormal_distribution distribution_type;
  33. _LIBCPP_INLINE_VISIBILITY
  34. explicit param_type(result_type __m = 0, result_type __s = 1)
  35. : __m_(__m), __s_(__s) {}
  36. _LIBCPP_INLINE_VISIBILITY
  37. result_type m() const {return __m_;}
  38. _LIBCPP_INLINE_VISIBILITY
  39. result_type s() const {return __s_;}
  40. friend _LIBCPP_INLINE_VISIBILITY
  41. bool operator==(const param_type& __x, const param_type& __y)
  42. {return __x.__m_ == __y.__m_ && __x.__s_ == __y.__s_;}
  43. friend _LIBCPP_INLINE_VISIBILITY
  44. bool operator!=(const param_type& __x, const param_type& __y)
  45. {return !(__x == __y);}
  46. };
  47. private:
  48. normal_distribution<result_type> __nd_;
  49. public:
  50. // constructor and reset functions
  51. #ifndef _LIBCPP_CXX03_LANG
  52. _LIBCPP_INLINE_VISIBILITY
  53. lognormal_distribution() : lognormal_distribution(0) {}
  54. _LIBCPP_INLINE_VISIBILITY
  55. explicit lognormal_distribution(result_type __m, result_type __s = 1)
  56. : __nd_(__m, __s) {}
  57. #else
  58. _LIBCPP_INLINE_VISIBILITY
  59. explicit lognormal_distribution(result_type __m = 0,
  60. result_type __s = 1)
  61. : __nd_(__m, __s) {}
  62. #endif
  63. _LIBCPP_INLINE_VISIBILITY
  64. explicit lognormal_distribution(const param_type& __p)
  65. : __nd_(__p.m(), __p.s()) {}
  66. _LIBCPP_INLINE_VISIBILITY
  67. void reset() {__nd_.reset();}
  68. // generating functions
  69. template<class _URNG>
  70. _LIBCPP_INLINE_VISIBILITY
  71. result_type operator()(_URNG& __g)
  72. {
  73. return _VSTD::exp(__nd_(__g));
  74. }
  75. template<class _URNG>
  76. _LIBCPP_INLINE_VISIBILITY
  77. result_type operator()(_URNG& __g, const param_type& __p)
  78. {
  79. typename normal_distribution<result_type>::param_type __pn(__p.m(), __p.s());
  80. return _VSTD::exp(__nd_(__g, __pn));
  81. }
  82. // property functions
  83. _LIBCPP_INLINE_VISIBILITY
  84. result_type m() const {return __nd_.mean();}
  85. _LIBCPP_INLINE_VISIBILITY
  86. result_type s() const {return __nd_.stddev();}
  87. _LIBCPP_INLINE_VISIBILITY
  88. param_type param() const {return param_type(__nd_.mean(), __nd_.stddev());}
  89. _LIBCPP_INLINE_VISIBILITY
  90. void param(const param_type& __p)
  91. {
  92. typename normal_distribution<result_type>::param_type __pn(__p.m(), __p.s());
  93. __nd_.param(__pn);
  94. }
  95. _LIBCPP_INLINE_VISIBILITY
  96. result_type min() const {return 0;}
  97. _LIBCPP_INLINE_VISIBILITY
  98. result_type max() const {return numeric_limits<result_type>::infinity();}
  99. friend _LIBCPP_INLINE_VISIBILITY
  100. bool operator==(const lognormal_distribution& __x,
  101. const lognormal_distribution& __y)
  102. {return __x.__nd_ == __y.__nd_;}
  103. friend _LIBCPP_INLINE_VISIBILITY
  104. bool operator!=(const lognormal_distribution& __x,
  105. const lognormal_distribution& __y)
  106. {return !(__x == __y);}
  107. template <class _CharT, class _Traits, class _RT>
  108. friend
  109. basic_ostream<_CharT, _Traits>&
  110. operator<<(basic_ostream<_CharT, _Traits>& __os,
  111. const lognormal_distribution<_RT>& __x);
  112. template <class _CharT, class _Traits, class _RT>
  113. friend
  114. basic_istream<_CharT, _Traits>&
  115. operator>>(basic_istream<_CharT, _Traits>& __is,
  116. lognormal_distribution<_RT>& __x);
  117. };
  118. template <class _CharT, class _Traits, class _RT>
  119. inline _LIBCPP_INLINE_VISIBILITY
  120. basic_ostream<_CharT, _Traits>&
  121. operator<<(basic_ostream<_CharT, _Traits>& __os,
  122. const lognormal_distribution<_RT>& __x)
  123. {
  124. return __os << __x.__nd_;
  125. }
  126. template <class _CharT, class _Traits, class _RT>
  127. inline _LIBCPP_INLINE_VISIBILITY
  128. basic_istream<_CharT, _Traits>&
  129. operator>>(basic_istream<_CharT, _Traits>& __is,
  130. lognormal_distribution<_RT>& __x)
  131. {
  132. return __is >> __x.__nd_;
  133. }
  134. _LIBCPP_END_NAMESPACE_STD
  135. _LIBCPP_POP_MACROS
  136. #endif // _LIBCPP___RANDOM_LOGNORMAL_DISTRIBUTION_H