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/prakcommon.hpp | 41 ++++++++++++---------------------------- 1 file changed, 12 insertions(+), 29 deletions(-) (limited to 'libprakpp/include/prakcommon.hpp') diff --git a/libprakpp/include/prakcommon.hpp b/libprakpp/include/prakcommon.hpp index c9bced3..47c793c 100644 --- a/libprakpp/include/prakcommon.hpp +++ b/libprakpp/include/prakcommon.hpp @@ -75,24 +75,6 @@ fequal(T x, T y, std::size_t ulps = 1) return std::fabs(x - y) <= ulps * std::ldexp(std::numeric_limits::epsilon(), exp); } -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]); -} /// prints a vector template @@ -124,16 +106,17 @@ using vector = std::vector>; /// prak value / pair value: a value with an error template struct pvalue { T val, err; }; - -template -struct pvalue operator*(const struct pvalue &v, T a) { - return pvalue{v.val * a, v.err * a}; -} - -template -std::ostream &operator<<(std::ostream &os, const struct pvalue &p) { - /* return os << "value {" << p.val << "±" << p.err << "}"; */ - return os << p.val << "±" << p.err; -} +template struct pvalue operator*(const struct pvalue &v, const T &a) { return {v.val * a, v.err * a}; } +template struct pvalue operator*(const T &a, const struct pvalue &v) { return {v.val * a, v.err * a}; } +template struct pvalue operator*(const struct pvalue &a, const struct pvalue &b) { return {a.val * b.val, a.val * b.val * std::hypot(a.err/a.val, b.err/b.val)}; } +template struct pvalue operator/(const struct pvalue &v, const T &a) { return {v.val / a, v.err / a}; } +template struct pvalue operator/(const struct pvalue &a, const struct pvalue &b) { return {a.val / b.val, a.val / b.val * std::hypot(a.err/a.val, b.err/b.val)}; } +template struct pvalue operator+(const struct pvalue &a, const struct pvalue &b) { return {a.val + b.val, std::hypot(a.err, b.err)}; } +template struct pvalue operator-(const struct pvalue &a, const struct pvalue &b) { return {a.val - b.val, std::hypot(a.err, b.err)}; } +template struct pvalue operator+(const struct pvalue &a, const T &b) { return {a.val + b, a.err}; } +template struct pvalue operator-(const struct pvalue &a, const T &b) { return {a.val - b, a.err}; } +template struct pvalue operator+(const T &a, const struct pvalue &b) { return {a + b.val, b.err}; } +template struct pvalue operator-(const T &a, const struct pvalue &b) { return {a - b.val, b.err}; } +template std::ostream &operator<<(std::ostream &os, const struct pvalue &p) { return os << p.val << "±" << p.err; } } // namespace prak -- cgit v1.2.3-70-g09d2