diff options
Diffstat (limited to 'libprakpp/include/prakmath.hpp')
-rw-r--r-- | libprakpp/include/prakmath.hpp | 42 |
1 files changed, 42 insertions, 0 deletions
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 <concepts> #include <iomanip> #include <numeric> +#include <functional> #ifdef __x86_64__ #include <immintrin.h> @@ -186,6 +187,45 @@ scalar: return linear + finalize(buf); } + +template<arithmetic T> +T sum(const std::vector<T> &args) { + T res{}; + for (const T& x : args) res += x; + return res; +} +template<std::floating_point T> +T prod(const std::vector<T> &args) { + T res = 1; + for (const T& x : args) res *= x; + return res; +} +template <std::floating_point T> +T hypot(const std::vector<T> &args) { + return std::sqrt(args[0]*args[0] + args[1]*args[1]); +} +template <arithmetic T, class op> +struct generic_arith { + T operator()(const std::vector<T> &args) { + return std::accumulate(args.cbegin(), args.cend(), T{}, op{}); + } +}; +template <arithmetic T> +struct plus: public generic_arith<T, std::plus<T>>{}; +template <arithmetic T> +struct minus: public generic_arith<T, std::minus<T>>{}; +template <arithmetic T> +T diff(const std::vector<T> &args) { return std::abs(args[0] - args[1]); } +template <arithmetic T> +T quot(const std::vector<T> &args) { return args[0] / args[1]; } +template <arithmetic T> T log(const std::vector<T> &arg){ return std::log(arg[0]); } +template <std::floating_point T> T inv(const std::vector<T> &arg){ return 1/arg[0]; } +template <typename T, T val> T add(const std::vector<T> &arg) { return val + arg[0]; } +template <typename T, T val> T sub(const std::vector<T> &arg) { return arg[0] - val; } +template <arithmetic T, T val> T mul(const std::vector<T> &arg) {return arg[0] * val; } +template <arithmetic T, T val> T div(const std::vector<T> &arg) {return arg[0] / val; } + + template <arithmetic T> T avg(const std::vector<T> &args) { T init{}; @@ -195,6 +235,8 @@ T avg(const std::vector<T> &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 <arithmetic T> T stddev(const std::vector<T> &args) { T sum = {}; |