From e10c7380486e07b277001ab3aa653b74aa990f79 Mon Sep 17 00:00:00 2001 From: justanothercatgirl Date: Mon, 24 Feb 2025 21:26:32 +0300 Subject: Added 227 --- libprakpp/include/prakmath.hpp | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) (limited to 'libprakpp/include/prakmath.hpp') diff --git a/libprakpp/include/prakmath.hpp b/libprakpp/include/prakmath.hpp index 54b902d..ebd02bf 100644 --- a/libprakpp/include/prakmath.hpp +++ b/libprakpp/include/prakmath.hpp @@ -4,6 +4,7 @@ #include #include #include +#include #ifdef __x86_64__ #include @@ -186,6 +187,45 @@ scalar: return linear + finalize(buf); } + +template +T sum(const std::vector &args) { + T res{}; + for (const T& x : args) res += x; + return res; +} +template +T prod(const std::vector &args) { + T res = 1; + for (const T& x : args) res *= x; + return res; +} +template +T hypot(const std::vector &args) { + return std::sqrt(args[0]*args[0] + args[1]*args[1]); +} +template +struct generic_arith { + T operator()(const std::vector &args) { + return std::accumulate(args.cbegin(), args.cend(), T{}, op{}); + } +}; +template +struct plus: public generic_arith>{}; +template +struct minus: public generic_arith>{}; +template +T diff(const std::vector &args) { return std::abs(args[0] - args[1]); } +template +T quot(const std::vector &args) { return args[0] / args[1]; } +template T log(const std::vector &arg){ return std::log(arg[0]); } +template T inv(const std::vector &arg){ return 1/arg[0]; } +template T add(const std::vector &arg) { return val + arg[0]; } +template T sub(const std::vector &arg) { return arg[0] - val; } +template T mul(const std::vector &arg) {return arg[0] * val; } +template T div(const std::vector &arg) {return arg[0] / val; } + + template T avg(const std::vector &args) { T init{}; @@ -195,6 +235,8 @@ T avg(const std::vector &args) { return init / args.size(); } +// Actually calculates not the standard deviation, but standard +// deviation OF the mean. This means additionally dividing by \sqrt{N-1} template T stddev(const std::vector &args) { T sum = {}; -- cgit v1.2.3-70-g09d2