diff options
Diffstat (limited to 'libprakpp/include/prakmath.hpp')
-rw-r--r-- | libprakpp/include/prakmath.hpp | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/libprakpp/include/prakmath.hpp b/libprakpp/include/prakmath.hpp index ebd02bf..bb09e74 100644 --- a/libprakpp/include/prakmath.hpp +++ b/libprakpp/include/prakmath.hpp @@ -289,6 +289,22 @@ pvalue<T> function(function_t<T> func, const std::vector<pvalue<T>> &args) { return ret; } +template <typename iterator, typename T = iterator::value_type> +requires requires(iterator a){ ++a; *a; } && (std::integral<T> || std::floating_point<T>) +T discrete_integral_trapezoid(iterator X_start, iterator X_end, iterator Y_start, iterator Y_end) +{ + T ret = 0; + for (T Xl = *X_start, Yl = *Y_start; + X_start != X_end && Y_start != Y_end; + ++X_start, ++Y_start) + { + ret += std::abs(*X_start - Xl) * (*Y_start + Yl) / 2; // first iteration will add 0 + Xl = *X_start; + Yl = *Y_start; + } + return ret; +} + /// calculate least-squares linear approximation to fit data /// ax+b = y (ss is an error of Y value) template <std::floating_point T> |