Skip to content

Commit

Permalink
define blade addition, subtraction, scalar multiplication
Browse files Browse the repository at this point in the history
Change-Id: I126530ee703ba00933222e9eaef3cb21f6aa7bd3
  • Loading branch information
oliverlee committed Aug 29, 2024
1 parent 51746d0 commit 524d3a5
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
23 changes: 23 additions & 0 deletions rigid_geometric_algebra/blade.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,29 @@ struct blade
return {-std::forward<Self>(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 <class T>
requires std::is_invocable_r_v<value_type, std::multiplies<>, 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
Expand Down
28 changes: 28 additions & 0 deletions test/blade_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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}));
};
}

0 comments on commit 524d3a5

Please sign in to comment.