numerical-collection-cpp 0.10.0
A collection of algorithms in numerical analysis implemented in C++
Loading...
Searching...
No Matches
de_semi_infinite_integrator.h
Go to the documentation of this file.
1/*
2 * Copyright 2021 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 <cmath>
23#include <type_traits>
24
29#include "num_collect/constants/half.h" // IWYU pragma: keep
30#include "num_collect/constants/one.h" // IWYU pragma: keep
31#include "num_collect/constants/pi.h" // IWYU pragma: keep
32#include "num_collect/constants/two.h" // IWYU pragma: keep
33#include "num_collect/constants/zero.h" // IWYU pragma: keep
36
38
41 "num_collect::integration::de_semi_infinite_integrator");
42
43namespace impl {
44
51template <typename Variable>
53
57template <>
59public:
61 static constexpr float default_max_point = 3.0F;
62};
63
67template <>
69public:
71 static constexpr double default_max_point = 4.0;
72};
73
74} // namespace impl
75
82template <typename Signature>
84
92template <typename Result, base::concepts::real_scalar Variable>
93class de_semi_infinite_integrator<Result(Variable)>
94 : public logging::logging_mixin {
95public:
97 using variable_type = std::decay_t<Variable>;
98
100 using result_type = std::decay_t<Result>;
101
107
115 template <base::concepts::invocable_as<result_type(variable_type)> Function>
116 [[nodiscard]] auto integrate(const Function& function) const
117 -> result_type {
118 const variable_type interval =
119 max_point_ / static_cast<variable_type>(points_);
120
121 constexpr variable_type diff_coeff_center =
123 result_type sum =
124 function(constants::one<variable_type>) * diff_coeff_center;
125
126 for (index_type i = 1; i < points_; ++i) {
127 const variable_type changed_var =
128 interval * static_cast<variable_type>(i);
129 const variable_type pi_sinh_value =
130 constants::pi<variable_type> * std::sinh(changed_var);
131
132 const variable_type var_plus = std::exp(pi_sinh_value);
133 const variable_type diff_coeff_plus = diff_coeff(changed_var);
134 sum += function(var_plus) * diff_coeff_plus;
135
136 const variable_type var_minus =
138 const variable_type diff_coeff_minus = diff_coeff(-changed_var);
139 sum += function(var_minus) * diff_coeff_minus;
140 }
141
142 return sum * interval;
143 }
144
152 template <base::concepts::invocable_as<result_type(variable_type)> Function>
153 [[nodiscard]] auto operator()(const Function& function) const
154 -> result_type {
155 return integrate(function);
156 }
157
165 NUM_COLLECT_PRECONDITION(val > static_cast<variable_type>(0),
166 this->logger(), "Maximum point must be a positive value.");
167 max_point_ = val;
168 return *this;
169 }
170
178 NUM_COLLECT_PRECONDITION(val > 0, this->logger(),
179 "Number of points must be a positive integer.");
180 points_ = val;
181 return *this;
182 }
183
184private:
193 [[nodiscard]] static auto diff_coeff(variable_type changed_var)
194 -> variable_type {
196 std::exp(constants::pi<variable_type> * std::sinh(changed_var)) *
197 std::cosh(changed_var);
198 }
199
201 static constexpr auto default_max_point =
203
205 variable_type max_point_{default_max_point};
206
208 static constexpr index_type default_points = 20;
209
211 index_type points_{default_points};
212};
213
214} // namespace num_collect::integration
auto operator()(const Function &function) const -> result_type
Integrate a function.
auto max_point(variable_type val) -> de_semi_infinite_integrator &
Set maximum point in changed variable.
auto integrate(const Function &function) const -> result_type
Integrate a function.
static auto diff_coeff(variable_type changed_var) -> variable_type
Calculate differential coefficient for change of variable.
auto points(index_type val) -> de_semi_infinite_integrator &
Set number of points.
Class to perform numerical integration on semi-infinite range using double exponential rule.
Class of tags of logs without memory management.
Class to incorporate logging in algorithms.
Definition of half.
Definition of index_type type.
Definition of invocable_as concept.
Definition of log_tag_view class.
Definition of logging_mixin class.
std::ptrdiff_t index_type
Type of indices in this library.
Definition index_type.h:33
constexpr T pi
Value of pi, .
Definition pi.h:40
constexpr T one
Value 1.
Definition one.h:30
Namespace of numerical integration.
constexpr auto de_semi_infinite_integrator_tag
Tag of de_semi_infinite_integrator.
Definition of one.
Definition of pi.
Definition of NUM_COLLECT_PRECONDITION macro.
#define NUM_COLLECT_PRECONDITION(CONDITION,...)
Check whether a precondition is satisfied and throw an exception if not.
Definition of real_scalar concept.
Helper class of constants for use in de_semi_infinite_integrator class.
Definition of two.
Definition of zero.