| 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 | |
| 9 | #ifndef _LIBCPP___RANDOM_LOGNORMAL_DISTRIBUTION_H |
| 10 | #define _LIBCPP___RANDOM_LOGNORMAL_DISTRIBUTION_H |
| 11 | |
| 12 | #include <__config> |
| 13 | #include <__random/is_valid.h> |
| 14 | #include <__random/normal_distribution.h> |
| 15 | #include <cmath> |
| 16 | #include <iosfwd> |
| 17 | #include <limits> |
| 18 | |
| 19 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
| 20 | # pragma GCC system_header |
| 21 | #endif |
| 22 | |
| 23 | _LIBCPP_PUSH_MACROS |
| 24 | #include <__undef_macros> |
| 25 | |
| 26 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 27 | |
| 28 | template <class _RealType = double> |
| 29 | class lognormal_distribution { |
| 30 | static_assert(__libcpp_random_is_valid_realtype<_RealType>::value, |
| 31 | "RealType must be a supported floating-point type" ); |
| 32 | |
| 33 | public: |
| 34 | // types |
| 35 | typedef _RealType result_type; |
| 36 | |
| 37 | class param_type { |
| 38 | result_type __m_; |
| 39 | result_type __s_; |
| 40 | |
| 41 | public: |
| 42 | typedef lognormal_distribution distribution_type; |
| 43 | |
| 44 | _LIBCPP_HIDE_FROM_ABI explicit param_type(result_type __m = 0, result_type __s = 1) : __m_(__m), __s_(__s) {} |
| 45 | |
| 46 | _LIBCPP_HIDE_FROM_ABI result_type m() const { return __m_; } |
| 47 | _LIBCPP_HIDE_FROM_ABI result_type s() const { return __s_; } |
| 48 | |
| 49 | friend _LIBCPP_HIDE_FROM_ABI bool operator==(const param_type& __x, const param_type& __y) { |
| 50 | return __x.__m_ == __y.__m_ && __x.__s_ == __y.__s_; |
| 51 | } |
| 52 | friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const param_type& __x, const param_type& __y) { return !(__x == __y); } |
| 53 | }; |
| 54 | |
| 55 | private: |
| 56 | normal_distribution<result_type> __nd_; |
| 57 | |
| 58 | public: |
| 59 | // constructor and reset functions |
| 60 | #ifndef _LIBCPP_CXX03_LANG |
| 61 | _LIBCPP_HIDE_FROM_ABI lognormal_distribution() : lognormal_distribution(0) {} |
| 62 | _LIBCPP_HIDE_FROM_ABI explicit lognormal_distribution(result_type __m, result_type __s = 1) : __nd_(__m, __s) {} |
| 63 | #else |
| 64 | _LIBCPP_HIDE_FROM_ABI explicit lognormal_distribution(result_type __m = 0, result_type __s = 1) : __nd_(__m, __s) {} |
| 65 | #endif |
| 66 | _LIBCPP_HIDE_FROM_ABI explicit lognormal_distribution(const param_type& __p) : __nd_(__p.m(), __p.s()) {} |
| 67 | _LIBCPP_HIDE_FROM_ABI void reset() { __nd_.reset(); } |
| 68 | |
| 69 | // generating functions |
| 70 | template <class _URNG> |
| 71 | _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g) { |
| 72 | return std::exp(__nd_(__g)); |
| 73 | } |
| 74 | |
| 75 | template <class _URNG> |
| 76 | _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g, const param_type& __p) { |
| 77 | typename normal_distribution<result_type>::param_type __pn(__p.m(), __p.s()); |
| 78 | return std::exp(__nd_(__g, __pn)); |
| 79 | } |
| 80 | |
| 81 | // property functions |
| 82 | _LIBCPP_HIDE_FROM_ABI result_type m() const { return __nd_.mean(); } |
| 83 | _LIBCPP_HIDE_FROM_ABI result_type s() const { return __nd_.stddev(); } |
| 84 | |
| 85 | _LIBCPP_HIDE_FROM_ABI param_type param() const { return param_type(__nd_.mean(), __nd_.stddev()); } |
| 86 | _LIBCPP_HIDE_FROM_ABI void param(const param_type& __p) { |
| 87 | typename normal_distribution<result_type>::param_type __pn(__p.m(), __p.s()); |
| 88 | __nd_.param(__pn); |
| 89 | } |
| 90 | |
| 91 | _LIBCPP_HIDE_FROM_ABI result_type min() const { return 0; } |
| 92 | _LIBCPP_HIDE_FROM_ABI result_type max() const { return numeric_limits<result_type>::infinity(); } |
| 93 | |
| 94 | friend _LIBCPP_HIDE_FROM_ABI bool operator==(const lognormal_distribution& __x, const lognormal_distribution& __y) { |
| 95 | return __x.__nd_ == __y.__nd_; |
| 96 | } |
| 97 | friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const lognormal_distribution& __x, const lognormal_distribution& __y) { |
| 98 | return !(__x == __y); |
| 99 | } |
| 100 | |
| 101 | template <class _CharT, class _Traits, class _RT> |
| 102 | friend basic_ostream<_CharT, _Traits>& |
| 103 | operator<<(basic_ostream<_CharT, _Traits>& __os, const lognormal_distribution<_RT>& __x); |
| 104 | |
| 105 | template <class _CharT, class _Traits, class _RT> |
| 106 | friend basic_istream<_CharT, _Traits>& |
| 107 | operator>>(basic_istream<_CharT, _Traits>& __is, lognormal_distribution<_RT>& __x); |
| 108 | }; |
| 109 | |
| 110 | template <class _CharT, class _Traits, class _RT> |
| 111 | inline _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>& |
| 112 | operator<<(basic_ostream<_CharT, _Traits>& __os, const lognormal_distribution<_RT>& __x) { |
| 113 | return __os << __x.__nd_; |
| 114 | } |
| 115 | |
| 116 | template <class _CharT, class _Traits, class _RT> |
| 117 | inline _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>& |
| 118 | operator>>(basic_istream<_CharT, _Traits>& __is, lognormal_distribution<_RT>& __x) { |
| 119 | return __is >> __x.__nd_; |
| 120 | } |
| 121 | |
| 122 | _LIBCPP_END_NAMESPACE_STD |
| 123 | |
| 124 | _LIBCPP_POP_MACROS |
| 125 | |
| 126 | #endif // _LIBCPP___RANDOM_LOGNORMAL_DISTRIBUTION_H |
| 127 | |