numerical-collection-cpp 0.10.0
A collection of algorithms in numerical analysis implemented in C++
Loading...
Searching...
No Matches
de_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
30#include "num_collect/constants/half.h" // IWYU pragma: keep
31#include "num_collect/constants/one.h" // IWYU pragma: keep
32#include "num_collect/constants/pi.h" // IWYU pragma: keep
33#include "num_collect/constants/zero.h" // IWYU pragma: keep
36
38
41 logging::log_tag_view("num_collect::integration::de_infinite_integrator");
42
49template <typename Signature>
51
59template <typename Result, base::concepts::real_scalar Variable>
60class de_infinite_integrator<Result(Variable)> : public logging::logging_mixin {
61public:
63 using variable_type = std::decay_t<Variable>;
64
66 using result_type = std::decay_t<Result>;
67
72 : logging::logging_mixin(de_infinite_integrator_tag) {}
73
81 template <base::concepts::invocable_as<result_type(variable_type)> Function>
82 [[nodiscard]] auto integrate(const Function& function) const
83 -> result_type {
84 const variable_type interval =
85 max_point_ / static_cast<variable_type>(points_);
86
87 constexpr variable_type diff_coeff_center = half_pi;
88 result_type sum =
89 function(constants::zero<variable_type>) * diff_coeff_center;
90
91 for (index_type i = 1; i < points_; ++i) {
92 const variable_type changed_var =
93 interval * static_cast<variable_type>(i);
94 const variable_type half_pi_sinh = half_pi * std::sinh(changed_var);
95 const variable_type var = std::sinh(half_pi_sinh);
96 const variable_type diff_coeff =
97 half_pi * std::cosh(half_pi_sinh) * std::cosh(changed_var);
98 sum += function(var) * diff_coeff;
99 sum += function(-var) * diff_coeff;
100 }
101
102 return sum * interval;
103 }
104
112 template <base::concepts::invocable_as<result_type(variable_type)> Function>
113 [[nodiscard]] auto operator()(const Function& function) const
114 -> result_type {
115 return integrate(function);
116 }
117
125 NUM_COLLECT_PRECONDITION(val > static_cast<variable_type>(0),
126 this->logger(), "Maximum point must be a positive value.");
127 max_point_ = val;
128 return *this;
129 }
130
138 NUM_COLLECT_PRECONDITION(val > 0, this->logger(),
139 "Number of points must be a positive integer.");
140 points_ = val;
141 return *this;
142 }
143
144private:
146 static constexpr variable_type half_pi =
148
150 static constexpr auto default_max_point = static_cast<variable_type>(4);
151
153 variable_type max_point_{default_max_point};
154
156 static constexpr index_type default_points = 20;
157
159 index_type points_{default_points};
160};
161
162} // namespace num_collect::integration
auto operator()(const Function &function) const -> result_type
Integrate a function.
auto max_point(variable_type val) -> de_infinite_integrator &
Set maximum point in changed variable.
auto integrate(const Function &function) const -> result_type
Integrate a function.
auto points(index_type val) -> de_infinite_integrator &
Set number of points.
Class to perform numerical integration on infinite range using double exponential rule.
Class of tags of logs without memory management.
Class to incorporate logging in algorithms.
Definition of exceptions.
Definition of half.
Definition of index_type type.
Definition of invocable_as concept.
Definition of macros for logging.
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 zero
Value 0.
Definition zero.h:30
constexpr T half
Value 0.5.
Definition half.h:30
Namespace of numerical integration.
constexpr auto de_infinite_integrator_tag
Tag of de_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.
Definition of zero.