/builds/MusicScience37Projects/numerical-analysis/numerical-collection-cpp/include/num_collect/functions/gamma.h
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2024 MusicScience37 (Kenta Kabashima) |
3 | | * |
4 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | | * you may not use this file except in compliance with the License. |
6 | | * You may obtain a copy of the License at |
7 | | * |
8 | | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | | * |
10 | | * Unless required by applicable law or agreed to in writing, software |
11 | | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | | * See the License for the specific language governing permissions and |
14 | | * limitations under the License. |
15 | | */ |
16 | | /*! |
17 | | * \file |
18 | | * \brief Definition of gamma function. |
19 | | */ |
20 | | #pragma once |
21 | | |
22 | | #include <cmath> |
23 | | #include <complex> |
24 | | |
25 | | #include "num_collect/constants/pi.h" |
26 | | #include "num_collect/functions/impl/gamma_lanczos.h" |
27 | | namespace num_collect::functions { |
28 | | |
29 | | /*! |
30 | | * \brief Calculate a value of gamma function. |
31 | | * |
32 | | * \param[in] x Argument. |
33 | | * \return Value. |
34 | | */ |
35 | 10 | [[nodiscard]] constexpr auto gamma(float x) -> float { |
36 | 10 | if (x < 1.0F) { |
37 | 4 | const float pi_1mx = constants::pi<float> * (1.0F - x); |
38 | 4 | return pi_1mx / std::sin(pi_1mx) / |
39 | | // NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers) |
40 | 4 | impl::gamma_lanczos<float>::gamma(2.0F - x); |
41 | 4 | } |
42 | 6 | return impl::gamma_lanczos<float>::gamma(x); |
43 | 10 | } |
44 | | |
45 | | /*! |
46 | | * \brief Calculate a value of gamma function. |
47 | | * |
48 | | * \param[in] x Argument. |
49 | | * \return Value. |
50 | | */ |
51 | 10 | [[nodiscard]] constexpr auto gamma(double x) -> double { |
52 | 10 | if (x < 1.0) { |
53 | 4 | const double pi_1mx = constants::pi<double> * (1.0 - x); |
54 | 4 | return pi_1mx / std::sin(pi_1mx) / |
55 | | // NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers) |
56 | 4 | impl::gamma_lanczos<double>::gamma(2.0 - x); |
57 | 4 | } |
58 | 6 | return impl::gamma_lanczos<double>::gamma(x); |
59 | 10 | } |
60 | | |
61 | | /*! |
62 | | * \brief Calculate a value of gamma function. |
63 | | * |
64 | | * \param[in] x Argument. |
65 | | * \return Value. |
66 | | */ |
67 | | [[nodiscard]] constexpr auto gamma(std::complex<float> x) |
68 | 4 | -> std::complex<float> { |
69 | 4 | if (x.real() < 1.0F) { |
70 | 2 | const std::complex<float> pi_1mx = constants::pi<float> * (1.0F - x); |
71 | 2 | return pi_1mx / std::sin(pi_1mx) / |
72 | | // NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers) |
73 | 2 | impl::gamma_lanczos<float>::gamma(2.0F - x); |
74 | 2 | } |
75 | 2 | return impl::gamma_lanczos<float>::gamma(x); |
76 | 4 | } |
77 | | |
78 | | /*! |
79 | | * \brief Calculate a value of gamma function. |
80 | | * |
81 | | * \param[in] x Argument. |
82 | | * \return Value. |
83 | | */ |
84 | | [[nodiscard]] constexpr auto gamma(std::complex<double> x) |
85 | 4 | -> std::complex<double> { |
86 | 4 | if (x.real() < 1.0) { |
87 | 2 | const std::complex<double> pi_1mx = constants::pi<double> * (1.0 - x); |
88 | 2 | return pi_1mx / std::sin(pi_1mx) / |
89 | | // NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers) |
90 | 2 | impl::gamma_lanczos<double>::gamma(2.0 - x); |
91 | 2 | } |
92 | 2 | return impl::gamma_lanczos<double>::gamma(x); |
93 | 4 | } |
94 | | |
95 | | /*! |
96 | | * \brief Calculate a natural logarithm of a value of gamma function. |
97 | | * |
98 | | * \param[in] x Argument. |
99 | | * \return Value. |
100 | | */ |
101 | 8 | [[nodiscard]] constexpr auto log_gamma(float x) -> float { |
102 | 8 | return impl::gamma_lanczos<float>::log_gamma(x); |
103 | 8 | } |
104 | | |
105 | | /*! |
106 | | * \brief Calculate a natural logarithm of a value of gamma function. |
107 | | * |
108 | | * \param[in] x Argument. |
109 | | * \return Value. |
110 | | */ |
111 | 8 | [[nodiscard]] constexpr auto log_gamma(double x) -> double { |
112 | 8 | return impl::gamma_lanczos<double>::log_gamma(x); |
113 | 8 | } |
114 | | |
115 | | } // namespace num_collect::functions |