From e3750707bc391507d37e9ad69f862cb01ba28560 Mon Sep 17 00:00:00 2001 From: lisyarus Date: Thu, 4 Jul 2024 22:58:18 +0300 Subject: [PATCH] Refactor geom::pointwise* and add geom::pointwise_pow --- libs/geom/include/psemek/geom/vector.hpp | 37 ++++++++++++++---------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/libs/geom/include/psemek/geom/vector.hpp b/libs/geom/include/psemek/geom/vector.hpp index 363e2c52..ddbaca50 100644 --- a/libs/geom/include/psemek/geom/vector.hpp +++ b/libs/geom/include/psemek/geom/vector.hpp @@ -343,40 +343,45 @@ namespace psemek::geom return {v[0] * c - v[1] * s, v[0] * s + v[1] * c}; } + template + auto pointwise(F && f, vector const & ... vs) + { + using R = decltype(f(vs[0]...)); + + vector result; + for (std::size_t i = 0; i < N; ++i) + result[i] = f(vs[i]...); + return result; + } + template vector pointwise_mult(vector const & v0, vector const & v1) { - vector result; - for (std::size_t i = 0; i < N; ++i) - result[i] = v0[i] * v1[i]; - return result; + return pointwise([](T const & a, T const & b){ return a * b; }, v0, v1); } template vector pointwise_divide(vector const & v0, vector const & v1) { - vector result; - for (std::size_t i = 0; i < N; ++i) - result[i] = v0[i] / v1[i]; - return result; + return pointwise([](T const & a, T const & b){ return a / b; }, v0, v1); } template vector pointwise_log(vector const & v) { - vector result; - for (std::size_t i = 0; i < N; ++i) - result[i] = std::log(v[i]); - return result; + return pointwise([](T const & a){ return std::log(a); }, v); } template vector pointwise_exp(vector const & v) { - vector result; - for (std::size_t i = 0; i < N; ++i) - result[i] = std::exp(v[i]); - return result; + return pointwise([](T const & a){ return std::exp(a); }, v); + } + + template + vector pointwise_pow(vector const & v0, vector const & v1) + { + return pointwise([](T const & a, T const & b){ return std::pow(a, b); }, v0, v1); } template