|
| 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 | +// SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. |
| 7 | +// |
| 8 | +//===----------------------------------------------------------------------===// |
| 9 | + |
| 10 | +#ifndef _CUDA_STD___CHI_SQUARED_DISTRIBUTION_H |
| 11 | +#define _CUDA_STD___CHI_SQUARED_DISTRIBUTION_H |
| 12 | + |
| 13 | +#include <cuda/std/detail/__config> |
| 14 | + |
| 15 | +#if defined(_CCCL_IMPLICIT_SYSTEM_HEADER_GCC) |
| 16 | +# pragma GCC system_header |
| 17 | +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_CLANG) |
| 18 | +# pragma clang system_header |
| 19 | +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC) |
| 20 | +# pragma system_header |
| 21 | +#endif // no system header |
| 22 | + |
| 23 | +#include <cuda/std/__limits/numeric_limits.h> |
| 24 | +#include <cuda/std/__random/gamma_distribution.h> |
| 25 | +#include <cuda/std/__random/is_valid.h> |
| 26 | + |
| 27 | +#include <cuda/std/__cccl/prologue.h> |
| 28 | + |
| 29 | +_CCCL_BEGIN_NAMESPACE_CUDA_STD |
| 30 | + |
| 31 | +template <class _RealType = double> |
| 32 | +class chi_squared_distribution |
| 33 | +{ |
| 34 | + static_assert(__libcpp_random_is_valid_realtype<_RealType>, "RealType must be a supported floating-point type"); |
| 35 | + |
| 36 | +public: |
| 37 | + // types |
| 38 | + using result_type = _RealType; |
| 39 | + |
| 40 | + class param_type |
| 41 | + { |
| 42 | + private: |
| 43 | + result_type __n_ = result_type{1}; |
| 44 | + |
| 45 | + public: |
| 46 | + using distribution_type = chi_squared_distribution; |
| 47 | + |
| 48 | + constexpr param_type() noexcept = default; |
| 49 | + |
| 50 | + _CCCL_API constexpr explicit param_type(result_type __n) noexcept |
| 51 | + : __n_{__n} |
| 52 | + {} |
| 53 | + |
| 54 | + [[nodiscard]] _CCCL_API constexpr result_type n() const noexcept |
| 55 | + { |
| 56 | + return __n_; |
| 57 | + } |
| 58 | + |
| 59 | + [[nodiscard]] friend _CCCL_API constexpr bool operator==(const param_type& __x, const param_type& __y) noexcept |
| 60 | + { |
| 61 | + return __x.__n_ == __y.__n_; |
| 62 | + } |
| 63 | +#if _CCCL_STD_VER <= 2017 |
| 64 | + [[nodiscard]] friend _CCCL_API constexpr bool operator!=(const param_type& __x, const param_type& __y) noexcept |
| 65 | + { |
| 66 | + return !(__x == __y); |
| 67 | + } |
| 68 | +#endif // _CCCL_STD_VER <= 2017 |
| 69 | + }; |
| 70 | + |
| 71 | +private: |
| 72 | + param_type __p_{}; |
| 73 | + |
| 74 | +public: |
| 75 | + // constructor and reset functions |
| 76 | + constexpr chi_squared_distribution() noexcept = default; |
| 77 | + |
| 78 | + _CCCL_API constexpr explicit chi_squared_distribution(result_type __n) noexcept |
| 79 | + : __p_{param_type{__n}} |
| 80 | + {} |
| 81 | + _CCCL_API constexpr explicit chi_squared_distribution(const param_type& __p) noexcept |
| 82 | + : __p_{__p} |
| 83 | + {} |
| 84 | + _CCCL_API void reset() noexcept {} |
| 85 | + |
| 86 | + // generating functions |
| 87 | + template <class _URng> |
| 88 | + [[nodiscard]] _CCCL_API result_type operator()(_URng& __g) |
| 89 | + { |
| 90 | + return (*this)(__g, __p_); |
| 91 | + } |
| 92 | + template <class _URng> |
| 93 | + [[nodiscard]] _CCCL_API result_type operator()(_URng& __g, const param_type& __p) |
| 94 | + { |
| 95 | + static_assert(__cccl_random_is_valid_urng<_URng>, "URng must meet the UniformRandomBitGenerator requirements"); |
| 96 | + return gamma_distribution<result_type>(__p.n() / 2, 2)(__g); |
| 97 | + } |
| 98 | + |
| 99 | + // property functions |
| 100 | + [[nodiscard]] _CCCL_API constexpr result_type n() const noexcept |
| 101 | + { |
| 102 | + return __p_.n(); |
| 103 | + } |
| 104 | + |
| 105 | + [[nodiscard]] _CCCL_API constexpr param_type param() const noexcept |
| 106 | + { |
| 107 | + return __p_; |
| 108 | + } |
| 109 | + _CCCL_API constexpr void param(const param_type& __p) noexcept |
| 110 | + { |
| 111 | + __p_ = __p; |
| 112 | + } |
| 113 | + |
| 114 | + [[nodiscard]] _CCCL_API static constexpr result_type min() noexcept |
| 115 | + { |
| 116 | + return result_type{0}; |
| 117 | + } |
| 118 | + [[nodiscard]] _CCCL_API static constexpr result_type max() noexcept |
| 119 | + { |
| 120 | + return numeric_limits<result_type>::infinity(); |
| 121 | + } |
| 122 | + |
| 123 | + [[nodiscard]] friend _CCCL_API constexpr bool |
| 124 | + operator==(const chi_squared_distribution& __x, const chi_squared_distribution& __y) noexcept |
| 125 | + { |
| 126 | + return __x.__p_ == __y.__p_; |
| 127 | + } |
| 128 | +#if _CCCL_STD_VER <= 2017 |
| 129 | + [[nodiscard]] friend _CCCL_API constexpr bool |
| 130 | + operator!=(const chi_squared_distribution& __x, const chi_squared_distribution& __y) noexcept |
| 131 | + { |
| 132 | + return !(__x == __y); |
| 133 | + } |
| 134 | +#endif // _CCCL_STD_VER <= 2017 |
| 135 | + |
| 136 | +#if !_CCCL_COMPILER(NVRTC) |
| 137 | + template <class _CharT, class _Traits> |
| 138 | + friend ::std::basic_ostream<_CharT, _Traits>& |
| 139 | + operator<<(::std::basic_ostream<_CharT, _Traits>& __os, const chi_squared_distribution& __x) |
| 140 | + { |
| 141 | + using ostream_type = ::std::basic_ostream<_CharT, _Traits>; |
| 142 | + using ios_base = typename ostream_type::ios_base; |
| 143 | + const typename ios_base::fmtflags __flags = __os.flags(); |
| 144 | + const _CharT __fill = __os.fill(); |
| 145 | + const ::std::streamsize __precision = __os.precision(); |
| 146 | + __os.flags(ios_base::dec | ios_base::left | ios_base::scientific); |
| 147 | + __os.precision(numeric_limits<result_type>::max_digits10); |
| 148 | + __os << __x.n(); |
| 149 | + __os.flags(__flags); |
| 150 | + __os.fill(__fill); |
| 151 | + __os.precision(__precision); |
| 152 | + return __os; |
| 153 | + } |
| 154 | + |
| 155 | + template <class _CharT, class _Traits> |
| 156 | + friend ::std::basic_istream<_CharT, _Traits>& |
| 157 | + operator>>(::std::basic_istream<_CharT, _Traits>& __is, chi_squared_distribution& __x) |
| 158 | + { |
| 159 | + using istream_type = ::std::basic_istream<_CharT, _Traits>; |
| 160 | + using ios_base = typename istream_type::ios_base; |
| 161 | + const typename ios_base::fmtflags __flags = __is.flags(); |
| 162 | + __is.flags(ios_base::dec | ios_base::skipws); |
| 163 | + result_type __n; |
| 164 | + __is >> __n; |
| 165 | + if (!__is.fail()) |
| 166 | + { |
| 167 | + __x.param(param_type{__n}); |
| 168 | + } |
| 169 | + __is.flags(__flags); |
| 170 | + return __is; |
| 171 | + } |
| 172 | +#endif // !_CCCL_COMPILER(NVRTC) |
| 173 | +}; |
| 174 | + |
| 175 | +_CCCL_END_NAMESPACE_CUDA_STD |
| 176 | + |
| 177 | +#include <cuda/std/__cccl/epilogue.h> |
| 178 | + |
| 179 | +#endif // _CUDA_STD___CHI_SQUARED_DISTRIBUTION_H |
0 commit comments