xref: /llvm-project/libcxx/include/__random/lognormal_distribution.h (revision 9783f28cbb155e4a8d49c12e1c60ce14dcfaf0c7)
1344cef66SArthur O'Dwyer //===----------------------------------------------------------------------===//
2344cef66SArthur O'Dwyer //
3344cef66SArthur O'Dwyer // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4344cef66SArthur O'Dwyer // See https://llvm.org/LICENSE.txt for license information.
5344cef66SArthur O'Dwyer // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6344cef66SArthur O'Dwyer //
7344cef66SArthur O'Dwyer //===----------------------------------------------------------------------===//
8344cef66SArthur O'Dwyer 
9344cef66SArthur O'Dwyer #ifndef _LIBCPP___RANDOM_LOGNORMAL_DISTRIBUTION_H
10344cef66SArthur O'Dwyer #define _LIBCPP___RANDOM_LOGNORMAL_DISTRIBUTION_H
11344cef66SArthur O'Dwyer 
12344cef66SArthur O'Dwyer #include <__config>
13727fef79SNhat Nguyen #include <__random/is_valid.h>
14344cef66SArthur O'Dwyer #include <__random/normal_distribution.h>
15344cef66SArthur O'Dwyer #include <cmath>
16344cef66SArthur O'Dwyer #include <iosfwd>
17344cef66SArthur O'Dwyer #include <limits>
18344cef66SArthur O'Dwyer 
19344cef66SArthur O'Dwyer #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
20344cef66SArthur O'Dwyer #  pragma GCC system_header
21344cef66SArthur O'Dwyer #endif
22344cef66SArthur O'Dwyer 
23344cef66SArthur O'Dwyer _LIBCPP_PUSH_MACROS
24344cef66SArthur O'Dwyer #include <__undef_macros>
25344cef66SArthur O'Dwyer 
26344cef66SArthur O'Dwyer _LIBCPP_BEGIN_NAMESPACE_STD
27344cef66SArthur O'Dwyer 
280359b85cSArthur O'Dwyer template <class _RealType = double>
29*9783f28cSLouis Dionne class _LIBCPP_TEMPLATE_VIS lognormal_distribution {
30727fef79SNhat Nguyen   static_assert(__libcpp_random_is_valid_realtype<_RealType>::value,
31727fef79SNhat Nguyen                 "RealType must be a supported floating-point type");
32727fef79SNhat Nguyen 
330359b85cSArthur O'Dwyer public:
340359b85cSArthur O'Dwyer   // types
350359b85cSArthur O'Dwyer   typedef _RealType result_type;
360359b85cSArthur O'Dwyer 
37*9783f28cSLouis Dionne   class _LIBCPP_TEMPLATE_VIS param_type {
380359b85cSArthur O'Dwyer     result_type __m_;
390359b85cSArthur O'Dwyer     result_type __s_;
40*9783f28cSLouis Dionne 
410359b85cSArthur O'Dwyer   public:
420359b85cSArthur O'Dwyer     typedef lognormal_distribution distribution_type;
430359b85cSArthur O'Dwyer 
__m_(__m)44*9783f28cSLouis Dionne     _LIBCPP_HIDE_FROM_ABI explicit param_type(result_type __m = 0, result_type __s = 1) : __m_(__m), __s_(__s) {}
450359b85cSArthur O'Dwyer 
m()46*9783f28cSLouis Dionne     _LIBCPP_HIDE_FROM_ABI result_type m() const { return __m_; }
s()47*9783f28cSLouis Dionne     _LIBCPP_HIDE_FROM_ABI result_type s() const { return __s_; }
480359b85cSArthur O'Dwyer 
49*9783f28cSLouis Dionne     friend _LIBCPP_HIDE_FROM_ABI bool operator==(const param_type& __x, const param_type& __y) {
50*9783f28cSLouis Dionne       return __x.__m_ == __y.__m_ && __x.__s_ == __y.__s_;
51*9783f28cSLouis Dionne     }
52*9783f28cSLouis Dionne     friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const param_type& __x, const param_type& __y) { return !(__x == __y); }
530359b85cSArthur O'Dwyer   };
540359b85cSArthur O'Dwyer 
550359b85cSArthur O'Dwyer private:
560359b85cSArthur O'Dwyer   normal_distribution<result_type> __nd_;
570359b85cSArthur O'Dwyer 
580359b85cSArthur O'Dwyer public:
590359b85cSArthur O'Dwyer   // constructor and reset functions
600359b85cSArthur O'Dwyer #ifndef _LIBCPP_CXX03_LANG
lognormal_distribution()61*9783f28cSLouis Dionne   _LIBCPP_HIDE_FROM_ABI lognormal_distribution() : lognormal_distribution(0) {}
__nd_(__m,__s)62*9783f28cSLouis Dionne   _LIBCPP_HIDE_FROM_ABI explicit lognormal_distribution(result_type __m, result_type __s = 1) : __nd_(__m, __s) {}
630359b85cSArthur O'Dwyer #else
64*9783f28cSLouis Dionne   _LIBCPP_HIDE_FROM_ABI explicit lognormal_distribution(result_type __m = 0, result_type __s = 1) : __nd_(__m, __s) {}
650359b85cSArthur O'Dwyer #endif
lognormal_distribution(const param_type & __p)66*9783f28cSLouis Dionne   _LIBCPP_HIDE_FROM_ABI explicit lognormal_distribution(const param_type& __p) : __nd_(__p.m(), __p.s()) {}
reset()67*9783f28cSLouis Dionne   _LIBCPP_HIDE_FROM_ABI void reset() { __nd_.reset(); }
680359b85cSArthur O'Dwyer 
690359b85cSArthur O'Dwyer   // generating functions
700359b85cSArthur O'Dwyer   template <class _URNG>
operator()71*9783f28cSLouis Dionne   _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g) {
7277a00c0dSLouis Dionne     return std::exp(__nd_(__g));
730359b85cSArthur O'Dwyer   }
740359b85cSArthur O'Dwyer 
750359b85cSArthur O'Dwyer   template <class _URNG>
operator()76*9783f28cSLouis Dionne   _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g, const param_type& __p) {
770359b85cSArthur O'Dwyer     typename normal_distribution<result_type>::param_type __pn(__p.m(), __p.s());
7877a00c0dSLouis Dionne     return std::exp(__nd_(__g, __pn));
790359b85cSArthur O'Dwyer   }
800359b85cSArthur O'Dwyer 
810359b85cSArthur O'Dwyer   // property functions
m()82*9783f28cSLouis Dionne   _LIBCPP_HIDE_FROM_ABI result_type m() const { return __nd_.mean(); }
s()83*9783f28cSLouis Dionne   _LIBCPP_HIDE_FROM_ABI result_type s() const { return __nd_.stddev(); }
840359b85cSArthur O'Dwyer 
param()85*9783f28cSLouis Dionne   _LIBCPP_HIDE_FROM_ABI param_type param() const { return param_type(__nd_.mean(), __nd_.stddev()); }
param(const param_type & __p)86*9783f28cSLouis Dionne   _LIBCPP_HIDE_FROM_ABI void param(const param_type& __p) {
870359b85cSArthur O'Dwyer     typename normal_distribution<result_type>::param_type __pn(__p.m(), __p.s());
880359b85cSArthur O'Dwyer     __nd_.param(__pn);
890359b85cSArthur O'Dwyer   }
900359b85cSArthur O'Dwyer 
min()91*9783f28cSLouis Dionne   _LIBCPP_HIDE_FROM_ABI result_type min() const { return 0; }
max()92*9783f28cSLouis Dionne   _LIBCPP_HIDE_FROM_ABI result_type max() const { return numeric_limits<result_type>::infinity(); }
930359b85cSArthur O'Dwyer 
94*9783f28cSLouis Dionne   friend _LIBCPP_HIDE_FROM_ABI bool operator==(const lognormal_distribution& __x, const lognormal_distribution& __y) {
95*9783f28cSLouis Dionne     return __x.__nd_ == __y.__nd_;
96*9783f28cSLouis Dionne   }
97*9783f28cSLouis Dionne   friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const lognormal_distribution& __x, const lognormal_distribution& __y) {
98*9783f28cSLouis Dionne     return !(__x == __y);
99*9783f28cSLouis Dionne   }
1000359b85cSArthur O'Dwyer 
1010359b85cSArthur O'Dwyer   template <class _CharT, class _Traits, class _RT>
102*9783f28cSLouis Dionne   friend basic_ostream<_CharT, _Traits>&
103*9783f28cSLouis Dionne   operator<<(basic_ostream<_CharT, _Traits>& __os, const lognormal_distribution<_RT>& __x);
1040359b85cSArthur O'Dwyer 
1050359b85cSArthur O'Dwyer   template <class _CharT, class _Traits, class _RT>
106*9783f28cSLouis Dionne   friend basic_istream<_CharT, _Traits>&
107*9783f28cSLouis Dionne   operator>>(basic_istream<_CharT, _Traits>& __is, lognormal_distribution<_RT>& __x);
1080359b85cSArthur O'Dwyer };
1090359b85cSArthur O'Dwyer 
1100359b85cSArthur O'Dwyer template <class _CharT, class _Traits, class _RT>
111*9783f28cSLouis Dionne inline _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
112*9783f28cSLouis Dionne operator<<(basic_ostream<_CharT, _Traits>& __os, const lognormal_distribution<_RT>& __x) {
1130359b85cSArthur O'Dwyer   return __os << __x.__nd_;
1140359b85cSArthur O'Dwyer }
1150359b85cSArthur O'Dwyer 
1160359b85cSArthur O'Dwyer template <class _CharT, class _Traits, class _RT>
117*9783f28cSLouis Dionne inline _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
118*9783f28cSLouis Dionne operator>>(basic_istream<_CharT, _Traits>& __is, lognormal_distribution<_RT>& __x) {
1190359b85cSArthur O'Dwyer   return __is >> __x.__nd_;
1200359b85cSArthur O'Dwyer }
1210359b85cSArthur O'Dwyer 
122344cef66SArthur O'Dwyer _LIBCPP_END_NAMESPACE_STD
123344cef66SArthur O'Dwyer 
124344cef66SArthur O'Dwyer _LIBCPP_POP_MACROS
125344cef66SArthur O'Dwyer 
126344cef66SArthur O'Dwyer #endif // _LIBCPP___RANDOM_LOGNORMAL_DISTRIBUTION_H
127