xref: /freebsd/contrib/llvm-project/libcxx/include/__random/lognormal_distribution.h (revision 0eae32dcef82f6f06de6419a0d623d7def0cc8f6)
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/normal_distribution.h>
14 #include <cmath>
15 #include <iosfwd>
16 #include <limits>
17 
18 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
19 #pragma GCC system_header
20 #endif
21 
22 _LIBCPP_PUSH_MACROS
23 #include <__undef_macros>
24 
25 _LIBCPP_BEGIN_NAMESPACE_STD
26 
27 template<class _RealType = double>
28 class _LIBCPP_TEMPLATE_VIS lognormal_distribution
29 {
30 public:
31     // types
32     typedef _RealType result_type;
33 
34     class _LIBCPP_TEMPLATE_VIS param_type
35     {
36         normal_distribution<result_type> __nd_;
37     public:
38         typedef lognormal_distribution distribution_type;
39 
40         _LIBCPP_INLINE_VISIBILITY
41         explicit param_type(result_type __m = 0, result_type __s = 1)
42             : __nd_(__m, __s) {}
43 
44         _LIBCPP_INLINE_VISIBILITY
45         result_type m() const {return __nd_.mean();}
46         _LIBCPP_INLINE_VISIBILITY
47         result_type s() const {return __nd_.stddev();}
48 
49         friend _LIBCPP_INLINE_VISIBILITY
50             bool operator==(const param_type& __x, const param_type& __y)
51             {return __x.__nd_ == __y.__nd_;}
52         friend _LIBCPP_INLINE_VISIBILITY
53             bool operator!=(const param_type& __x, const param_type& __y)
54             {return !(__x == __y);}
55         friend class lognormal_distribution;
56 
57         template <class _CharT, class _Traits, class _RT>
58         friend
59         basic_ostream<_CharT, _Traits>&
60         operator<<(basic_ostream<_CharT, _Traits>& __os,
61                    const lognormal_distribution<_RT>& __x);
62 
63         template <class _CharT, class _Traits, class _RT>
64         friend
65         basic_istream<_CharT, _Traits>&
66         operator>>(basic_istream<_CharT, _Traits>& __is,
67                    lognormal_distribution<_RT>& __x);
68     };
69 
70 private:
71     param_type __p_;
72 
73 public:
74     // constructor and reset functions
75 #ifndef _LIBCPP_CXX03_LANG
76     _LIBCPP_INLINE_VISIBILITY
77     lognormal_distribution() : lognormal_distribution(0) {}
78     _LIBCPP_INLINE_VISIBILITY
79     explicit lognormal_distribution(result_type __m, result_type __s = 1)
80         : __p_(param_type(__m, __s)) {}
81 #else
82     _LIBCPP_INLINE_VISIBILITY
83     explicit lognormal_distribution(result_type __m = 0,
84                                     result_type __s = 1)
85         : __p_(param_type(__m, __s)) {}
86 #endif
87     _LIBCPP_INLINE_VISIBILITY
88     explicit lognormal_distribution(const param_type& __p)
89         : __p_(__p) {}
90     _LIBCPP_INLINE_VISIBILITY
91     void reset() {__p_.__nd_.reset();}
92 
93     // generating functions
94     template<class _URNG>
95         _LIBCPP_INLINE_VISIBILITY
96         result_type operator()(_URNG& __g)
97         {return (*this)(__g, __p_);}
98     template<class _URNG>
99         _LIBCPP_INLINE_VISIBILITY
100         result_type operator()(_URNG& __g, const param_type& __p)
101         {return _VSTD::exp(const_cast<normal_distribution<result_type>&>(__p.__nd_)(__g));}
102 
103     // property functions
104     _LIBCPP_INLINE_VISIBILITY
105     result_type m() const {return __p_.m();}
106     _LIBCPP_INLINE_VISIBILITY
107     result_type s() const {return __p_.s();}
108 
109     _LIBCPP_INLINE_VISIBILITY
110     param_type param() const {return __p_;}
111     _LIBCPP_INLINE_VISIBILITY
112     void param(const param_type& __p) {__p_ = __p;}
113 
114     _LIBCPP_INLINE_VISIBILITY
115     result_type min() const {return 0;}
116     _LIBCPP_INLINE_VISIBILITY
117     result_type max() const {return numeric_limits<result_type>::infinity();}
118 
119     friend _LIBCPP_INLINE_VISIBILITY
120         bool operator==(const lognormal_distribution& __x,
121                         const lognormal_distribution& __y)
122         {return __x.__p_ == __y.__p_;}
123     friend _LIBCPP_INLINE_VISIBILITY
124         bool operator!=(const lognormal_distribution& __x,
125                         const lognormal_distribution& __y)
126         {return !(__x == __y);}
127 
128     template <class _CharT, class _Traits, class _RT>
129     friend
130     basic_ostream<_CharT, _Traits>&
131     operator<<(basic_ostream<_CharT, _Traits>& __os,
132                const lognormal_distribution<_RT>& __x);
133 
134     template <class _CharT, class _Traits, class _RT>
135     friend
136     basic_istream<_CharT, _Traits>&
137     operator>>(basic_istream<_CharT, _Traits>& __is,
138                lognormal_distribution<_RT>& __x);
139 };
140 
141 template <class _CharT, class _Traits, class _RT>
142 inline _LIBCPP_INLINE_VISIBILITY
143 basic_ostream<_CharT, _Traits>&
144 operator<<(basic_ostream<_CharT, _Traits>& __os,
145            const lognormal_distribution<_RT>& __x)
146 {
147     return __os << __x.__p_.__nd_;
148 }
149 
150 template <class _CharT, class _Traits, class _RT>
151 inline _LIBCPP_INLINE_VISIBILITY
152 basic_istream<_CharT, _Traits>&
153 operator>>(basic_istream<_CharT, _Traits>& __is,
154            lognormal_distribution<_RT>& __x)
155 {
156     return __is >> __x.__p_.__nd_;
157 }
158 
159 _LIBCPP_END_NAMESPACE_STD
160 
161 _LIBCPP_POP_MACROS
162 
163 #endif // _LIBCPP___RANDOM_LOGNORMAL_DISTRIBUTION_H
164