From fcf77faf19dcb9d75b7842ec7138551bd73418cf Mon Sep 17 00:00:00 2001 From: justanothercatgirl Date: Sun, 24 Nov 2024 19:42:42 +0300 Subject: Added 101 prak --- libprakpp/include/prakmath.hpp | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) (limited to 'libprakpp/include/prakmath.hpp') diff --git a/libprakpp/include/prakmath.hpp b/libprakpp/include/prakmath.hpp index 3a4e4db..a5eb0b8 100644 --- a/libprakpp/include/prakmath.hpp +++ b/libprakpp/include/prakmath.hpp @@ -2,6 +2,7 @@ #include #include +#include #ifdef __x86_64__ #include @@ -184,6 +185,25 @@ scalar: return linear + finalize(buf); } +template +T avg(const std::vector &args) { + T init{}; + for (const T &x : args) { + init += x; + } + return init / args.size(); +} + +template +T stddev(const std::vector &args) { + T sum = {}; + const T a = avg(args); + for (T el : args) { + sum += (a-el)*(a-el); + } + return std::sqrt(sum/args.size()/(args.size()-1)); +} + // take a derivative of function `f` with arguments `at` and with differentiable variable being at index `varidx` template T deriv4(function_t f, std::vector /*I actually do want a copy here*/ at, size_t varidx) { @@ -204,7 +224,7 @@ T deriv4(function_t f, std::vector /*I actually do want a copy here*/ at, /// get error of the calculated value template -T sigma(function_t f, const std::vector &args, const std::vector sigmas) noexcept(false) { +T sigma(function_t f, const std::vector &args, const std::vector &sigmas) noexcept(false) { if (args.size() != sigmas.size()) throw dimension_error("function prak::sigma : Args.size() does not match sigmas.size()"); T sum = 0; for (size_t i = 0; i < args.size(); ++i) { @@ -277,7 +297,7 @@ void least_squares_linear( /// May throw std::bad_optional_access template std::enable_if, std::vector>> -polynomial_regression( +polynominal_regression( size_t degree, std::vector data_x, std::vector data_y, -- cgit v1.2.3-70-g09d2