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