From 524d3a59731a5e187245836c30ae38b69855360b Mon Sep 17 00:00:00 2001 From: Oliver Lee Date: Wed, 28 Aug 2024 21:12:36 -0700 Subject: [PATCH] define blade addition, subtraction, scalar multiplication Change-Id: I126530ee703ba00933222e9eaef3cb21f6aa7bd3 --- rigid_geometric_algebra/blade.hpp | 23 +++++++++++++++++++++++ test/blade_test.cpp | 28 ++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/rigid_geometric_algebra/blade.hpp b/rigid_geometric_algebra/blade.hpp index ded8b6a..ef0bf11 100644 --- a/rigid_geometric_algebra/blade.hpp +++ b/rigid_geometric_algebra/blade.hpp @@ -70,6 +70,29 @@ struct blade return {-std::forward(self).coefficient}; } + /// addition + /// + friend constexpr auto operator+(const blade& lhs, const blade& rhs) -> blade + { + return {lhs.coefficient + rhs.coefficient}; + } + + /// subtraction + /// + friend constexpr auto operator-(const blade& lhs, const blade& rhs) -> blade + { + return {lhs.coefficient - rhs.coefficient}; + } + + /// scalar multiplication + /// + template + requires std::is_invocable_r_v, T, value_type> + friend constexpr auto operator*(const T& lhs, const blade& rhs) -> blade + { + return {lhs * rhs.coefficient}; + } + /// obtain the blade with indices in canonical form /// /// Returns same blade expressed in canonical form - i.e. with indices in diff --git a/test/blade_test.cpp b/test/blade_test.cpp index 1ddfeb7..4f98415 100644 --- a/test/blade_test.cpp +++ b/test/blade_test.cpp @@ -67,4 +67,32 @@ auto main() -> int eq(rga::blade<0, 1, 2>{1}, -rga::blade<0, 2, 1>{1}) and eq(rga::blade<0, 1, 2>{1}, -rga::blade<2, 1, 0>{1})); }; + + "addition"_test = [] { + const auto b0 = rga::blade<>{}; + const auto b1 = rga::blade<>{1}; + const auto b2 = rga::blade<>{2}; + + return expect( + eq(b0, b0 + b0) and eq(b1, b0 + b1) and eq(b2, b0 + b2) and + eq(b1, b1 + b0) and eq(b2, b2 + b0) and eq(b2, b1 + b1) and + eq(b2, b2 + b0 + b0)); + }; + + "subtraction"_test = [] { + const auto b0 = rga::blade<>{}; + const auto b1 = rga::blade<>{1}; + const auto b2 = rga::blade<>{2}; + + return expect( + eq(b0, b0 - b0) and eq(-b1, b0 - b1) and eq(-b2, b0 - b2) and + eq(b1, b1 - b0) and eq(b2, b2 - b0) and eq(b0, b1 - b1) and + eq(b2, b2 - b0 - b0)); + }; + + "scalar multiplication"_test = [] { + return expect( + eq(rga::blade<>{2}, 2 * rga::blade<>{1}) and + eq(rga::blade<>{2}, 2.0 * rga::blade<>{1})); + }; }