qxLib
integration.h
Go to the documentation of this file.
1 /**
2 
3  @file integration.h
4  @author Khrapov
5  @date 6.08.2022
6  @copyright (c) Nick Khrapov, 2022. All right reserved.
7 
8 **/
9 #pragma once
10 
11 #include <qx/recursive_lambda.h>
12 
13 #include <algorithm>
14 #include <cmath>
15 #include <limits>
16 #include <optional>
17 #include <random>
18 
19 QX_PUSH_SUPPRESS_ALL_WARNINGS();
20 #include <glm/glm.hpp>
21 QX_POP_SUPPRESS_WARNINGS();
22 
23 namespace qx
24 {
25 
26 /**
27  @brief Integrate using rectangle rule
28  @details Supports reversed bounds; noexcept depends on the callback.
29  @param func - target function
30  @param x0 - left border
31  @param x1 - right border
32  @param nIntervalsPer1 - number of intervals per dx = 1
33  @tpara m function_2d_t - function that takes double and returns double
34  @retval - approximate integral; 0 for zero extent;
35  NaN for invalid bounds, zero density or sample count overflow
36 **/
37 template<class function_2d_t>
38 inline double integrate_rectangle_rule(
39  const function_2d_t& func,
40  double x0,
41  double x1,
42  size_t nIntervalsPer1 = 10) noexcept(noexcept(static_cast<double>(func(0.0))));
43 
44 /**
45  @brief Integrate using trapezoid rule
46  @details Supports reversed bounds; noexcept depends on the callback.
47  @param func - target function
48  @param x0 - left border
49  @param x1 - right border
50  @param nIntervalsPer1 - number of intervals per dx = 1
51  @tparam function_2d_t - function that takes double and returns double
52  @retval - approximate integral; 0 for zero extent;
53  NaN for invalid bounds, zero density or sample count overflow
54 **/
55 template<class function_2d_t>
56 inline double integrate_trapezoid_rule(
57  const function_2d_t& func,
58  double x0,
59  double x1,
60  size_t nIntervalsPer1 = 10) noexcept(noexcept(static_cast<double>(func(0.0))));
61 
62 /**
63  @brief Integrate using adaptive midpoint
64  @details Supports reversed bounds; noexcept depends on the callback.
65  @param func - target function
66  @param x0 - left border
67  @param x1 - right border
68  @param fMaxSliceError - max error per one slice
69  @param nIntervalsPer1 - number of intervals per dx = 1
70  @param nMaxRecursion - max recursion depth
71  @tpara m function_2d_t - function that takes double and returns double
72  @retval - approximate integral; 0 for zero extent;
73  NaN for invalid bounds, zero density or sample count overflow
74 **/
75 template<class function_2d_t>
76 inline double integrate_adaptive_midpoint(
77  const function_2d_t& func,
78  double x0,
79  double x1,
80  double fMaxSliceError,
81  size_t nIntervalsPer1 = 10,
82  size_t nMaxRecursion = 300) noexcept(noexcept(static_cast<double>(func(0.0))));
83 
84 /**
85  @brief Integrate using probabilistic algorithm Monte Carlo
86  @details Supports reversed bounds; noexcept depends on the callback.
87  @param funcIsInside - func that returns
88  1 if point is inside shape with positive value
89  0 if point is not inside shape
90  -1 if point is inside shape with negative value
91  @param pos0 - left down corner coordinates
92  @param pos1 - right up corner coordinates
93  @param nPointsPerOneSquare - points per 1 square (more is better)
94  @tparam function_2d_t - function that takes double and returns double
95  @retval - approximate integral; 0 for zero area;
96  NaN for invalid bounds, zero density or sample count overflow
97 **/
98 template<class function_2d_t>
99 inline double integrate_monte_carlo(
100  const function_2d_t& funcIsInside,
101  glm::dvec2 pos0,
102  glm::dvec2 pos1,
103  size_t nPointsPerOneSquare = 1000) noexcept(noexcept(static_cast<int>(funcIsInside(0.0, 0.0))));
104 
105 } // namespace qx
106 
107 #include <qx/math/integration.inl>
double integrate_rectangle_rule(const function_2d_t &func, double x0, double x1, size_t nIntervalsPer1=10) noexcept(noexcept(static_cast< double >(func(0.0))))
Integrate using rectangle rule.
Definition: integration.inl:36