14824e7fdSDimitry Andric //===----------------------------------------------------------------------===//
24824e7fdSDimitry Andric //
34824e7fdSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44824e7fdSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
54824e7fdSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
64824e7fdSDimitry Andric //
74824e7fdSDimitry Andric //===----------------------------------------------------------------------===//
84824e7fdSDimitry Andric 
94824e7fdSDimitry Andric #ifndef _LIBCPP___RANDOM_LOGNORMAL_DISTRIBUTION_H
104824e7fdSDimitry Andric #define _LIBCPP___RANDOM_LOGNORMAL_DISTRIBUTION_H
114824e7fdSDimitry Andric 
124824e7fdSDimitry Andric #include <__config>
135f757f3fSDimitry Andric #include <__random/is_valid.h>
144824e7fdSDimitry Andric #include <__random/normal_distribution.h>
154824e7fdSDimitry Andric #include <cmath>
164824e7fdSDimitry Andric #include <iosfwd>
174824e7fdSDimitry Andric #include <limits>
184824e7fdSDimitry Andric 
194824e7fdSDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
204824e7fdSDimitry Andric #  pragma GCC system_header
214824e7fdSDimitry Andric #endif
224824e7fdSDimitry Andric 
234824e7fdSDimitry Andric _LIBCPP_PUSH_MACROS
244824e7fdSDimitry Andric #include <__undef_macros>
254824e7fdSDimitry Andric 
264824e7fdSDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD
274824e7fdSDimitry Andric 
2804eeddc0SDimitry Andric template <class _RealType = double>
29*cb14a3feSDimitry Andric class _LIBCPP_TEMPLATE_VIS lognormal_distribution {
305f757f3fSDimitry Andric   static_assert(__libcpp_random_is_valid_realtype<_RealType>::value,
315f757f3fSDimitry Andric                 "RealType must be a supported floating-point type");
325f757f3fSDimitry Andric 
3304eeddc0SDimitry Andric public:
3404eeddc0SDimitry Andric   // types
3504eeddc0SDimitry Andric   typedef _RealType result_type;
3604eeddc0SDimitry Andric 
37*cb14a3feSDimitry Andric   class _LIBCPP_TEMPLATE_VIS param_type {
3804eeddc0SDimitry Andric     result_type __m_;
3904eeddc0SDimitry Andric     result_type __s_;
40*cb14a3feSDimitry Andric 
4104eeddc0SDimitry Andric   public:
4204eeddc0SDimitry Andric     typedef lognormal_distribution distribution_type;
4304eeddc0SDimitry Andric 
__m_(__m)44*cb14a3feSDimitry Andric     _LIBCPP_HIDE_FROM_ABI explicit param_type(result_type __m = 0, result_type __s = 1) : __m_(__m), __s_(__s) {}
4504eeddc0SDimitry Andric 
m()46*cb14a3feSDimitry Andric     _LIBCPP_HIDE_FROM_ABI result_type m() const { return __m_; }
s()47*cb14a3feSDimitry Andric     _LIBCPP_HIDE_FROM_ABI result_type s() const { return __s_; }
4804eeddc0SDimitry Andric 
49*cb14a3feSDimitry Andric     friend _LIBCPP_HIDE_FROM_ABI bool operator==(const param_type& __x, const param_type& __y) {
50*cb14a3feSDimitry Andric       return __x.__m_ == __y.__m_ && __x.__s_ == __y.__s_;
51*cb14a3feSDimitry Andric     }
52*cb14a3feSDimitry Andric     friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const param_type& __x, const param_type& __y) { return !(__x == __y); }
5304eeddc0SDimitry Andric   };
5404eeddc0SDimitry Andric 
5504eeddc0SDimitry Andric private:
5604eeddc0SDimitry Andric   normal_distribution<result_type> __nd_;
5704eeddc0SDimitry Andric 
5804eeddc0SDimitry Andric public:
5904eeddc0SDimitry Andric   // constructor and reset functions
6004eeddc0SDimitry Andric #ifndef _LIBCPP_CXX03_LANG
lognormal_distribution()61*cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI lognormal_distribution() : lognormal_distribution(0) {}
__nd_(__m,__s)62*cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI explicit lognormal_distribution(result_type __m, result_type __s = 1) : __nd_(__m, __s) {}
6304eeddc0SDimitry Andric #else
64*cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI explicit lognormal_distribution(result_type __m = 0, result_type __s = 1) : __nd_(__m, __s) {}
6504eeddc0SDimitry Andric #endif
lognormal_distribution(const param_type & __p)66*cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI explicit lognormal_distribution(const param_type& __p) : __nd_(__p.m(), __p.s()) {}
reset()67*cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI void reset() { __nd_.reset(); }
6804eeddc0SDimitry Andric 
6904eeddc0SDimitry Andric   // generating functions
7004eeddc0SDimitry Andric   template <class _URNG>
operator()71*cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g) {
725f757f3fSDimitry Andric     return std::exp(__nd_(__g));
7304eeddc0SDimitry Andric   }
7404eeddc0SDimitry Andric 
7504eeddc0SDimitry Andric   template <class _URNG>
operator()76*cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g, const param_type& __p) {
7704eeddc0SDimitry Andric     typename normal_distribution<result_type>::param_type __pn(__p.m(), __p.s());
785f757f3fSDimitry Andric     return std::exp(__nd_(__g, __pn));
7904eeddc0SDimitry Andric   }
8004eeddc0SDimitry Andric 
8104eeddc0SDimitry Andric   // property functions
m()82*cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI result_type m() const { return __nd_.mean(); }
s()83*cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI result_type s() const { return __nd_.stddev(); }
8404eeddc0SDimitry Andric 
param()85*cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI param_type param() const { return param_type(__nd_.mean(), __nd_.stddev()); }
param(const param_type & __p)86*cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI void param(const param_type& __p) {
8704eeddc0SDimitry Andric     typename normal_distribution<result_type>::param_type __pn(__p.m(), __p.s());
8804eeddc0SDimitry Andric     __nd_.param(__pn);
8904eeddc0SDimitry Andric   }
9004eeddc0SDimitry Andric 
min()91*cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI result_type min() const { return 0; }
max()92*cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI result_type max() const { return numeric_limits<result_type>::infinity(); }
9304eeddc0SDimitry Andric 
94*cb14a3feSDimitry Andric   friend _LIBCPP_HIDE_FROM_ABI bool operator==(const lognormal_distribution& __x, const lognormal_distribution& __y) {
95*cb14a3feSDimitry Andric     return __x.__nd_ == __y.__nd_;
96*cb14a3feSDimitry Andric   }
97*cb14a3feSDimitry Andric   friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const lognormal_distribution& __x, const lognormal_distribution& __y) {
98*cb14a3feSDimitry Andric     return !(__x == __y);
99*cb14a3feSDimitry Andric   }
10004eeddc0SDimitry Andric 
10104eeddc0SDimitry Andric   template <class _CharT, class _Traits, class _RT>
102*cb14a3feSDimitry Andric   friend basic_ostream<_CharT, _Traits>&
103*cb14a3feSDimitry Andric   operator<<(basic_ostream<_CharT, _Traits>& __os, const lognormal_distribution<_RT>& __x);
10404eeddc0SDimitry Andric 
10504eeddc0SDimitry Andric   template <class _CharT, class _Traits, class _RT>
106*cb14a3feSDimitry Andric   friend basic_istream<_CharT, _Traits>&
107*cb14a3feSDimitry Andric   operator>>(basic_istream<_CharT, _Traits>& __is, lognormal_distribution<_RT>& __x);
10804eeddc0SDimitry Andric };
10904eeddc0SDimitry Andric 
11004eeddc0SDimitry Andric template <class _CharT, class _Traits, class _RT>
111*cb14a3feSDimitry Andric inline _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
112*cb14a3feSDimitry Andric operator<<(basic_ostream<_CharT, _Traits>& __os, const lognormal_distribution<_RT>& __x) {
11304eeddc0SDimitry Andric   return __os << __x.__nd_;
11404eeddc0SDimitry Andric }
11504eeddc0SDimitry Andric 
11604eeddc0SDimitry Andric template <class _CharT, class _Traits, class _RT>
117*cb14a3feSDimitry Andric inline _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
118*cb14a3feSDimitry Andric operator>>(basic_istream<_CharT, _Traits>& __is, lognormal_distribution<_RT>& __x) {
11904eeddc0SDimitry Andric   return __is >> __x.__nd_;
12004eeddc0SDimitry Andric }
12104eeddc0SDimitry Andric 
1224824e7fdSDimitry Andric _LIBCPP_END_NAMESPACE_STD
1234824e7fdSDimitry Andric 
1244824e7fdSDimitry Andric _LIBCPP_POP_MACROS
1254824e7fdSDimitry Andric 
1264824e7fdSDimitry Andric #endif // _LIBCPP___RANDOM_LOGNORMAL_DISTRIBUTION_H
127