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