numerical-collection-cpp 0.10.0
A collection of algorithms in numerical analysis implemented in C++
Loading...
Searching...
No Matches
gamma_lanczos.h
Go to the documentation of this file.
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 */
20#pragma once
21
22#include <array>
23#include <cmath>
24#include <complex>
25#include <cstddef>
26#include <type_traits> // IWYU pragma: keep
27
29#include "num_collect/constants/half.h" // IWYU pragma: keep
30
32
37template <base::concepts::real_scalar Real>
39public:
41 static constexpr std::size_t num_coeffs = 14;
42
44 static constexpr std::array<Real, num_coeffs> coeffs{
45 static_cast<Real>(57.1562356658629235),
46 static_cast<Real>(-59.5979603554754912),
47 static_cast<Real>(14.1360979747417471),
48 static_cast<Real>(-0.491913816097620199),
49 static_cast<Real>(0.339946499848118887e-4),
50 static_cast<Real>(0.465236289270485756e-4),
51 static_cast<Real>(-0.983744753048795646e-4),
52 static_cast<Real>(0.158088703224912494e-3),
53 static_cast<Real>(-0.210264441724104883e-3),
54 static_cast<Real>(0.217439618115212643e-3),
55 static_cast<Real>(-0.164318106536762890e-3),
56 static_cast<Real>(0.844182239838527433e-4),
57 static_cast<Real>(-0.261908384015814087e-4),
58 static_cast<Real>(0.368991826295316234e-5)};
59
61 static constexpr auto constant = static_cast<Real>(0.999999999999997092);
62
64 static constexpr auto series_coeff = static_cast<Real>(2.5066282746310005);
65
67 static constexpr auto rational =
68 static_cast<Real>(671) / static_cast<Real>(128);
69
77 template <typename T>
78 requires(
79 std::is_same_v<T, Real> || std::is_same_v<T, std::complex<Real>>)
80 [[nodiscard]] static constexpr auto gamma(T x) -> T {
81 const T offset_x = x + rational;
82 T series_sum = constant;
83 for (std::size_t i = 0; i < num_coeffs; ++i) {
84 series_sum += coeffs[i] / (x + static_cast<Real>(i + 1U));
85 }
86 using std::exp;
87 using std::log;
88 using std::pow;
89 return pow(offset_x, x + constants::half<Real>) * exp(-offset_x) *
90 series_coeff * series_sum / x;
91 }
92
99 [[nodiscard]] static constexpr auto log_gamma(Real x) -> Real {
100 const Real offset_x = x + rational;
101 Real series_sum = constant;
102 for (std::size_t i = 0; i < num_coeffs; ++i) {
103 series_sum += coeffs[i] / (x + static_cast<Real>(i + 1U));
104 }
105 using std::log;
106 return (x + constants::half<Real>)*log(offset_x) - offset_x +
107 log(series_coeff * series_sum / x);
108 }
109};
110
111} // namespace num_collect::functions::impl
Class to calculate gamma function using approximation derived by Lanczos press2007.
static constexpr auto gamma(T x) -> T
Calculate a value of gamma function.
static constexpr auto constant
Constant term.
static constexpr auto series_coeff
Coefficient for the series.
static constexpr std::array< Real, num_coeffs > coeffs
Coefficients.
static constexpr std::size_t num_coeffs
Number of coefficients.
static constexpr auto rational
Rational for parameter.
static constexpr auto log_gamma(Real x) -> Real
Calculate a natural logarithm of a value of gamma function.
Definition of half.
constexpr T half
Value 0.5.
Definition half.h:30
Namespace of internal implementations.
Definition of real_scalar concept.