diff --git a/headers/vmath.hpp/vmath_qua_fun.hpp b/headers/vmath.hpp/vmath_qua_fun.hpp index b8f3280..5c83b55 100644 --- a/headers/vmath.hpp/vmath_qua_fun.hpp +++ b/headers/vmath.hpp/vmath_qua_fun.hpp @@ -165,6 +165,48 @@ namespace vmath_hpp::detail namespace vmath_hpp { + // +operator + + template < typename T > + [[nodiscard]] constexpr qua operator+(const qua& xs) { + return xs; + } + + // -operator + + template < typename T > + [[nodiscard]] constexpr qua operator-(const qua& xs) { + return map_join([](T x){ return -x; }, xs); + } + + // operator+ + + template < typename T > + [[nodiscard]] constexpr qua operator+(const qua& xs, const qua& ys) { + return map_join([](T x, T y){ return x + y; }, xs, ys); + } + + // operator+= + + template < typename T > + constexpr qua& operator+=(qua& xs, const qua& ys) { + return (xs = (xs + ys)); + } + + // operator- + + template < typename T > + [[nodiscard]] constexpr qua operator-(const qua& xs, const qua& ys) { + return map_join([](T x, T y){ return x - y; }, xs, ys); + } + + // operator-= + + template < typename T > + constexpr qua& operator-=(qua& xs, const qua& ys) { + return (xs = (xs - ys)); + } + // operator== template < typename T > diff --git a/untests/vmath_qua_fun_tests.cpp b/untests/vmath_qua_fun_tests.cpp index 4f02c92..fc61a1c 100644 --- a/untests/vmath_qua_fun_tests.cpp +++ b/untests/vmath_qua_fun_tests.cpp @@ -39,4 +39,24 @@ TEST_CASE("vmath/qua_fun") { return acc + x; }, qua{1,2,3,4}) == 10); } + + SUBCASE("Operators") { + STATIC_REQUIRE(+qua(1,-2,3,-4) == qua(1,-2,3,-4)); + STATIC_REQUIRE(-qua(1,-2,3,-4) == qua(-1,2,-3,4)); + + STATIC_REQUIRE(qua(1,2,3,4) + qua(3,4,5,6) == qua(4,6,8,10)); + STATIC_REQUIRE(qua(1,2,3,4) - qua(3,5,7,9) == qua(-2,-3,-4,-5)); + + { + qua v{1,2,3,4}; + REQUIRE(&v == &(v += qua{3,4,5,6})); + REQUIRE(v == qua{4,6,8,10}); + } + + { + qua v{1,2,3,4}; + REQUIRE(&v == &(v -= qua{3,4,5,6})); + REQUIRE(v == qua{-2,-2,-2,-2}); + } + } }