lognormal_distribution.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. #ifdef _LIBCPP_ABI_OLD_LOGNORMAL_DISTRIBUTION
  22. template<class _RealType = double>
  23. class _LIBCPP_TEMPLATE_VIS lognormal_distribution
  24. {
  25. public:
  26. // types
  27. typedef _RealType result_type;
  28. class _LIBCPP_TEMPLATE_VIS param_type
  29. {
  30. normal_distribution<result_type> __nd_;
  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. : __nd_(__m, __s) {}
  36. _LIBCPP_INLINE_VISIBILITY
  37. result_type m() const {return __nd_.mean();}
  38. _LIBCPP_INLINE_VISIBILITY
  39. result_type s() const {return __nd_.stddev();}
  40. friend _LIBCPP_INLINE_VISIBILITY
  41. bool operator==(const param_type& __x, const param_type& __y)
  42. {return __x.__nd_ == __y.__nd_;}
  43. friend _LIBCPP_INLINE_VISIBILITY
  44. bool operator!=(const param_type& __x, const param_type& __y)
  45. {return !(__x == __y);}
  46. friend class lognormal_distribution;
  47. template <class _CharT, class _Traits, class _RT>
  48. friend
  49. basic_ostream<_CharT, _Traits>&
  50. operator<<(basic_ostream<_CharT, _Traits>& __os,
  51. const lognormal_distribution<_RT>& __x);
  52. template <class _CharT, class _Traits, class _RT>
  53. friend
  54. basic_istream<_CharT, _Traits>&
  55. operator>>(basic_istream<_CharT, _Traits>& __is,
  56. lognormal_distribution<_RT>& __x);
  57. };
  58. private:
  59. param_type __p_;
  60. public:
  61. // constructor and reset functions
  62. #ifndef _LIBCPP_CXX03_LANG
  63. _LIBCPP_INLINE_VISIBILITY
  64. lognormal_distribution() : lognormal_distribution(0) {}
  65. _LIBCPP_INLINE_VISIBILITY
  66. explicit lognormal_distribution(result_type __m, result_type __s = 1)
  67. : __p_(param_type(__m, __s)) {}
  68. #else
  69. _LIBCPP_INLINE_VISIBILITY
  70. explicit lognormal_distribution(result_type __m = 0,
  71. result_type __s = 1)
  72. : __p_(param_type(__m, __s)) {}
  73. #endif
  74. _LIBCPP_INLINE_VISIBILITY
  75. explicit lognormal_distribution(const param_type& __p)
  76. : __p_(__p) {}
  77. _LIBCPP_INLINE_VISIBILITY
  78. void reset() {__p_.__nd_.reset();}
  79. // generating functions
  80. template<class _URNG>
  81. _LIBCPP_INLINE_VISIBILITY
  82. result_type operator()(_URNG& __g)
  83. {return (*this)(__g, __p_);}
  84. template<class _URNG>
  85. _LIBCPP_INLINE_VISIBILITY
  86. result_type operator()(_URNG& __g, const param_type& __p)
  87. {return _VSTD::exp(const_cast<normal_distribution<result_type>&>(__p.__nd_)(__g));}
  88. // property functions
  89. _LIBCPP_INLINE_VISIBILITY
  90. result_type m() const {return __p_.m();}
  91. _LIBCPP_INLINE_VISIBILITY
  92. result_type s() const {return __p_.s();}
  93. _LIBCPP_INLINE_VISIBILITY
  94. param_type param() const {return __p_;}
  95. _LIBCPP_INLINE_VISIBILITY
  96. void param(const param_type& __p) {__p_ = __p;}
  97. _LIBCPP_INLINE_VISIBILITY
  98. result_type min() const {return 0;}
  99. _LIBCPP_INLINE_VISIBILITY
  100. result_type max() const {return numeric_limits<result_type>::infinity();}
  101. friend _LIBCPP_INLINE_VISIBILITY
  102. bool operator==(const lognormal_distribution& __x,
  103. const lognormal_distribution& __y)
  104. {return __x.__p_ == __y.__p_;}
  105. friend _LIBCPP_INLINE_VISIBILITY
  106. bool operator!=(const lognormal_distribution& __x,
  107. const lognormal_distribution& __y)
  108. {return !(__x == __y);}
  109. template <class _CharT, class _Traits, class _RT>
  110. friend
  111. basic_ostream<_CharT, _Traits>&
  112. operator<<(basic_ostream<_CharT, _Traits>& __os,
  113. const lognormal_distribution<_RT>& __x);
  114. template <class _CharT, class _Traits, class _RT>
  115. friend
  116. basic_istream<_CharT, _Traits>&
  117. operator>>(basic_istream<_CharT, _Traits>& __is,
  118. lognormal_distribution<_RT>& __x);
  119. };
  120. template <class _CharT, class _Traits, class _RT>
  121. inline _LIBCPP_INLINE_VISIBILITY
  122. basic_ostream<_CharT, _Traits>&
  123. operator<<(basic_ostream<_CharT, _Traits>& __os,
  124. const lognormal_distribution<_RT>& __x)
  125. {
  126. return __os << __x.__p_.__nd_;
  127. }
  128. template <class _CharT, class _Traits, class _RT>
  129. inline _LIBCPP_INLINE_VISIBILITY
  130. basic_istream<_CharT, _Traits>&
  131. operator>>(basic_istream<_CharT, _Traits>& __is,
  132. lognormal_distribution<_RT>& __x)
  133. {
  134. return __is >> __x.__p_.__nd_;
  135. }
  136. #else // _LIBCPP_ABI_OLD_LOGNORMAL_DISTRIBUTION
  137. template<class _RealType = double>
  138. class _LIBCPP_TEMPLATE_VIS lognormal_distribution
  139. {
  140. public:
  141. // types
  142. typedef _RealType result_type;
  143. class _LIBCPP_TEMPLATE_VIS param_type
  144. {
  145. result_type __m_;
  146. result_type __s_;
  147. public:
  148. typedef lognormal_distribution distribution_type;
  149. _LIBCPP_INLINE_VISIBILITY
  150. explicit param_type(result_type __m = 0, result_type __s = 1)
  151. : __m_(__m), __s_(__s) {}
  152. _LIBCPP_INLINE_VISIBILITY
  153. result_type m() const {return __m_;}
  154. _LIBCPP_INLINE_VISIBILITY
  155. result_type s() const {return __s_;}
  156. friend _LIBCPP_INLINE_VISIBILITY
  157. bool operator==(const param_type& __x, const param_type& __y)
  158. {return __x.__m_ == __y.__m_ && __x.__s_ == __y.__s_;}
  159. friend _LIBCPP_INLINE_VISIBILITY
  160. bool operator!=(const param_type& __x, const param_type& __y)
  161. {return !(__x == __y);}
  162. };
  163. private:
  164. normal_distribution<result_type> __nd_;
  165. public:
  166. // constructor and reset functions
  167. #ifndef _LIBCPP_CXX03_LANG
  168. _LIBCPP_INLINE_VISIBILITY
  169. lognormal_distribution() : lognormal_distribution(0) {}
  170. _LIBCPP_INLINE_VISIBILITY
  171. explicit lognormal_distribution(result_type __m, result_type __s = 1)
  172. : __nd_(__m, __s) {}
  173. #else
  174. _LIBCPP_INLINE_VISIBILITY
  175. explicit lognormal_distribution(result_type __m = 0,
  176. result_type __s = 1)
  177. : __nd_(__m, __s) {}
  178. #endif
  179. _LIBCPP_INLINE_VISIBILITY
  180. explicit lognormal_distribution(const param_type& __p)
  181. : __nd_(__p.m(), __p.s()) {}
  182. _LIBCPP_INLINE_VISIBILITY
  183. void reset() {__nd_.reset();}
  184. // generating functions
  185. template<class _URNG>
  186. _LIBCPP_INLINE_VISIBILITY
  187. result_type operator()(_URNG& __g)
  188. {
  189. return _VSTD::exp(__nd_(__g));
  190. }
  191. template<class _URNG>
  192. _LIBCPP_INLINE_VISIBILITY
  193. result_type operator()(_URNG& __g, const param_type& __p)
  194. {
  195. typename normal_distribution<result_type>::param_type __pn(__p.m(), __p.s());
  196. return _VSTD::exp(__nd_(__g, __pn));
  197. }
  198. // property functions
  199. _LIBCPP_INLINE_VISIBILITY
  200. result_type m() const {return __nd_.mean();}
  201. _LIBCPP_INLINE_VISIBILITY
  202. result_type s() const {return __nd_.stddev();}
  203. _LIBCPP_INLINE_VISIBILITY
  204. param_type param() const {return param_type(__nd_.mean(), __nd_.stddev());}
  205. _LIBCPP_INLINE_VISIBILITY
  206. void param(const param_type& __p)
  207. {
  208. typename normal_distribution<result_type>::param_type __pn(__p.m(), __p.s());
  209. __nd_.param(__pn);
  210. }
  211. _LIBCPP_INLINE_VISIBILITY
  212. result_type min() const {return 0;}
  213. _LIBCPP_INLINE_VISIBILITY
  214. result_type max() const {return numeric_limits<result_type>::infinity();}
  215. friend _LIBCPP_INLINE_VISIBILITY
  216. bool operator==(const lognormal_distribution& __x,
  217. const lognormal_distribution& __y)
  218. {return __x.__nd_ == __y.__nd_;}
  219. friend _LIBCPP_INLINE_VISIBILITY
  220. bool operator!=(const lognormal_distribution& __x,
  221. const lognormal_distribution& __y)
  222. {return !(__x == __y);}
  223. template <class _CharT, class _Traits, class _RT>
  224. friend
  225. basic_ostream<_CharT, _Traits>&
  226. operator<<(basic_ostream<_CharT, _Traits>& __os,
  227. const lognormal_distribution<_RT>& __x);
  228. template <class _CharT, class _Traits, class _RT>
  229. friend
  230. basic_istream<_CharT, _Traits>&
  231. operator>>(basic_istream<_CharT, _Traits>& __is,
  232. lognormal_distribution<_RT>& __x);
  233. };
  234. template <class _CharT, class _Traits, class _RT>
  235. inline _LIBCPP_INLINE_VISIBILITY
  236. basic_ostream<_CharT, _Traits>&
  237. operator<<(basic_ostream<_CharT, _Traits>& __os,
  238. const lognormal_distribution<_RT>& __x)
  239. {
  240. return __os << __x.__nd_;
  241. }
  242. template <class _CharT, class _Traits, class _RT>
  243. inline _LIBCPP_INLINE_VISIBILITY
  244. basic_istream<_CharT, _Traits>&
  245. operator>>(basic_istream<_CharT, _Traits>& __is,
  246. lognormal_distribution<_RT>& __x)
  247. {
  248. return __is >> __x.__nd_;
  249. }
  250. #endif // _LIBCPP_ABI_OLD_LOGNORMAL_DISTRIBUTION
  251. _LIBCPP_END_NAMESPACE_STD
  252. _LIBCPP_POP_MACROS
  253. #endif // _LIBCPP___RANDOM_LOGNORMAL_DISTRIBUTION_H